Question
// Stack Exercise : Complete the tasks given . The correct // implementation should be as the comments part at the bottom // of this
// Stack Exercise : Complete the tasks given . The correct // implementation should be as the comments part at the bottom // of this file.
// This exercise is for linked List Implementation of Stack
#include
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started