#include
struct node
{
int data;
struct node *pre;
struct node *next;
};
struct node *top,*bottom;
void main()
{
int num,option;
char choice='y';
int pop();
void push(int num);
void display();
clrscr();
while(choice=='y')
{
printf("\n\n1.PUSH");
printf("\n\n2.POP");
printf("\n\n3.DISPLAY\n\n");
printf("\n\nEnter Your choice:");
scanf("%d",&option);
switch(option)
{
case 1:
printf("\n\nEnter The number:");
scanf("%d",&num);
push(num);
printf("\n\nDo you want to continue:");
break;
case 2:
num=pop();
printf("\n\nPOP data=%d",num);
printf("\n\nDo you want to continue:");
break;
case 3:
display();
printf("\n\nDo you want to continue:");
break;
default :
printf("\n\nInvalid option");
printf("\n\nDo you want to continue:");
break;
}
choice=getch();
}
getch();
}//End of Main function
/* function to push data into stack */
void push(int num)
{
if(top==NULL && bottom==NULL)
{
top=bottom=(struct node *)malloc(sizeof(struct node));
top->data=num;
top->next=NULL;
top->pre=NULL;
}
else
{
top->next=(struct node *)malloc(sizeof(struct node));
top->next->data=num;
top->next->next=NULL;
top->next->pre=top;
top=top->next;
}
}
/*fuction to display stack data */
void display()
{
struct node *q=bottom;
if(top==NULL && bottom==NULL)
{
printf("\n\nStack is empty");
return;
}
else
{
printf("\n\n");
while(q!=NULL)
{
printf("%d, ",q->data);
q=q->next;
}
}
}
/*function to pop data from stack */
int pop()
{
int result;
if(top==NULL && bottom==NULL)
{
printf("\n\nStack is Empty\n");
return -32768;
}
else
{
result=top->data;
top=top->pre;
if(top!=NULL)
{
free(top->next);
top->next=NULL;
}
if(top==NULL)
{
free(bottom);
bottom=NULL;
}
return(result);
}
}
No comments:
Post a Comment