What is Stack ?
Stack is a linear data structure which follows a particular order in which the operations are performed.
The order may be LIFO(Last In First Out) or FILO(First In Last Out).
What is pop operation in Stack ?
-Deletion in stack is called the pop operation in stack.
-Deletion in stack is done on the top of the stack.
Function for pop() function:
void pop()
{
struct stack *ptr=top;
top=top->back;
free(ptr);
}
#include< stdio.h>
#include< conio.h>
#include< alloc.h>
struct stack
{
int data;
struct stack *back;
};
struct stack *top;
void create()
{
char ch;
struct stack *ptr,*cpt;
printf("Enter Number\n");
cpt=(struct node*)malloc(sizeof(struct stack));
scanf("%d",&cpt->data);
cpt->back=NULL;
do
{
printf("Enter Numbern");
ptr=(struct stack*)malloc(sizeof(struct stack));
scanf("%d",&ptr->data);
ptr->back=cpt;
cpt=ptr;
printf("Continue(y/n)?\n");
ch=getch();
}while(ch=='y');
top=cpt;
}
void traverse()
{
struct stack *ptr=top;
printf("Stack is :\n");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->back;
}
}
void pop()
{
struct stack *ptr=top;
top=top->back;
free(ptr);
}
void main()
{
clrscr();
create();
traverse();
pop();
printf("After Pop operation\n");
traverse();
getch();
}
Enter Number
3
Enter Number
4
Continue(y/n)?
Enter Number
7
Continue(y/n)?
Enter Number
1
Continue(y/n)?
Stack is :
1 7 4 3
After Pop operation
Stack is :
7 4 3