Question: // Stack Exercise : Complete the tasks given . The correct implementation should be as the comments part at the bottom #include using namespace std;

// Stack Exercise : Complete the tasks given . The correct implementation should be as the comments part at the bottom

#include using namespace std; class Node { public : string name; int salary; Node *next;

Node() { name = " "; salary = 0; } }; //Node

class Stack { Node *top; public: Stack(); ~Stack(); void push(); void *pop(); void print(); }; //Stack

Stack::Stack() { top = NULL; }

Stack::~Stack() { while(top) { Node *s = top->next; delete top; top = s; } }

void Stack::push() { Node *newnode=new Node(); //Task 1 : Enter name and salary

//Task 2 : Insert newnode as the top/front element }

void *Stack::pop() { Node *t=top; if(top==NULL) cout<<"Stack Underflow"<

void Stack::print() { Node *temp= top; // Task 5 : Display all elements in the stack } cout << endl; }

int main() { Stack *st = new Stack; //Task 6 : Call push method 3 times

st->print(); //Task 7 : Call pop method 2 times

cout << "After poped "; st->print(); cout << endl; return 0; }

// Program implementation //push ### /* Enter name : Sarah Enter salary : 4500 Enter name : Hadi Enter salary : 7800 Enter name : Fang Enter salary : 6500 //display### Fang 6500 Hadi 7800 Sarah 4500 //pop The popped element is Fang 6500 The popped element is Hadi 7800 After poped Sarah 4500 */

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!