Tuesday 9 October 2012

IMPLEMENT STACK USING LINKED LIST


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *prev;
}*top;
void push();
void pop();
void show();
void main()
{
int ch;
while(1)
{
clrscr();
printf("1. Add element");
printf("\n\n2. delete element");
printf("\n\n3. Show");
printf("\n\n4. Exit");
printf("\n\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
exit(0);
break;
default:
printf("\n\n\n\tWrong choice..");
}

}
}
void push()
{
struct node *temp;
clrscr();
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter element: ");
scanf("%d",&temp->info);
if(top==NULL)
{
temp->prev=NULL;
top=temp;
return;
}
else
{
temp->prev=top;
top=temp;
return;
}
}
void show()
{
struct node *q;
clrscr();
q=top;
if(q==NULL)
{
printf("List empty");
getch();
return;
}
while(q!=NULL)
{
printf("%d\t",q->info);
q=q->prev;
}
getch();
}
void pop()
{
struct node *q;
clrscr();
if(top==NULL)
{
printf("Stack underflows");
getch();
return;
}
else
{
q=top;
top=top->prev;
free(q);
}
printf("item deleted..");
getch();
}