MEG 202 · Basic Software Engineering
Our notes
2 min readLinked 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
| Need | Good choice |
|---|---|
| fast access by position | array |
| frequent insertion/deletion in the middle | linked list |
| last-in-first-out processing | stack |
| first-come-first-served processing | queue |
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
-
Linked list
(opens en.wikipedia.org in a new tab)
Wikipedia, Linked list, CC BY-SA 4.0 · licence
-
Stack (abstract data type)
(opens en.wikipedia.org in a new tab)
Wikipedia, Stack (abstract data type), CC BY-SA 4.0 · licence
-
Queue (abstract data type)
(opens en.wikipedia.org in a new tab)
Wikipedia, Queue (abstract data type), CC BY-SA 4.0 · licence
-
NOUN CIT 216: Fundamentals of Data Structures
NOUN course book (PDF, about 3.3 MB). Arrays, records, lists, stacks, queues, sorting and searching.
(opens nou.edu.ng in a new tab)
-
OpenStax, Introduction to Computer Science (whole free textbook)
The whole book, free to read online or download as a PDF (the PDF is large: Wi-Fi).
(opens openstax.org in a new tab)
OpenStax, OpenStax, Introduction to Computer Science (whole free textbook), CC BY-NC-SA 4.0 · licence
Watch
-
Best pick 3.1 Stack in Data Structure | Introduction to Stack | Data Structures Tutorials
18 min · ≈ 81 MB · about ₦45
(opens youtube.com in a new tab)
-
Data Structures: Stacks and Queues
6 min · ≈ 27 MB · about ₦15
(opens youtube.com in a new tab)
-
Stacks And Queues In Data Structure | Data Structures And Algorithms Tutorial | Simplilearn
12 min · ≈ 54 MB · about ₦30
(opens youtube.com in a new tab)
-
Introduction to Stacks and Queues (Data Structures & Algorithms #12)
16 min · ≈ 72 MB · about ₦40
(opens youtube.com in a new tab)
-
Lecture 05: Stack and Common Data Structures / Containers
31 min · ≈ 140 MB · about ₦79
(opens youtube.com in a new tab)
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.