Stack is a list of elements in which insertions and deletions are at the same end of the list called top. The other end is known as Bottom.Insertion is also known as push, deletion is also known as pop
This code gives the simplest way to implement the Stack Data structure using Link List instead of using Arrays.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0main()
{
struct node
{
int data,n;
struct node *link;
};int n,item;
typedef struct node node;
node *temp,*sp=NULL;
while(1)
{
clrscr();printf(”Please select your choice\n1. PUSH\n2. POP\n3. Search\n4. Display\n5. Ex!t\n”);
scanf(”%d”,&n);switch(n)
{
case 1:
printf(”Enter the data to PUSH into Stack\n”);
scanf(”%d”,&item);
temp=(node *) malloc(sizeof(node));
temp->data=item;
temp->link=sp;
sp=temp;
break;
case 2:
if(sp==NULL)
{
printf(”\nSTACK underflow\n”);
getch();
}
else
{
item=sp->data;
sp=sp->link;
printf(”\nPOP Successful Removed Data Is %d”,item);
getch();
}
break;
case 3:
if(sp==NULL)
{
printf(”\nSTACK underflow\n”);
}
else
{
temp=sp;
printf(”\nEnter data to search from Stack”);
scanf(”%d”,&item);
n=0;
while(temp->data!=item)
{
if(temp->link==NULL)
goto print;
temp=temp->link;
n++;
}
print:
if(temp->data==item)
{
printf(”Data %d Found In Stack at position %d”,item,n+1);
}
else
{
printf(”Data not present in Stack”);
}
getch();
break;
case 4:
if(sp==NULL)
{
printf(”\nSTACK is empty\n”);
}
else
{
temp=sp;
printf(”\n%d”,temp->data);
while(temp->link!=NULL)
{
temp=temp->link;
printf(”\n%d”,temp->data);
}
}
getch();
break;
case 5:
exit(0);
break;
}
}
}
}




