Wednesday, 29 August 2012

Stack Using List using C programming

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*link;
};
struct node*top=NULL,*temp;
void main()
{
int choice,data;
clrscr();
while(1)
{
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
printf("\n enter your choice....");
scanf("%d",&choice);
switch(choice)
{
case 1:
temp=(struct node*)malloc(sizeof(struct node));
printf("\n enter a node data...");
scanf("%d",&data);
temp->data=data;
temp->link=top;
top=temp;
break;
case 2:
if(top!=NULL)
{
printf("\n The poped element is %d",top->data);
top=top->link;
}
else
{
printf("\n stack underflow");
}
break;
case 3:
temp=top;
if(temp==NULL)
{
printf("\n stack is empty\n");
}
while(temp!=NULL)
{
printf("(%d)",temp->data);
temp=temp->link;
}
break;
case 4:
exit(0);
}
}
}

No comments:

Post a Comment

There was an error in this gadget