Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
class node{
int uservalue;
node* nxtnode;
public:
void setvalue(int v){ uservalue = v; }
int getvalue(){ return uservalue ; }
void setnext(node* n) { nxtnode = n; }
node* getnext() { return nxtnode; }
}; //node class
class myStack{
int size;
node* head;
public:
myStack(){ size=0; head = NULL;}
void push(int d){
node* newnode = new node();
newnode->setvalue(d);
newnode->setnext(head);
head = newnode;
size++;
}///push
int pop(){
int x= head->getvalue();
node* p = head;
head = head->getnext();
delete p;
size--;
return x;
}//ispop()
int isEmpty(){
return (head == NULL);
}///isempty()
int top(){
return head->getvalue();
}//top()
int sz(){
return size;
}//sz
};////stack
main(){
int option=0;
int value;
myStack cm;
while(1){
system("cls");
cout<<"\n\n\t ===== Stack operations =====";
cout<<"\n\n\t1. Push value";
cout<<"\n\n\t2. Pop value";
cout<<"\n\n\t3. Display top value";
cout<<"\n\n\t4. No. of values in the Stack";
cout<<"\n\n\t5. Exit";
cout<<"\n\n\tEnter your choice: ";
cin>>option;
switch(option){
case 1:
cout<<"\n\n\tEnter value: ";
cin>>value;
cm.push(value);
cout<<"\n\n\tValue pushed into stack successfully\n\n\t";
system("pause");
break; //case 1 for push
case 2:
if(cm.isEmpty()){
cout<<"\n\n\tStack is empty.\n\n\t";
}
else{
cout<<"\n\n\tPoped value: "<<cm.pop();
cout<<"\n\n\t";
}
system("pause");
break; //case 2 for pop
case 3:
if(cm.isEmpty()){
cout<<"\n\n\tStack is empty.\n\n\t";
}
else{
cout<<"\n\n\tTop value: "<<cm.top();
cout<<"\n\n\t";
}
system("pause");
break; //case 2 for pop
case 4:
if(cm.isEmpty()){
cout<<"\n\n\tStack is empty.\n\n\t";
}
else{
cout<<"\n\n\tNo. of values in the Stack: "<<cm.sz();
cout<<"\n\n\t";
}
system("pause");
break;
case 5:
exit(0);
break;
}///switch
}///while
}//main
Output:
0 Comments