#include
#include
#include
struct list
{
int data; struct list *next;
}
*top=NULL,*head=NULL,*temp;
void display();
void push()
{
temp=(struct list *)malloc(sizeof(struct list));
temp->next=NULL;
printf("\nEnter the data:-");
scanf("%d",&temp->data);
if(head==NULL)
{
head=temp; top=head;
}
else
{
top->next=temp; top=top->next;
}
display();
}
void pop()
{
if(head==NULL)
display();
else
for(temp=head;temp!=NULL;temp=temp->next)
{
if (temp==top)
{
head=NULL;
top=head;
printf("\nThe poped out element is %d",temp->data);
free(temp);
break;
}
if(temp->next==top)
{
top=temp;
printf("\nThe poped out element is %d",temp->next->data);
top->next=NULL;
free(temp->next);
break;
}
}
}
void display()
{
struct list *disp;
if(head==NULL)
printf("\nThe stack is empty");
else
{ printf("\nhead<--");
for(disp=head;disp!=NULL;disp=disp->next)
printf("%d <--",disp->data); printf("NULL");
}
}
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n\n1.Push\t2.Pop\t3.Display\t4.Exit"); printf("\n\nEnter ur choice:->");
scanf("%d",&choice);
switch (choice)
{
case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0);
}
}
}
No comments:
Post a Comment