UNIHIVE

Study Hub UNIPORT MEG 202

MEG 202 · Basic Software Engineering

Data structures: lists, stacks and queues

Linked lists, stacks (LIFO) and queues (FIFO), their operations, implementations and uses.

Our notes

2 min read

Linked lists

A linked list is a sequence of nodes; each node holds data and a pointer (link) to the next node. The list is reached through a head pointer; the last node points to NULL.

  • Insertion and deletion are easy: change a couple of pointers, no shifting.
  • Size can grow and shrink while the program runs (dynamic memory).
  • Access is sequential: to reach the 100th item you follow 99 links (slow compared with an array).
  • Variants: doubly linked (links forward and back), circular.

Inserting node N after node P: N.next ← P.next; then P.next ← N. (Doing it in the other order loses the rest of the list.)

Stacks

A stack is a list where items are added and removed at one end only (the top): last in, first out (LIFO), like a pile of plates.
Operations: push (add to top), pop (remove from top), peek/top (look without removing), isEmpty. Errors: overflow (push onto a full fixed-size stack) and underflow (pop from an empty stack).
Example: push 3, push 7, push 5, pop, push 2. Stack (bottom to top): 3, 7, 2. The pop returned 5.
Uses:

  • The call stack of function calls and recursion.
  • Undo in editors and CAD software.
  • Evaluating expressions and checking brackets.
  • Reverse Polish notation (postfix): 3 4 + 2 × means (3 + 4) × 2. Evaluate with a stack: push 3, push 4; "+" pops 4 and 3, pushes 7; push 2; "×" pops 2 and 7, pushes 14.

Queues

A queue adds at the rear and removes at the front: first in, first out (FIFO), like a queue at a bank.
Operations: enqueue, dequeue, front, isEmpty.
Example: enqueue A, B, C; dequeue (returns A); enqueue D. Queue front to rear: B, C, D.
In an array, a circular queue reuses space: the rear index wraps round to the start with (rear + 1) mod size.
Uses:

  • Print queues and job scheduling in operating systems.
  • Buffers for data streams (keyboard input, network packets, sensor readings).
  • Simulation of service systems (a production line, a filling station).
  • Breadth-first search.

A priority queue removes the highest-priority item first (urgent jobs, event-driven simulation).

Implementations

Stacks and queues can be built on arrays (simple, fixed size) or linked lists (flexible size).

Choosing a structure

NeedGood choice
fast access by positionarray
frequent insertion/deletion in the middlelinked list
last-in-first-out processingstack
first-come-first-served processingqueue

Exams usually ask

  • Distinguish LIFO and FIFO with examples.
  • Show the state of a stack or queue after a sequence of operations.
  • Evaluate a postfix expression using a stack.

Common mistake

Popping operands in the wrong order for non-commutative operators. For 8 2 −, pop 2 then 8, and compute 8 − 2 = 6, not 2 − 8.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All MEG 202 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.