Advertisement
wheelsmanx

CPS 272 Machine Problem 4 source.cpp

Nov 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. #include "Cleanup.h"
  2. #include <iostream>
  3.  
  4.  
  5. using namespace std;
  6. template <typename T>
  7. class stackNode {
  8. public:
  9. T data;
  10. stackNode<T> *nextNode = nullptr;
  11. };
  12.  
  13. template <typename T>
  14. class stackTop {
  15. private:
  16.  
  17. public:
  18. stackNode<T> *top, *bottom;
  19. int sizeOfList = 0;
  20. stackTop() {
  21. bottom = nullptr;
  22. top = nullptr;
  23. }
  24.  
  25. void display()
  26. {
  27. stackNode<T> *temp = new stackNode<T>;
  28. temp = bottom;
  29. while (temp != NULL)
  30. {
  31. cout << temp->data << endl;
  32. temp = temp->nextNode;
  33. }
  34. }
  35. void addNode(T userData) {
  36. stackNode<T> *tempNode = new stackNode<T>;
  37. tempNode->data = userData;
  38. tempNode->nextNode = nullptr;
  39. if (bottom == nullptr) {
  40. bottom = tempNode;
  41. top = tempNode;
  42. }
  43. else {
  44. top->nextNode = tempNode;
  45. top = tempNode;
  46. }
  47. sizeOfList++;
  48. }
  49.  
  50. void pop()
  51. {
  52. sizeOfList--;
  53. stackNode<T> *current = new stackNode<T>;
  54. stackNode<T> *previous = new stackNode<T>;
  55. current = bottom;
  56. while (current->nextNode != NULL)
  57. {
  58. previous = current;
  59. current = current->nextNode;
  60. }
  61. top = previous;
  62. previous->nextNode = nullptr;
  63. delete current;
  64. }
  65. T last() {
  66. stackNode<T> *temp = new stackNode<T>;
  67. temp = top;
  68. T returnObject = temp->data;
  69. return returnObject;
  70. }
  71. };
  72. template <typename T>
  73. bool stackCompare(stackTop<T> lhs, stackTop<T> rhs) {
  74. bool returnObject = false;
  75. stackTop<T> tempA, tempB;
  76. tempA = lhs;
  77. tempB = rhs;
  78. if (tempA.sizeOfList != tempB.sizeOfList) {
  79. return returnObject;
  80. }else {
  81. for (int i = 0; i < tempA.sizeOfList; i++) {
  82. if (tempA.top->data == tempB.top->data) {
  83. tempA.pop();
  84. tempB.pop();
  85. }
  86. else {
  87. cout << "Pattern Does Not Match" << endl;
  88. break;
  89. }
  90. }
  91. return returnObject = true;
  92. }
  93. }
  94.  
  95.  
  96. void main() {
  97. stackTop<char> stackA, stackB, stackC;
  98. string userInput;
  99. cout << "Enter phrase: " << endl;
  100. cin >> userInput;
  101. for (int i = 0; i < userInput.size(); i++) {
  102.  
  103. int tempChar = userInput[i];
  104. stackA.addNode(tempChar);
  105. }
  106. for (int i = 0; i < stackA.sizeOfList; i++) {
  107. char temp = stackA.top->data;
  108. if (temp == '#') {
  109. stackA.pop();
  110. break;
  111. }
  112. else {
  113. stackB.addNode(temp);
  114. }
  115. stackA.pop();
  116. }
  117. if (stackCompare(stackA, stackB)) {
  118. cout << "Pattern matches" << endl;
  119. }
  120.  
  121.  
  122. system("pause");
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement