Stack in C
Stack in C
Stack in C
Two operations
that can be performed on a Stack are: Push operation which inserts an element into the
stack. Pop operation which removes the last element that was added into the stack. It
follows Last In First Out(LIFO) Order.
What is Stack?
It is type of linear data structure.
It follows LIFO (Last In First Out) property.
It has only one pointer TOP that points the last or top most element of Stack.
Insertion and Deletion in stack can only be done from top only.
Insertion in stack is also known as a PUSH operation.
Deletion from stack is also known as POP operation in stack.
Stack Representation
The following diagram depicts a stack and its operations −
Basic Operations
Stack operations may involve initializing the stack, using it and then de-initializing it. Apart
from these basic stuffs, a stack is used for the following two primary operations −
• push() − Pushing (storing) an element on the stack.
• pop() − Removing (accessing) an element from the stack.
• peek() − get the top data element of the stack, without removing it.
• isFull() − check if stack is full.
• isEmpty() − check if stack is empty.
Push Operation
The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps −
Pop Operation
Accessing the content while removing it from the stack,
is known as a Pop Operation. In an array implementation of pop() operation, the data element
is not actually removed, instead top is decremented to a lower position in the stack to point to
the next value. But in linked-list implementation, pop() actually removes data element and
deallocates memory space.
A Pop operation may involve the following steps −
Applications of stack:
1. Infix to Postfix /Prefix conversion
2. Redo-undo features at many places like editors, photoshop.
3. Forward and backward feature in web browsers
4. To reverse a string
5. When function (sub-program) is called
6. Used in many algorithms like Tower of Hanoi, tree traversals
7. Other applications can be Backtracking, rat in a maze, N queen problem and
sudoku solver
8. In Graph Algorithms like Topological Sorting and Strongly Connected
Components
Implementation:
There are two ways to implement a stack:
1. Using array
2. Using linked list
A program to implement stack using Array method
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
default:
printf ("\n\t Please Enter a Valid
Choice(1/2/3/4)");
}
while(choice!=4);
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}