0% found this document useful (0 votes)
4 views

Data structure(STACK)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data structure(STACK)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

 What is Data Structure?

Ans:- A Data Structure is a way of organizing and storing data in a computer so that it can be accessed and used
efficiently.

 Type of Data Structure:-

 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.

 Why Stack is Called LIFO Data Structure?


Ans:- The Data which is inserted in the stack at last, the particular data can be deleted at first. For that reason
Stack is called LIFO (Last In First Out) Data Structure.

 What is Stack Top?


Ans:- Stack is a collection of homogeneous data in linear order where insertion or deletion is possible only from
one side, which is known as TOP.

 What is PUSH & POP?


PUSH- The push operation in a stack is used to add an element to the top of the stack. In the time of PUSH
operation we first increase the value of TOP then we insert the data into the 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

 What is Underflow Condition of Stack?


Ans:- When a stack is empty (i.e. TOP= -1) and we try to delete more element from it, then this condition is
called underflow condition.

 What is Overflow Condition of Stack?


Ans:- When stack is completely full (i.e. TOP= MaxSize -1 ) and we try to insert more element onto stack then
this condition is called overflow condition and no further element could be inserted now until any element is
deleted.

 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

 Algorithm of POP:- Input : A stack with element.


Output : Removes an ITEM from the TOP of the stack if it is not empty.
Data Structure : An array A with TOP as the pointer.
Step-1 : If TOP < 1 then
1.1. Print “Stack is empty”
Step-2 : Else
2.1. ITEM = A[TOP]
2.2. TOP = TOP-1
Step-3 : EndIf
Step-4 : STOP
 Function of PUSH:-
void push ()
{
int val;
if (top == n )
printf("\n Overflow");
else
{
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = val;
}
}

 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");
}
}

You might also like