Data structure(STACK)
Data structure(STACK)
Ans:- A Data Structure is a way of organizing and storing data in a computer so that it can be accessed and used
efficiently.
Linear Data Structure:- A Data Structure is said to be linear if its elements are stored in sequential order.
Ex- Array, Linked List, Stack, Queue
Non-Linear Data Structure:- A Data Structure is said to be non-linear if there is no particular sequence of its
elements. Ex- Tree, Graph, Set, Table.
Stack is a Linear Data Structure. The insertion and deletion of data takes place from one side. No operation is
performed in the middle. This special kind of Data Structure is known as Stack.
The elements are inserted in the order as A, B, C, D, E, it represents the stack of five elements. In the above
figure , we want to push ‘A’ element on the stack then the top becomes zero (top=0), similarly the top=1 when
‘B’ element is pushed, top=2 when the ‘C’ element is pushed, top=3 when the ‘D’ element is pushed, and top=4
when the ‘E’ element is pushed.
So whatever the elements I have taken is placed in the stack, now the stack is full. If you want to push another
element there is no place in the stack, so it indicates the overflow. Now the stack is full if you want to pop the
element ‘E’ element has to be deleted first
POP- The pop operation is used to remove the element from the top of the stack. In the time of POP operation
we decrease the value of TOP.
We have to use the pop operation to delete the elements in the stack. So just mention pop () don’t write
arguments in the pop because by default it deletes the top element. The first ‘E’ element is deleted next ‘D’
element…..’ A’. When the top elements are deleting then the top value decreases. When top=-1 the stack
indicates underflow
Algorithm of PUSH:- Input : The new item ITEM to be pushed onto stack.
Output: A stack with newly pushed ITEM at the TOP position of the array.
Data Structure: An array A with TOP as the pointer.
SIZE represents the maximum size of the stack & array index varies from 1 to SIZE.
Step-1 : If TOP ≥ SIZE then
1.1. Print “stack is full”
Step-2 : Else
2.1. TOP=TOP+1
2.2. A[TOP] = ITEM
Step-3 : EndIf
Step-4 : Stop
Function of POP:-
void pop ()
{
if(top == -1)
printf("Underflow");
else
top = top -1;
}
#include <stdio.h>
#include <conio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main ()
{
printf("Enter the number of elements in the stack ");
scanf("%d",&n);
printf("*********Stack operations using array*********");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("Chose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
show();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
getch();
}
void push ()
{
int val;
if (top == n )
printf("\n Overflow");
else
{
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = val;
}
}
void pop ()
{
if(top == -1)
printf("Underflow");
else
top = top -1;
}
void show()
{
for (i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(top == -1)
{
printf("Stack is empty");
}
}