What Is Stack
What Is Stack
Stack is a linear data structure that follows a particular order in which the operations are performed. The
order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is
inserted last, comes out first and FILO implies that the element that is inserted first, comes out last.
There are many real-life examples of a stack. Consider an example of plates stacked over one
another in the canteen. The plate which is at the top is the first one to be removed, i.e. the plate
which has been placed at the bottommost position remains in the stack for the longest period of
time. So, it can be simply seen to follow LIFO(Last In First Out)/FILO(First In Last Out) order.
uses of stack:
1.Plate Stacking: Placing clean plates on a stack, and taking the top one when needed.
2.Book Stacking: Adding books to a pile, and taking the top book for reading.
3.Pancake Making: Adding layers to a pancake stack, with the top layer being the first one
consumed.
4.Luggage Packing: Placing suitcases in a car trunk, with the last one in being the first one out.
5.Cash Register Operations: Processing items in a retail setting, where the last item scanned is
the first processed.
6.Browser History: Navigating web pages with back and forward buttons, similar to a stack.
7.Checkout Lines: Joining a line in a supermarket, with the last person joining being the first
served.
9.Pringles Chips Packaging: Chips are stacked in a container, with the last chip placed being
the first accessed.
10.Paper Trays in a Printer: The printer pulls paper from the top tray first.
11.Queue at a Ticket Counter: The last person to join is usually the first to get a ticket.
12.Stacking Blocks or Toys: Children stack items, with the last one placed being the first
removed.
Advantages of Stack:
1. Efficient data management: Stack helps you manage the data in a LIFO (last in, first out)
method, which is not possible with a Linked list and array.
2. Efficient management of functions: When a function is called, the local variables are
stored in a stack, and it is automatically destroyed once returned.
3. Control over memory: Stack allows you to control how memory is allocated and
deallocated.
4. Smart memory management: Stack automatically cleans up the object.
5. Not easily corrupted: Stack does not get corrupted easily; hence it is more secure and
reliable.
6. Does not allow resizing of variables: Variables cannot be resized.
2.Chances of stack overflow: Creating too many objects on the stack can increase the risk
of stack overflow.
3.Random access is not possible: In a stack, random accessing the data is not possible.
4.Unreliable: When variable storage will get overwritten, it will sometimes leads to
undefined behaviour of the function or program.
5.Undesired termination: The stack will fall outside of the memory area, which might lead
to an abnormal termination.
Implementation of stack:
1.stack using array:
#include<stdio.h>
#define CAPACITY 3
int stack[CAPACITY];
int top=-1;
void push(int x)
{
if(top<CAPACITY-1)
top=top+1;
stack[top]=x;
printf("%d\n",x);
int pop()
if(top>=0)
int val=stack[top];
top=top-1;
return val;
return -1;
int peek()
if(top>=0)
return stack[top];
return -1;
}
int main()
push(10);
push(20);
push(30);
printf("peek is:%d\n",peek());
printf("pop:%d\n",pop());
printf("peek:%d",peek());
#include<stdlib.h>
struct node
int data;
};
void push(int x)
newnode->data=x;
newnode->link=top;
top=newnode;
}
void display()
temp=top;
if(top==0)
printf("empty");
else
while(temp!=0)
printf("%d\n",temp->data);
temp=temp->link;
void peek()
if(top==0)
printf("empty");
else
void pop()
{
temp=top;
if(top==0)
printf("underflow");
else
printf("%d\n",top->data);
top=top->link;
free(temp);
void main()
push(2);
push(3);
push(10);
display();
peek();
pop();
peek();
display();
}
Algoritm of a stack:
Algorithm for Stack using Linked List:
- struct Node {
- Create a global pointer 'top' and set it to NULL to indicate an empty stack.
3. Push operation:
- Set the link of 'newnode' to point to the current top of the stack.
4. Pop operation:
5. Peek operation:
- Check if the stack is not empty (top is not NULL).
6. Display operation:
- Otherwise, while 'temp' is not NULL, print the data of 'temp', then update 'temp' to point to the next node.
7. Main function:
8. Memory management:
- Ensure that memory allocated for each node is properly freed using 'free' to avoid memory leaks.
9. Error handling:
- Implement mechanisms to handle cases of stack underflow (e.g., printing "underflow") or empty stack (e.g.,
printing "empty").
- Consider edge cases, such as pushing to an empty stack or popping from an empty stack, and handle them
appropriately.
2. Push operation:
3. Pop operation:
- Decrement 'top' by 1.
4. Peek operation:
5. Display operation:
- Print stack[i].
6. Main function:
7. Memory management:
- No dynamic memory allocation needed, as the stack is implemented using a fixed-size array.
8. Error handling:
- Implement mechanisms to handle cases of stack overflow (e.g., printing "Stack Overflow") or empty stack (e.g.,
printing "Stack Underflow").
- Consider edge cases, such as pushing to a full stack or popping from an empty stack, and handle them
appropriately.
A stack is a data structure that stores a A queue is a data structure that stores a collection
collection of elements, with operations of elements, with operations to enqueue (add)
to push (add) and pop (remove) elements at the back of the queue, and dequeue
elements from the top of the stack. (remove) elements from the front of the queue.
Stacks are based on the LIFO Queues are based on the FIFO principle, i.e., the
Stacks Queues
principle, i.e., the element inserted at element inserted at the first, is the first element to
the last, is the first element to come come out of the list.
out of the list.
Stacks are implemented using an array Queues are implemented using an array or linked
or linked list data structure. list data structure.
Stacks are often used for recursive Queues are often used in multithreaded
algorithms or for maintaining a history applications, where tasks are added to a queue and
of function calls. executed by a pool of worker threads.
#include<stdlib.h>
struct node
int data;
};
void enqueue(int x)
newnode->data=x;
newnode->next=0;
front=rear=newnode;
else
rear->next=newnode;
rear=newnode;
void display()
if(front==0&&rear==0)
printf("empty");
else
temp=front;
while(temp!=0)
printf("%d\n",temp->data);
temp=temp->next;
void dequeue()
temp=front;
if(front==0&&rear==0)
printf("empty");
else
{
printf("%d\n",front->data);
front=front->next;
free(temp);
void peek()
if(front==0&&rear==0)
printf("empty");
else
printf("%d\n",front->data);
void main()
enqueue(5);
enqueue(0);
enqueue(-3);
display();
peek();
converted code:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
newnode->data = x;
void display()
printf("empty");
else
while(temp != 0)
printf("%d\n",temp->data);
temp = temp->next;
}
void pop() // Change 'dequeue' to 'pop'.
printf("empty");
else
free(temp);
void peek()
printf("empty");
else
void main()
display();
peek();
}