Earn without any Investment!

Saturday, November 13, 2010

Stack Operation Using Linked List

#include
#include
#include
class node
{
public:
int info;
node *next;
};
class stack
{
private:
node *top;
public:
stack()
{
top=NULL;
}
int empty()
{
if(top==NULL)
return(1);
else
return(0);
}
void push(int passed_value)
{
node *temp;
if(top==NULL)
{
top=new node;
top->info=passed_value;
top->next=NULL;
}
else
{
temp=new node();
temp->info=passed_value;
temp->next=top;
top=temp;
}
cout<<"\n"<next;
int x=tmp->info;
delete(tmp);
return(x);
}
void display()
{
if(top==NULL)// if stack is empty
cout<<"\nStack Empty\n"; else { node *currentnode; cout<<"The Contents of the Stack are:\n"; for(currentnode=top;currentnode!=NULL;currentnode=currentnode->next)
cout<info<<"\t"; } } }; void main() { clrscr(); int choice,x; stack mystack; while(1) { cout<<"\n**STACK MENU**\n\n" <<"1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n" <<"Enter ur Choice: "; cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter an element: "; cin>>choice;
mystack.push(choice);
getch();clrscr();
break;
case 2:
if(mystack.empty())
{
cout<<"\nStack Empty\n";
break;
}
x=mystack.pop();
cout<<"\nThe Popped element is : "< getch();clrscr();
break;
case 3:
mystack.display();
getch();clrscr();
break;
case 4:
exit(0);
break;
default:
cout<<"\nEnter Correct Choice:";
}
}
}

No comments:

Post a Comment