Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <cstdio>
- #include <string>
- #include <cstring>
- using namespace std;
- template<typename K>
- class Stack
- {
- public:
- int size;
- Stack(){
- this->first=NULL;
- this->size=0;
- };
- struct Structer
- {
- K value;
- Structer* next;
- };
- Structer* first;
- void Push(K value)
- {
- Structer* tr = new Structer;
- tr->value=value;
- tr->next = first;
- first = tr;
- size++;
- }
- void pop()
- {
- if(first!=NULL){
- Structer* tmp=first;
- first = first->next;
- free(tmp);
- size--;}
- }
- bool empty()
- {
- return (first == NULL) ? true : false;
- }
- };
- template<typename K>
- void does(Stack<K> Str){
- freopen("input.txt", "r", stdin);
- char T;
- int N;
- cin>>T>>N;
- for(int i=0; i<N; i++){
- string P;
- cin>>P;
- int l=P.length();
- if(l==4){
- K value;
- cin>>value;
- cout<<value;
- Str.Push(value);
- }
- else
- Str.pop();
- }
- cout<<Str.size<<' ';
- K sum;
- while (Str.first!=NULL){
- sum+=Str.first->value;
- Str.first=Str.first->next;
- }
- cout<<sum;
- }
- void createStack(char T){
- if (T='I'){
- Stack<int>A;
- does(A);}
- else if (T='D'){
- Stack<double>A;
- does(A);}
- else if (T='S'){
- Stack<string>A;
- does(A);}
- };
- int main(void){
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- char T;
- cin>>T;
- createStack(T);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement