Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- using namespace std;
- template <typename T>
- class stackNode {
- public:
- T data;
- stackNode<T> *nextNode = nullptr;
- };
- template <typename T>
- class stackTop {
- private:
- stackNode<T> *top, *bottom;
- public:
- int sizeOfList = 0;
- stackTop() {
- bottom = nullptr;
- top = nullptr;
- }
- void display()
- {
- stackNode<T> *temp = new stackNode<T>;
- temp = bottom;
- while (temp != NULL)
- {
- cout << temp->data << endl;
- temp = temp->nextNode;
- }
- }
- void addNode(T userData) {
- stackNode<T> *tempNode = new stackNode<T>;
- tempNode->data = userData;
- tempNode->nextNode = nullptr;
- if (bottom == nullptr) {
- bottom = tempNode;
- top = tempNode;
- }
- else {
- top->nextNode = tempNode;
- top = tempNode;
- }
- sizeOfList++;
- }
- void pop()
- {
- stackNode<T> *current = new stackNode<T>;
- stackNode<T> *previous = new stackNode<T>;
- current = bottom;
- while (current->nextNode != NULL)
- {
- previous = current;
- current = current->nextNode;
- }
- top = previous;
- previous->nextNode = nullptr;
- delete current;
- }
- };
- void main() {
- stackTop<int> temp;
- temp.addNode(112);
- temp.addNode(122);
- temp.addNode(344);
- temp.display();
- temp.pop();
- cout << endl;
- temp.display();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment