Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Cleanup.h"
- #include <iostream>
- using namespace std;
- template <typename T>
- class stackNode {
- public:
- T data;
- stackNode<T> *nextNode = nullptr;
- };
- template <typename T>
- class stackTop {
- private:
- public:
- stackNode<T> *top, *bottom;
- 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()
- {
- sizeOfList--;
- 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;
- }
- T last() {
- stackNode<T> *temp = new stackNode<T>;
- temp = top;
- T returnObject = temp->data;
- return returnObject;
- }
- };
- template <typename T>
- bool stackCompare(stackTop<T> lhs, stackTop<T> rhs) {
- bool returnObject = false;
- stackTop<T> tempA, tempB;
- tempA = lhs;
- tempB = rhs;
- if (tempA.sizeOfList != tempB.sizeOfList) {
- return returnObject;
- }else {
- for (int i = 0; i < tempA.sizeOfList; i++) {
- if (tempA.top->data == tempB.top->data) {
- tempA.pop();
- tempB.pop();
- }
- else {
- cout << "Pattern Does Not Match" << endl;
- break;
- }
- }
- return returnObject = true;
- }
- }
- void main() {
- stackTop<char> stackA, stackB, stackC;
- string userInput;
- cout << "Enter phrase: " << endl;
- cin >> userInput;
- for (int i = 0; i < userInput.size(); i++) {
- int tempChar = userInput[i];
- stackA.addNode(tempChar);
- }
- for (int i = 0; i < stackA.sizeOfList; i++) {
- char temp = stackA.top->data;
- if (temp == '#') {
- stackA.pop();
- break;
- }
- else {
- stackB.addNode(temp);
- }
- stackA.pop();
- }
- if (stackCompare(stackA, stackB)) {
- cout << "Pattern matches" << endl;
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement