Skip to main content
Informatik.uz
Chapter 2. Data structures

Stack and queue

A stack works on the “last in, first out” principle, a queue on “first in, first out”. We study their operations, Python implementation and uses.

Topic 5 of 112 min readUpdated October 27, 2026
All topics (5 of 11)

A data structure is a way of storing data and performing operations on it. One of the simplest is the stack, which works on the “last in, first out” principle (LIFO). It can be compared to a pile of plates: you put a new plate on top and take only the top one. The main operations are push (add to the top), pop (remove the top element) and peek (look without removing).

A queue works on the “first in, first out” principle (FIFO), just like a line in a shop: a newcomer joins at the end, and service starts with the person at the front. The main operations are enqueue (add to the end) and dequeue (take from the front).

In Python a stack can be built from an ordinary list: s.append(x) adds an element on top, and s.pop() returns the top element and removes it. For a queue, deque from the collections module is convenient: q.append(x) adds to the end and q.popleft() takes from the front. It is better not to use a.pop(0) on an ordinary list for a queue, because it shifts all the remaining elements and is slow on large lists.

Stacks are everywhere: the browser’s “Back” button, undo in a text editor (Ctrl+Z), the order of function calls (including recursion) and checking that brackets in an expression are balanced. Queues are used for documents sent to a printer, network packets and the order in which an operating system runs processes.

Key takeaways

  • A stack works as LIFO: the last element added is the first one taken out.
  • A queue works as FIFO: the first element added is the first one taken out.
  • Stack operations are push, pop and peek; queue operations are enqueue and dequeue.
  • In Python a stack is conveniently built on a list and a queue on collections.deque.
  • Stacks are used for undo and bracket checking, queues for running tasks in order.

Practice tasks

  1. Push the elements 1, 2, 3 onto an empty stack one after another, then perform pop twice; write down which elements come out and what remains in the stack. Repeat this with an empty queue (enqueue, dequeue) and compare the results.
  2. Write a Python program that uses a stack to check whether brackets are placed correctly: (()[]) is valid, ([)] is invalid.
  3. Using deque, write a “print queue” program: the user enters document names, and the program “prints” (displays) them in the order they were entered.

Review questions

Test yourself: think first, then reveal the answer.

  1. 1What is the main difference between a stack and a queue?
    In a stack the last element added comes out first (LIFO), while in a queue the first element added comes out first (FIFO).
  2. 2Which structure does the browser’s “Back” button resemble, and why?
    A stack: you return first to the page opened last.
  3. 3Why is `deque` usually used for a queue instead of an ordinary list?
    In a list `pop(0)` shifts all elements and is slow, while `deque` is fast at both ends.

0 of 11 topics completed

Sign in to mark
Informatika 11-sinf darsligi muqovasi
In the textbookInformatics — Grade 11