Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template<class T>
- struct Node
- {
- T data;
- Node* next=nullptr;
- };
- template<class T>
- class Stack
- {
- private:
- Node<T>* first=nullptr;
- public:
- void add_element(T data)
- {
- if(first==nullptr)
- {
- Node<T>* a = new Node<T>;
- a->data=data;
- first=a;
- }
- else
- {
- Node<T>* current=first;
- while(current->next!=nullptr)
- {
- current=current->next;
- }
- Node<T>* a = new Node<T>;
- a->data=data;
- current->next=a;
- }
- }
- T peak()
- {
- Node<T>* current=first;
- while(current->next!=nullptr)
- {
- current=current->next;
- }
- return current->data;
- }
- void pop()
- {
- Node<T>* current=first;
- if(current->next==nullptr)
- {
- delete current->next;
- current->next=nullptr;
- }
- else
- {
- while(current->next->next!=nullptr)
- {
- current=current->next;
- }
- delete current->next;
- current->next=nullptr;
- }
- }
- void print()
- {
- Node<T>* current=first;
- while(current!=nullptr)
- {
- cout<<current->data;
- current=current->next;
- }
- }
- };
- int main()
- {
- Stack<char> s;
- s.add_element('(');
- s.add_element(')');
- s.print();
- cout<<s.peak();
- s.pop();
- s.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement