IMPLEMENT POP OPERATION ON STACK USING LINKED LIST

/*PROGRAM TO IMPLEMENT POP OPERATION ON STACK USING LINKED LIST*/
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
void main()
{
struct node *top=NULL,*ptr,*temp;
int num;
char ch;
clrscr();
//PUSHING ELEMENTS IN THE STACK
do
{
temp=(struct node *)malloc(sizeof(struct node));

printf(“Enter the element to be pushed: “);
scanf(“%d”,&num);
temp->data=num;
temp->link=NULL;
if(top==NULL)
{
top=temp;
}
else
{
temp->link=top;
top=temp;
}
fflush(stdin);
printf(“Do you want to push more elements?? “);
scanf(“%c”,&ch);
}while(ch==’y’||ch==’Y’);
printf(“Elements in the stack are: \n”);
for(ptr=top;ptr!=NULL;ptr=ptr->link)
{
printf(“\t%d”,ptr->data);
}
//POP OPERATION
fflush(stdin);
printf(“\nDo you want to perform pop operation on stack?? “);
scanf(“%c”,&ch);
if(ch==’y’||ch==’Y’)
{
do
{
if(top==NULL)
{
printf(“Stack is empty”);
break;
}
else
{
num=top->data;
printf(“Deleted element is: “,num);
top=top->link;
}
fflush(stdin);
printf(“\nDo you want to pop more elements?? “);
scanf(“%c”,&ch);
}while(ch==’y’||ch==’Y’);
}
else
{
exit(0);
}
if(top==NULL)
printf(“Press any key to exit”);
else
{
printf(“Remaining elements in the stack are: \n”);
for(ptr=top;ptr!=NULL;ptr=ptr->link)
{
printf(“%d”,ptr->data);
}
}
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *