Tutorials, Free Online Tutorials,It Challengers provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, core java, sql, php, c language etc. for beginners and professionals.

Breaking

Data Structure - Stack Push,Pop &Display Question of Pune University (MCA)

------------------------------------------ " Data Structure "---------------------------------------

STACK

#include<iostream>
# include <stdlib.h>
using namespace std;

typedef struct t
{
int data;
t *next;
}Node;

//Class declaration of stack
class stk
{
Node*top;
int elt;
public:
stk()
{
top=NULL;
elt=0;
}
int IsEmpty()
{
if(top==NULL)
return 1;
else
return 0;
}

//Push function

void push(int d)
{
Node*temp;
temp=new Node;
temp->next=NULL;
temp->data=d;
temp->next=top;
top=temp;
elt++;
}

//Pop function

int pop()
{
int d;
Node*temp;
d=top->data;
temp=top;
top=top->next;
delete temp;
elt--;
return d;
}

//Display function

void display()
{
Node*trv;
trv=top;
while(trv!=NULL)
{
cout<<trv->data<<" ";
trv=trv->next;
}
}
};
//Main function & Menu 
int main()
{
stk s;
int n,val;
char ch;
do
{
cout<<"\t*****MENU******\n";
cout<<"\t1.PUSH.   \n";
cout<<"\t2.DISPLAY.\n";
cout<<"\t3.POP.    \n";
cout<<"\t4.Exit.   \n";
cout<<"\nEnter your choice: ";
cin>>n;
switch(n)
{
case 1:        //Insert data
do
{
cout<<"Enter data: ";
cin>>val;
s.push(val);
cout<<"\tDo u want push?: ";
cin>>ch;
}while(ch!='n');
break;
case 2:        //Display data
cout<<"\nElement List is: ";
s.display();
break;
case 3:   //IsEmpty check
do
{
if(s.IsEmpty())
{
cout<<"stack is Empty";
break;
}
else
cout<<"Poped data is: ";
cout<<s.pop();
cout<<"\tDo u want pop: ";
cin>>ch;
}while(ch=='y'||ch=='Y');
break;
case 4:exit(0);
default:cout<<"Invalide option";
}
cout<<"\nDo you want to show menu: ";
cin>>ch;
}while(ch=='y'||ch=='Y');
cin.get();
cin.get();
return 0;
}



No comments:

Post a Comment