Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the stack using a linked list. Use the following class definition for

ID: 3651642 • Letter: I

Question

Implement the stack using a linked list. Use the following class definition for the stack. #include 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 Top(StackElement & 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 }; Allow your program to be menu driven using the following menu. 1. Insert 2. Remove 3. Display top 4. Display 5. Exit Program Guidelines for options: Option 1. Prompt the user for a value to put on the stack and add the value to the stack. Option 2. Remove the top value from the stack. Option 3. Display the value that is currently at the top of the stack Option 4. Display the stack from top to bottom.

Explanation / Answer

#include #include #include using namespace std; struct person { string name; person *next; }; class StackOfPersons { private: person *head; person P; public: StackOfPersons() { head=NULL; } void push(string name) { person *curr=head; person *prev=NULL; if (head==NULL) { head=new person; head->name=name; head->next=NULL; } else { while (curr!=NULL) { prev=curr; curr=curr->next; } curr=new person; curr->name=name; prev->next=curr; curr->next=NULL; } } person* pop() { person *curr=head; person *prev=NULL; while (curr->next!=NULL) { prev=curr; curr=curr->next; } return curr; delete curr; prev->next=NULL; } void print() { person *curr=head; while(curr!=NULL) { coutnext; } cout