Tuesday 24 February 2015

Stack Implementation Using C++

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class StackDemo
{
private:
struct Stack
{
int Data;
struct Stack *Next;
}*Top;
public:
StackDemo
{
Top=NULL;
}
Push()
{
Stack *Source=new Stack;

cout<<"\n Enter Data For Stack\n";
cin>>Source->Data;
Source->Next=NULL;

if(Top==NULL)
{
Top=new Stack;
Top=Source;
}
else
{
Source->Next=Top;
Top=Source;
}
}

Pop()
{
Stack *Source=new Stack;
Source=Top;
if(Source==NULL)
{
cout<<"\n Stack Is Empty";
}
else
{
cout<<"\n Poped Item is :"<<Source->Data;
Top=Top->Next;
delete Source;
}
getch();
}

Display()
{
Stack *Source=new Stack;
Source=Top ;
while(Source!=NULL)
{
cout<<"\n Stack Item is :"<<Source->Data;
Source=Source->Next;
}
getch();
}

GetChoice()
{
int choice;
Home:
clrscr();
cout<<"\n\t\t\t Stack Demo \n";
cout<<"\n\t\t\t1.. Push";
cout<<"\n\t\t\t2.. Pop";
cout<<"\n\t\t\t3.. Display";
cout<<"\n\t\t\t4.. Exit";
cout<<"\n\n\t\t Enter Your Choice\n";
cin>>choice;
switch(choice)
{
case 1:
Push();
break;
case 2:
Pop();
break;
case 3:
Display();
break;
case 4:
exit(0);
case 5:
cout<<"\n Wrong Press";
}
goto Home;
}
};
void main()
{
StackDemo d;
d.GetChoice();
}

No comments:

Post a Comment