Done By: Prof.Dhumane A.V. VIIT, Pune
*/
#include
#include
struct stack
{
int data;
struct stack *next;
};
struct stack* push(struct stack*head)
{
struct stack*trav,*temp;
if(head==NULL)
{
head=(struct stack*)malloc(sizeof(struct stack));
printf("\nEnter the data to be PUSHed ");
scanf("%d",&head->data);
head->next=NULL;
}
else
{
trav=head;
while(trav->next!=NULL)
{
trav=trav->next;
}
temp=(struct stack*)malloc(sizeof(struct stack));
printf("\nEnter the data to be PUSHed ");
scanf("%d",&temp->data);
temp->next=NULL;
trav->next=temp;
}
return head;
}
void display(struct stack*head)
{
struct stack*trav;
printf("\nContent of the stack is :\n");
for(trav=head;trav!=NULL;trav=trav->next)
{
printf("[%d]->",trav->data);
}
}
struct stack* pop(struct stack*head)
{
struct stack*trav;
if(head==NULL)
{
printf("\n Stack Empty !!!");
}
else
{
if(head->next==NULL)
{
printf("\nElement poped is %d",head->data);
head=NULL;
}
else
{
trav=head;
while(trav->next->next!=NULL)
{
trav=trav->next;
}
printf("\nElement poped is %d",trav->next->data);
trav->next=NULL;
}
}
return head;
}
void main()
{
struct stack *head=NULL,*trav;
int choice;
clrscr();
while(1)
{
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1: head=push(head);
break;
case 2: head=pop(head);
break;
case 3: display(head);
break;
case 4: exit(0);
}
}
}
No comments:
Post a Comment