What is stack in data structure?
stack can be defined as:
Its is the linear data structure which follow the specific order for handling the data elements.
In stack every element enter from top of stack and also remove from top of the stack.
Stack follow the rule which is called LIFO (Last In First Out) because it is a ordered list.
LIFO mean first element which enter in stack will remove at last and last element will remove first because it follow the specific order.
Diagram for understanding:
Basic Functions of stack:
Push()
Pop()
Peek()
isFull()
isEmpty()
Display()
1:Push() :
Push function is used for insert element in stack from the top
2: Pop()
Pop function is used for remove element from the top.
3: Display()
Display function is used for displaying all elements of stack.
4: Peek()
Peek function is use for checking the top element
5:isFull()
isFull function is used for check either stack is full or not. If stack is full then no element can be inserted more
6:isEmpty()
isEmpty function is checking for either stack is empty or not.
Stack implementation:
stack is implemented wit help of array. Array has fix size it can not b increase at run time .
so we implement the stack using array.
Push element in stack:
Now we can insert element in stack from the top. let see the algorithm of push function.
Algorithm of push function:
1: first check the stack is full or not. If stack is full then print stack overflow.
2: if stack is empty then print underflow.
3: if stack is not full nor empty then insert element in it and increment the top by top++.
4: Return element inserted.
Code of push function:
int push(int a)
{
if(isFull())
{
cout<<endl<<"stack is full";
}
else
{
top++;
array[top]=a;
}
}
firstly we check stack is full or not if not then we simple increment the top and insert element with help of array.Push element in stack:
Now we can delete element in stack from the top. let see the algorithm of pop function.
Algorithm of pop function:
1: first check the stack is full or not. If stack is full then print stack overflow.
2: if stack is empty then print underflow.
3: if stack is not empty then pop element from top and decrement the top by top--.
4: Return success.
Code of pop function:
int pop()
{
if(isEmpty())
{
cout<<"\nstack is empty";
}
else
top--;
}
Display element from stack:
Now we can display element from stack. let see the algorithm of push function.
Algorithm of push function:
1: first check the stack is empty or not. If stack is empty then print stack underflow.
2: if stack is not empty then start a loop by giving condition less then equal to top.so that loop will stop when equal to top.
3: Print elements.
Code of display function:
int display()
{
if(isEmpty())
{
cout<<endl<<"stack is empty"<<endl;
}
else
for(int i=0;i<=top;i++)
{
cout<<endl<<"element of stack: "<<array[i]<<endl;
}
}
isFull() function of stack:
Now we can check if top is equal to size or array then return true otherwise return false.
Code of isFull() function:
bool isFull()
{
if(top==SIZE-1)
return true;
else
return false;
}
isEmpty() function of stack:
Now we can check if top is less then 0 , then return true otherwise return false.
Code of isEmpty() function:
bool isEmpty()
{
if(top<0)
return true;
else
return false;
}
#include<iostream>
using namespace std;
#define SIZE 5
int array[SIZE];
int top=-1;
bool isFull()
{
if(top==SIZE-1)
return true;
else
return false;
}
bool isEmpty()
{
if(top<0)
return true;
else
return false;
}
int push(int a)
{
if(isFull())
{
cout<<endl<<"stack is full";
}
else
{
top++;
array[top]=a;
}
}
int pop()
{
if(isEmpty())
{
cout<<"\nstack is empty";
}
else
top--;
}
int display()
{
if(isEmpty())
{
cout<<endl<<"stack is empty"<<endl;
}
else
for(int i=0;i<=top;i++)
{
cout<<endl<<"element of stack: "<<array[i]<<endl;
}
}
int main()
{
int choice, z=1, value;
while(z==1)
{
cout<<"\n1.PUSH\n2.POP\n3.DISPLAY_STACK\n4:EXIST";
cin>>choice;
switch (choice)
{
case 1:
cout<<"Enter Value for insertion: ";
cin>>value;
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
z=0;
break;
}
}
return 0;
}
0 Comments