#include<stdio.h>
#include<conio.h>
void push();
void pop();
void traverse();
struct node
{ int num;
struct node *next;
}*top=NULL;
typedef struct node node;
typedef struct node* nptr;
void main()
{ char choice;
while(1)
{
clrscr();
printf("-----DYNAMIC STACK-----\n\n");
printf(" 1.PUSH\n");
printf(" 2.POP\n");
printf(" 3.TRAVERSE\n");
printf(" 4.EXIT\n");
printf(" Enter your choice: ");
fflush(stdin);
scanf("%c",&choice);
switch(choice)
{ case '1': push();
break;
case '2': pop();
break;
case '3': traverse();
break;
case '4': exit(0);
default: printf("\n\ninvalid choice");
}
getch();
}
}
void push()
{ nptr p;
int item;
if((p=((nptr)malloc(sizeof(node))))!=NULL)
{ printf("\nenter the item: ");
scanf("%d",&item);
p->num=item;
p->next=top;
top=p;
}
else
{ printf("\noverflow");
}
}
void pop()
{ nptr p;
if(top==NULL)
{ printf("\nUNDERFLOW");
}
else
{ p=top;
top=top->next;
printf("\n %d is deleted",p->num);
free(p);
}
}
void traverse()
{ nptr p;
p=top;
if(top==NULL)
{ printf("\n stack is empty");
}
else
{ printf("\n STACK: ");
while(p!=NULL)
{ printf(" %d",p->num);
p=p->next;
}
}
}
Comments
Post a Comment