Push Pop Linked List


Push Pop Linked List

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct llink
{
int data;
struct llink *next;
};
int c=0;
struct llink *top=NULL;
void push_link();
void pop_link();
void display();
void push_link()
{
int m;
struct llink *newnode;
newnode=(struct llink*)malloc(sizeof(struct llink));
c++;
printf("enter the data in the the node %d ",c);
scanf("%d",&m);
newnode->data=m;
newnode->next=top;
top=newnode;
}
void pop_link()
{
struct llink *p;
if(top==NULL)
printf("\nlinked link is empty");
else
{
p=top;
printf("\nthe deleted item is %d",p->data);
top=top->next;
free(p);
}
}
void display()
{
struct llink* p;
p=top;
while(p!=NULL)
{
printf("\t%d",p->data);
p=p->next;
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\nenter 1. for push");
printf("\nenter 2. for pop");
printf("\nenter 3. for display");
printf("\nenter 4. for exit");
printf("\nenter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
push_link();
break;
case 2:
pop_link();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nwrong choice");
}
}
}