Showing posts with label STACK. Show all posts
Showing posts with label STACK. Show all posts

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();
}

Monday, 23 February 2015

Stack Program in C


#include<stdio.h>
#include<conio.h>

struct Stack
{
int Data;
struct Stack *Next;
}*Top;

main()
{
int choice;
Home:
clrscr();
printf("\n\t\t\t Stack Demo \n");
printf("\n\t\t\t1.. Push");
printf("\n\t\t\t2.. Pop");
printf("\n\t\t\t3.. Display");
printf("\n\t\t\t4.. Exit");
printf("\n\n\t\t Enter Your Choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
Push();
break;
case 2:
Pop();
break;
case 3:
Display();
break;
case 4:
exit(0);
case 5:
printf("\n Wrong Press");
}
goto Home;
}

Push()
{
struct Stack *Source=(struct Stack*)malloc(sizeof(struct Stack));

printf("\n Enter Data For Stack\n");
scanf("%d",&Source->Data);
Source->Next=NULL;

if(Top==NULL)
{
Top=(struct Stack*)malloc(sizeof(struct Stack));
Top=Source;
}
else
{
Source->Next=Top;
Top=Source;
}
}
Pop()
{
struct Stack *Source=(struct Stack*)malloc(sizeof(struct Stack));
Source=Top;
if(Source==NULL)
{
printf("\n Stack Is Empty");
}
else
{
printf("\n Poped Item is :%d",Source->Data);
Top=Top->Next;
free(Source);
}
getch();
}
Display()
{
struct Stack *Source=(struct Stack*)malloc(sizeof(struct Stack));
Source=Top ;
while(Source!=NULL)
{
printf("\n Stack Item is :%d",Source->Data);
Source=Source->Next;
}
getch();
}