EICTA, IIT Kanpur

Stacks and Queues: Implementation, Operations and Applications

EICTA Content Team17 March 2025

Stacks and queues are dynamic structures that influence how companies handle and process information in many programming settings when organizing and manipulating data. These unique data structures add a new level of organization and control, allowing programmers to efficiently and systematically simplify processes and address challenging issues.

Stacks, which resemble a pile of objects, operate on the last-in, first-out (LIFO) concept, while queues, which correspond to a line of people, work on the first-in, first-out (FIFO) principle.

Must Read: AI in Fintech: Applications

What is a Stack, its Operations, and its Implementations?

A stack is a type of data structure where only the top of the stack can add or remove components. The stack operates on the pushdown method, pushing the element into the stack and popping them out. The push action adds the element to the stack, and with pop, it is removed from the stack, all from the top of the stack only.

The simple way of understanding how a stack works is considering you have a stack of books, where you can only remove the top one and add on the top one.

Stack works on the last in and first out principle. It is necessary to have the pointer on top of the stack as it will be the last element to be inserted, and programmers can only access the elements from the top to implement the stack. Both array and a linked list can implement it.

You can even use the PEEK option during the stack. It is used to return the value of the topmost element without deleting it from the stack.

What is a Queue, its Operations, and Implementations?

As the name suggests, Queue is similar to a line of people. It is a data structure that works on enqueue and dequeue operations. To help understand how a queue system works, let’s take an example of a queue of people waiting at a billing counter.

The new customers are added in the back, which refers to enqueue, and the old customers are removed from the front, which refers to dequeue.

Queue works on the first in and first out principle. There are many types of queues, such as double-ended, simple, priority, and circular. Like a stack, you can implement it via array and linked lists.

Stack and Queue Principles

Let’s look at how the first in, first out, last in, and last out function works in Stack and Queue.

  • Queue – First in and First out – The first element of the queue is the first one to leave.
  • Stack – Last in and First out – The last element of the stack is the first element to leave the stack.
  • Queue – Last in and Last out – The final element of the queue is the last one to leave the queue.
  • Stack – First in and Last out – The first element of the stack is the last one to leave the stack.

Stack and Queue Applications

Let’s look at the application of stack and queue:

Stack Applications

  • Function Calls and Recursion – The program’s state pushes onto the stack when a function is invoked. The previous function’s execution is continued after the function returns by popping the state off the stack.
  • Undo and Redo Operations – Various apps’ undo-redo functionality employs stacks to remember the prior operations. A new action is added to the stack each time it is completed. The top member of the stack is popped, and the initial procedure is then carried out to reverse the process.
  • Expression Evaluation – The stack data structure evaluates expressions in the infix, postfix, and prefix notations. Operations are carried out depending on the top elements of the stack once operators and operands are pushed onto it.
  • Browser History – Web browsers use stacks to record the websites you visit. The previous URL is taken from the stack when you use the back button and re-added when you access a new page.
  • Balanced Parentheses – To determine whether parentheses are balanced or not, a stack data structure is used. As an opening parenthesis is added, a closing parenthesis is removed from the stack. The parentheses are balanced if the stack is empty after the expression.
  • Backtracking Algorithms – The backtracking approach keeps track of the steps in the solution process using stacks. The method pushes the current state onto the stack and then moves backward, removing the previous state from the stack.

Queue Applications

  • Multiprogramming – Multiple programs executing simultaneously in the main memory are called multi-programming. It is crucial to arrange these numerous programs, and queues are the best way to do this.
  • Network – Devices like routers and switches in a network employ queues. A mail queue, a directory that holds data and manages mail message files, is another use for queues.
  • Job Scheduling – A set number of jobs scheduled to run one after another must be completed by the computer. These jobs are given individually to the processor in a queue-organized fashion.
  • Shared Resources – Waiting lists for a single shared resource are used in queues.

Here are some real-life case applications of stack and queues

Stack Queue
CD/DVD stand ATM booth line
Call center systems Keyboards’ keypress sequence
Undo and redo mechanism in text editors CPU task scheduling
History of web browser Customer waiting time in an inbound call center
Calls logs, emails, photos
YouTube downloads, notifications
Memory allocation by the operating system

Conclusion

In the programming world, stacks and queues are fundamental in creating the landscape of data processing and management. Their relevance can be seen in boosting effectiveness, order, and logic in diverse circumstances in real-world applications.

The queue’s function in organizing task management and the stack’s capacity to enable controlled access have demonstrated their significance in resolving challenging problems across various disciplines.

With the help of stacks and queues, developers and programmers can now create programs and software that functions efficiently, effectively and demonstrates the artistry of efficient data manipulation.

Frequently Asked Questions (FAQs)

1. What is the basic difference between a stack and a queue?

The article explains that a stack follows the Last-In, First-Out (LIFO) principle-only the top element can be added or removed-while a queue follows First-In, First-Out (FIFO), where insertion happens at the rear and deletion at the front. You can think of a stack like a pile of plates (you use the last one placed on top) and a queue like a line of people at a ticket counter (the person who comes first is served first).

2. How are stacks and queues implemented in C in this article?

Both stacks and queues can be implemented using arrays or linked lists, with a stack typically tracking a single top pointer and a queue tracking front and rear indices or pointers. The stack supports push and pop operations from the top, while the queue supports enqueue at the rear and dequeue at the front; in both cases, additional helper operations like isEmpty or isFull are commonly implemented.

3. What are the main operations performed on stacks and queues?

For stacks, the key operations described are push (insert an element at the top), pop (remove the top element), peek/top (view the top element without removing it), and checks for underflow or overflow depending on capacity. For queues, the main operations are enqueue (insert at the rear), dequeue (remove from the front), peek/front (view the front element), and checks for empty or full conditions, especially in array-based circular queues.

4. Where are stacks and queues used in real-world applications?

Stacks are widely used in function call management (call stack), expression evaluation and conversion (infix to postfix), undo/redo operations, and backtracking algorithms. Queues are commonly used in task scheduling, print spooling, handling requests in web servers, breadth-first search (BFS) in graphs, and managing resources where items must be processed in arrival order.

Customer Support

Subscribe for expert insights and updates on the latest in emerging tech, directly from the thought leaders at EICTA consortium.