DEFINE STACK:
Stack is a linear data structure based on LIFO(Last In First Out)
principle in which the insertion of a new element and removal of an existing
element takes place at the same end represented as the top of the stack.
What is a Stack?
1.A stack is a linear data structure where elements are stored in the LIFO (Last
In First Out) principle where the last element inserted would be the first element
to be deleted.
2. To implement the stack, it is required to maintain the pointer to the top of the
stack , which is the last element to be inserted because we can access the
elements only on the top of the stack
3.A stack is an Abstract Data Type (ADT), that is popularly used in most
programming languages. It is named stack because it has the similar operations
as the real-world stacks, for example − a pack of cards or a pile of plates, etc.
4 Stack is considered a complex data structure because it uses other data
structures for implementation, such as Arrays, Linked lists, etc.
Stack Representation
A stack allows all data operations at one end only. At any given time, we
can only access the top element of a stack.
A stack can be implemented by means of Array, Structure, Pointer, and
Linked List. Stack can either be a fixed size one or it may have a sense of
dynamic resizing
Types of Stack Data Structure:
Fixed Size Stack : As the name suggests, a fixed size stack has a fixed size
and cannot grow or shrink dynamically. If the stack is full and an attempt is
made to add an element to it, an overflow error occurs. If the stack is
empty and an attempt is made to remove an element from it, an underflow
error occurs.
Dynamic Size Stack : A dynamic size stack can grow or shrink
dynamically. When the stack is full, it automatically increases its size to
accommodate the new element, and when the stack is empty, it decreases
its size. This type of stack is implemented using a linked list, as it allows
for easy resizing of the stack.
Working of Stack
Stack works on the LIFO pattern. As we can observe in the below figure there are
five memory blocks in the stack; therefore, the size of the stack is 5.
Suppose we want to store the elements in a stack and let's assume that stack is
empty. We have taken the stack of size 5 as shown below in which we are pushing
the elements one by one until the stack becomes full.
Since our stack is full as the size of the stack is 5. In the above cases, we can
observe that it goes from the top to the bottom when we were entering the new
element in the stack. The stack gets filled up from the bottom to the top
When we perform the delete operation on the stack, there is only one way for
entry and exit as the other end is closed. It follows the LIFO pattern, which
means that the value entered first will be removed last. In the above case, the
value 5 is entered first, so it will be removed only after the deletion of all the
other elements.
EXPLAIN THE PUSH,POP AND DISPLAY OPERATIONS OF A STACK
Basic Operations on Stack Data Structure:
Stack operations are usually performed for initialization, usage and, de-
initialization of the stack ADT.
The most fundamental operations in the stack ADT include: push(),
pop(), peek(), isFull(), isEmpty().These are all built-in operations to carry
out data manipulation and to check the status of the stack.
In order to make manipulations in a stack, there are
certain operations provided to us.
push() to insert an element into the stack
pop() to remove an element from the stack
top() Returns the top element of the stack.
isEmpty() returns true if stack is empty else false.
isFull() returns true if the stack is full else false.
1.Stack Insertion: push()
The push() is an operation that inserts elements into the stack.
Adds an item to the stack. If the stack is full, then it is said to be
an Overflow condition
Algorithm for Push Operation:
Before pushing the element to the stack, we check if the stack is full .
If the stack is full (top == capacity-1) , then Stack Overflows and we
cannot insert the element to the stack.
Otherwise, we increment the value of top by 1 (top = top + 1) and the new
value is inserted at top position .
The elements can be pushed into the stack till we reach the capacity of the
stack.
STEPS:
Step 1: First, check whether or not the stack is full
Step 2: If the stack is complete, then exit
Step 3: If not, increment the top by one
Step 4: Insert a new element where the top is pointing
Step 5: Success
2.Stack Deletion: pop()
The pop() is a data manipulation operation which removes elements from the
stack
Removes an item from the stack. The items are popped in the reversed order
in which they are pushed. If the stack is empty, then it is said to be
an Underflow condition.
Algorithm for Pop Operation:
Before popping the element from the stack, we check if the stack
is empty .
If the stack is empty (top == -1), then Stack Underflows and we cannot
remove any element from the stack.
Otherwise, we store the value at top, decrement the value of top by 1 (top =
top – 1) and return the stored top value.
STEPS:
Step 1: First, check whether or not the stack is empty
Step 2: If the stack is empty, then exit
Step 3: If not, access the topmost data element
Step 4: Decrement the top by one
Step 5: Success
3.Retrieving topmost Element from Stack: peek()
The peek() is an operation retrieves the topmost element within the stack,
without deleting it.
This operation is used to check the status of the stack with the help of the
top pointer.
Returns the top element of the stack.
Algorithm for Top Operation:
Before returning the top element from the stack, we check if the stack
is empty.
If the stack is empty (top == -1), we simply print “Stack is empty”.
Otherwise, we return the element stored at index = top .
4. Verifying whether the Stack is full: isFull()
The isFull() operation checks whether the stack is full. This operation is
used to check the status of the stack with the help of top pointer.
Returns true if the stack is full, else false.
Stack Overflow — An error called when an item is pushed onto a stack, but the
stack is full.
Stack Overflow: This occurs when you try to push an element onto a stack
that is already full and has reached its maximum capacity. In other words,
the stack cannot accommodate any more elements. Attempting to push an
element onto a full stack will result in a stack overflow condition.
Algorithm for isFull Operation:
Check for the value of top in stack.
If (top == capacity-1), then the stack is full so return true.
Otherwise, the stack is not full so return false.
5.Verifying whether the Stack is empty: isEmpty()
The isEmpty() operation verifies whether the stack is empty. This
operation is used to check the status of the stack with the help of top
pointer.
Returns true if the stack is empty, else false.
1. Stack Underflow — An error called when an item is called from a
stack, but the stack is empty
Stack Underflow: This occurs when you try to pop an element from an
empty stack. In this case, the stack does not contain any elements to
pop, so attempting to pop an element from an empty stack will result in
a stack underflow condition.
Algorithm for isEmpty Operation:
Check for the value of top in stack.
If (top == -1) , then the stack is empty so return true .
Otherwise, the stack is not empty so return false.
Implementation of Stack in Data Structure
You can perform the implementation of stacks in data structures using two data
structures that are an array and a linked list.
The basic operations that can be performed on a stack include push, pop, and
peek. There are two ways to implement a stack –
Using Array
Using Linked List
EXPLAIN ARRAY IMPLEMENTATION OF A STACK WITH VARIOUS
OPERATIONS.
In an array-based implementation, the push operation is implemented by
incrementing the index of the top element and storing the new element at that
index.
The pop operation is implemented by returning the value stored at the top index
and then decrementing the index of the top element.
Implement Stack using Array:
Step-by-step approach:
1. Initialize an array to represent the stack.
2. Use the end of the array to represent the top of the stack.
3. Implement push (add to end), pop (remove from the end), and peek (check
end) operations, ensuring to handle empty and full stack conditions.
Implement Stack Operations using Array:
Push Operation in Stack:
Adds an item to the stack. If the stack is full, then it is said to be an Overflow
condition.
Algorithm for Push Operation:
Before pushing the element to the stack, we check if the stack is full .
If the stack is full (top == capacity-1) , then Stack Overflows and we
cannot insert the element to the stack.
Otherwise, we increment the value of top by 1 (top = top + 1) and the new
value is inserted at top position .
The elements can be pushed into the stack till we reach the capacity of the
stack.
Pop Operation in Stack:
Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an Underflow
condition.
Algorithm for Pop Operation:
Before popping the element from the stack, we check if the stack is empty .
If the stack is empty (top == -1), then Stack Underflows and we cannot
remove any element from the stack.
Otherwise, we store the value at top, decrement the value of top by 1 (top =
top – 1) and return the stored top value.
Top or Peek Operation in Stack:
Returns the top element of the stack.
Algorithm for Top Operation:
Before returning the top element from the stack, we check if the stack is
empty.
If the stack is empty (top == -1), we simply print “Stack is empty”.
Otherwise, we return the element stored at index = top .
isEmpty Operation in Stack:
Returns true if the stack is empty, else false.
Algorithm for isEmpty Operation :
Check for the value of top in stack.
If (top == -1) , then the stack is empty so return true .
Otherwise,the stack is not empty so return false .
isFull Operation in Stack :
Returns true if the stack is full, else false.
Algorithm for isFull Operation:
Check for the value of top in stack.
If (top == capacity-1), then the stack is full so return true .
Otherwise, the stack is not full so return false.
EXPLAIN PROGRAM FOR ARRAY IMPLEMENTATION OF STACK WITH
VARIOUS OPERATIONS
/*program stacks*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 4
int top=-1,arr[SIZE];
void push();
void pop();
void show();
int main()
{
int choice;
while(1)
{
printf("\n perform operations on the stack \n");
printf("\n 1.push \n 2.pop \n 3.show \n 4.End");
printf("\n enter the choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:show();
break;
case 4:
printf("\n exiting.....");
break;
//exit(o);
default:
printf("\n Invalid choice \n");
}
}
}
void push()
{
int x;
if(top==SIZE-1)
{
printf("\n Overflow");
else
{
printf("\n Enter the element to be added on to the stack \n");
scanf("%d",&x);
top=top+1;
arr[top]=x;
}
}
void pop()
{
if(top==-1)
{
printf(\n Underflow");
}
else{
printf("\n popped element ;%d",arr[top]);
top=top-1;
}
}
void show()
{
if(top==-1)
{
printf("\n underflow \n");
}
else{
printf("\n elements present in the stack :\n")
}
for(int i=top;i>0;i--)
{
printf("%d \n",arr[i]);
}
}
OUTPUT:
Perform operations on the Stack
1.push the element
2.pop the element
3.show
4.end
Enter the choice 1
Enter the element to be added on to the stack
13
Perform operations on the Stack
1.push the element
2.pop the element
3.show
4.end
Enter the choice 1
Enter the element to be added on to the stack
72
Perform operations on the Stack
1.push the element
2.pop the element
3.show
4.end
Enter the choice 3
Elements present in the stack:
72
13
Perform operations on the Stack
1.push the element
2.pop the element
3.show
4.end
Enter the choice 2
Popped element:72
Perform operations on the Stack
1.push the element
2.pop the element
3.show
4.end
Enter the choice 3
Elements present in the stack:
13
Perform operations on the Stack
1.push the element
2.pop the element
3.show
4.end
Enter the choice 4
Complexity Analysis:
Time Complexity:
o push: O(1)
o pop: O(1)
o peek: O(1)
o is_empty: O(1)
o is_full: O(1)
Auxiliary Space: O(n), where n is the number of items in the stack.
Advantages of Array Implementation:
Easy to implement.
Memory is saved as pointers are not involved.
Disadvantages of Array Implementation:
It is not dynamic i.e., it doesn’t grow and shrink depending on needs at
runtime. The total size of the stack must be defined beforehand.