wheelsmanx

CPS 272 Stack

Nov 17th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. template <typename T>
  6. class stackNode {
  7. public:
  8. T data;
  9. stackNode<T> *nextNode = nullptr;
  10. };
  11.  
  12. template <typename T>
  13. class stackTop {
  14. private:
  15. stackNode<T> *top, *bottom;
  16. public:
  17. int sizeOfList = 0;
  18. stackTop() {
  19. bottom = nullptr;
  20. top = nullptr;
  21. }
  22. void display()
  23. {
  24. stackNode<T> *temp = new stackNode<T>;
  25. temp = bottom;
  26. while (temp != NULL)
  27. {
  28. cout << temp->data << endl;
  29. temp = temp->nextNode;
  30. }
  31. }
  32. void addNode(T userData) {
  33. stackNode<T> *tempNode = new stackNode<T>;
  34. tempNode->data = userData;
  35. tempNode->nextNode = nullptr;
  36. if (bottom == nullptr) {
  37. bottom = tempNode;
  38. top = tempNode;
  39. }
  40. else {
  41. top->nextNode = tempNode;
  42. top = tempNode;
  43. }
  44. sizeOfList++;
  45. }
  46.  
  47. void pop()
  48. {
  49. stackNode<T> *current = new stackNode<T>;
  50. stackNode<T> *previous = new stackNode<T>;
  51. current = bottom;
  52. while (current->nextNode != NULL)
  53. {
  54. previous = current;
  55. current = current->nextNode;
  56. }
  57. top = previous;
  58. previous->nextNode = nullptr;
  59. delete current;
  60. }
  61. };
  62.  
  63.  
  64.  
  65. void main() {
  66. stackTop<int> temp;
  67. temp.addNode(112);
  68. temp.addNode(122);
  69. temp.addNode(344);
  70. temp.display();
  71. temp.pop();
  72. cout << endl;
  73. temp.display();
  74.  
  75. system("pause");
  76. }
Advertisement
Add Comment
Please, Sign In to add comment