Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template <typename T>
- class Node {
- public:
- T value;
- class Node *next;
- };
- template <typename T>
- class Stack {
- private:
- class Node<T> *top = nullptr;
- public:
- void push(const T);
- T pop();
- void display();
- };
- template <typename T>
- void Stack<T>::push(const T value) {
- Node<T> *newNode = new Node<T>;
- newNode->value = value;
- newNode->next = top;
- top = newNode;
- }
- template <typename T>
- T Stack<T>::pop() {
- if (top != nullptr) {
- cout << "Empty stack!" << endl;
- return 0;
- }
- Node<T> *temp = top;
- T value = temp->value;
- top = top->next;
- delete temp;
- return value;
- }
- template <typename T>
- void Stack<T>::display() {
- Node<T> *temp = top;
- cout << "----------" << endl;
- while (temp != nullptr) {
- cout << temp->value << endl;
- temp = temp->next;
- }
- cout << "----------" << endl;
- }
- void main()
- {
- Stack<int> stack;
- stack.push(3);
- stack.push(5);
- stack.display();
- for (int i = 0; i < 10; i++)
- {
- stack.push(i);
- }
- stack.display();
- cout << "Deleted: " << stack.pop() << endl;
- cout << "Deleted: " << stack.pop() << endl;
- stack.display();
- system("pause");
- }
Add Comment
Please, Sign In to add comment