Question: C++ Get program to run. Hi guys. No actual coding involved. I cant get answer code from this assignment to run. Its from another Chegg

C++ Get program to run.

Hi guys. No actual coding involved.

I cant get answer code from this assignment to run. Its from another Chegg Q&A. If you can provide the running code in a Copy Paste format that would be great. I will Rate Thumbs up! Please test it or Let me know whats wrong. I get bugs when i try it. Let me know if you cant open the page. Ill take provide anything if asked.

http://www.chegg.com/homework-help/questions-and-answers/implement-stack-queue-using-linked-list-use-given-stack-queue-class-definitions-need-write-q11088263

ORIGINAL QUESTION AND THE ANSWER PROVIDED

C++ Get program to run. Hi guys. No actual coding involved. Icant get answer code from this assignment to run. Its from another

The following file is Stack.cpp , which is the implementation file of Stack.h .Here stack is implemented using Linked list .Dev C++ IDE is Used to build the code.

#include #include"stack.h" #include using namespace std; stack ::stack() { mytop=NULL; } bool Stack :: empty() { if(mytop==NULL) return 1; else return 0; } void Stack::push(StackElement X) { struct node *temp; if(mytop==NULL) { mytop=(struct node*)malloc(sizeof(struct node)); mytop->data=X; mytop->next=NULL; } else { temp=(struct node*)malloc(sizeof(struct node)); temp->data=X; temp->next=top; top=temp; } }

int Stack:: top() { return top->data; }

void Stack :: pop() { if(top==NULL) { coutnext; free(temp); }

void Stack:: Sdisplay() { struct node *temp; temp=top; while(temp) // while there are elemnt in stack { coutdata; temp=temp->data; } }

Now the following file is Queue.cpp , which is the implementation file of Queue.h header file.Here queue is implemented using Linked list .Dev C++ IDE is Used to build the code.

#include #include"stack.h" #include using namespace std; Queue::Queue() { myfront=NULL; myback=NULL; }

void Queue:: AddQ(QueueElement x ) { struct node *temp; if (myback == NULL) { myback = (struct node *)malloc(1*sizeof(struct node)); myback->next = NULL; myback->data = x; myfront = myback; } else { temp=(struct node *)malloc(1*sizeof(struct node)); myback->next = temp; temp->data = x; temp->next = NULL; myback = temp; }

}

QueueElement Queue:: Front() { return (myfront->data); }

void Queue :: RemoveQ() { struct node *front1 = front; if (front1 == NULL) { coutnext != NULL) //means more than 1 node are there { front1 = front1->next; free(front); myfront = front1; } else //only one node is there { free(front1); myfront = NULL; myback = NULL; }

}

void Queue :: Qdisplay() { struct node *temp; if( myfront == NULL) coutdata; temp=temp->next; } }

}

Now the following file is Client.cpp. Here evaluation of Reverse Polish Notation Expresssion is done using the above 2 files Stack.cpp and Queue.cpp.

#include"Stack.h" #include"Queue.h" #include #include #include bool isoprtr(char ch) ; void evaluate(char array[50]); int applyOperation(int p1, int p2, char op);

using namespace std int main() { int i; //variable for user choice char str[50]; //temporary character array used for calculation printf(" 1. Enter the expression"); printf(" 2. Evaluate an expression"); printf(" 3. printing all expression that have been evaluated"); printf(" 4. Exit "); while(1) { printf(" Choose Option: "); scanf("%d",&i); switch(i) { case 1: { printf(" Enter the arithmetic expression to be evaluated: "); gets(str) AddQ(str); //the string is added to the queue break; } case 2: { str=Front(); // the front string of queue is copied RemoveQ(); // the front elemnt is copied. evaluate(str);// function is called to evaluate the expression. display(); break; } case 3: { Qdisplay(); // displaying all expression of queue break; } case 4: { exit(0); } default: { printf(" wrong choice for operation"); } } }

return 0; }

void evaluate(char array[50]) { int len,i,j,buf[50],x; stack s; len=strlen(array); j = 0; for(i=0; i

if(array[i]>='0' && array[i]

else if(array[i]==' ') //end of string { if(j>0) { buf[j] = '\0'; x = atoi(buf); //string to integer conversion s.push(x); //using local stack j = 0; } }

else if(isoprtr(array[i])) { oprtr1 = s.top(); s.pop(); oprtr2 = s.top(); s.pop(); s.push(performOperation(oprtr1, oprtr2, array[i])); } } printf(" expression is %s and evaluated_answer is %d ",array, s.top());

} bool isoprtr(char ch) //to check weather the character is operator or not? { if (ch=='+' || ch=='-' || ch=='*' || ch=='/') return true; else return false; }

int applyOperation(int p1, int p2, char op) //module for applying operation and returning resulted answer { int answer; switch(op){ case '+': answer = p2 + p1; break; case '-': answer = p2 - p1; break; case '*': answer = p2 * p1; break; case '/': answer = p2 / p1; break; } return answer; }

Implement the stack and the queue using a linked list. Use the given Stack and Queue class definitions. You will need to write the Stack.cpp, Queue.cpp and your client code. I/Stackh Header File #include using namespace std; typedef int stackElement; struct node StackElement data; node * next; }; class Stack public: Stack); // create an empty stack bool empty) ; // return true if stack is empty, otherwise return false void push (stackelement x); //add a new value to the top of the stack bool ToriStackElement & x); //retrieves the data that is at the top of the stack void pop); // removes the value at the top of the stack void display); //displays the data stored in the stack private: node * mytop; //pointer to the top of the stack }; //Queue.h Header File #include using namespace std; typedef string QueueElement; struct qnode QueueElement data; qnode * next

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!