IMPLEMENTATION OF STACK USING LINKED LIST IN C

#include<stdio.h>
#include<malloc.h>
struct stack
{
int n;
struct stack *next;
}*top=NULL;
typedef struct stack st;
void push();
int pop();
int diplay();
main()
{
int ch;
do
{
printf(“\nmenu\n1.push\n2.pop\n3.display\n4.exit\n”);
printf(“entr th choice\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:break;
}
}while(ch!=4);
}
void push()
{
st *p;
p=(st*)malloc(sizeof(p));
printf(“enter the number\n”);
scanf(“%d”,&p->n);
p->next=top;
top=p;
}
int pop()
{
st *p;
p=top;
if(top==NULL)
{
printf(“stack is empty\n”);
}
else
{
top=top->next;
free(p);
}
return(p->n);
}
int display()
{
st *p;
p=top;
while(p->next!=NULL)
{
printf(“\t%d”,p->n);
p=p->next;
}
printf(“\t%d”,p->n);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s