Unit 1: Stacks, queues and lists
Data Structures notes · PTU syllabus (PGCA1913)
On this page
- Unit summary
- Stacks: contiguous implementation
- Stack operations
- Polish notations and conversion
- Infix to prefix conversion
- Evaluating postfix and prefix expressions
- Linear queues
- Circular queues
- Linked implementation of stacks and queues
- Linked lists and comparison with arrays
- Singly linked list operations
- Doubly and circular linked lists
- Key terms
- Quick revision
- Important questions
Unit summary
Stacks, queues and linked lists are the workhorses of programming. This unit covers the contiguous implementation of stacks, Polish notations and their conversion and evaluation, linear and circular queues, linked implementations of stacks and queues, and singly, doubly and circular linked lists with their operations.
After this unit you can
- Implement stacks with arrays and linked lists
- Convert and evaluate infix, prefix and postfix expressions
- Implement linear and circular queues with arrays and linked lists
- Perform operations on singly, doubly and circular linked lists
PTU syllabus topics
- Contiguous implementation of stacks
- polish notations (infix/prefix/postfix conversion and evaluation)
- linear and circular queue implementation
- linked implementation of stacks and queues
- singly/doubly/circular linked lists and their operations
Infix
A + B * C
Precedence rules and brackets
Prefix (Polish)
+ A * B C
Scan right to left with a stack
Postfix (reverse Polish)
A B C * +
Scan left to right with a stack
Topic 1
Stacks: contiguous implementation
A stack is a linear data structure that follows LIFO (Last In, First Out): the last element inserted is the first removed. Insertion is PUSH and deletion is POP, both at the TOP.
- Array representation: an array STACK[MAX] and a variable TOP (−1 when empty).
- Linked representation: each push adds a node at the start of a linked list; the list's head is TOP. No overflow until memory runs out.
Topic 2
Stack operations
- 1Check overflow
If TOP = MAX − 1, print "Overflow" and stop
- 2Increase TOP
TOP = TOP + 1
- 3Insert
STACK[TOP] = ITEM
- 1Check underflow
If TOP = −1, print "Underflow" and stop
- 2Take the item
ITEM = STACK[TOP]
- 3Decrease TOP
TOP = TOP − 1
Other operations: PEEK (read the top without removing it), isEmpty and isFull. All are O(1).
Topic 3
Polish notations and conversion
- Infix: operator between operands — A + B
- Prefix (Polish): operator before operands — + A B
- Postfix (Reverse Polish): operator after operands — A B +
Postfix needs no brackets and is easy for a computer to evaluate with a stack.
- 1
Scan left to right
- 2
Operand
Send to output
- 3
Left bracket
Push
- 4
Operator
Pop operators of higher or equal precedence to output, then push it
- 5
Right bracket
Pop to output until the left bracket
- 6
End
Pop all remaining operators
Example
A + B × C → A B C × +, because × has higher precedence than +. (A + B) × C → A B + C ×.
Topic 4
Infix to prefix conversion
- 1Reverse the infix expression, swapping ( and )
- 2Convert the result to postfix (for equal precedence, pop only operators of higher precedence)
- 3Reverse the postfix to get the prefix
Example
(A + B) × C − D: prefix = − × + A B C D; postfix = A B + C × D −.
Topic 5
Evaluating postfix and prefix expressions
Scan left to right: push operands; when an operator appears, pop two operands (the first popped is the right operand), apply the operator and push the result. The final value on the stack is the answer.
| Symbol | Action | Stack |
|---|---|---|
| 5 | Push | 5 |
| 3 | Push | 5, 3 |
| + | Pop 3 and 5, push 8 | 8 |
| 2 | Push | 8, 2 |
| × | Pop 2 and 8, push 16 | 16 |
So 5 3 + 2 × = 16.
- Prefix evaluation: scan right to left; push operands; on an operator pop two operands (first popped is the left operand), apply and push. − × + 2 3 4 5 → (2 + 3) × 4 − 5 = 15.
Topic 6
Linear queues
A queue is a linear data structure that follows FIFO (First In, First Out). Insertion (enqueue) happens at the REAR; deletion (dequeue) happens at the FRONT.
- Array representation: QUEUE[MAX] with FRONT and REAR (both −1 when empty).
- Linked representation: a linked list with pointers to the front and rear nodes; insert at the rear, delete from the front.
Principle
LIFO
FIFO
Insertion
At TOP (PUSH)
At REAR (enqueue)
Deletion
At TOP (POP)
At FRONT (dequeue)
Example
Undo history
Printer jobs
Topic 7
Circular queues
A circular queue connects the last position back to the first, so freed spaces are reused. Positions move with modulo arithmetic:
- REAR = (REAR + 1) mod MAX, FRONT = (FRONT + 1) mod MAX
- Full when (REAR + 1) mod MAX = FRONT; empty when FRONT = −1.
Example
With MAX = 5, after inserting 5 items and deleting 2, a simple queue says it is full, but a circular queue can store 2 more by wrapping REAR to positions 0 and 1.
Topic 8
Linked implementation of stacks and queues
cstruct node { int data; struct node *next; };
/* Linked stack: push and pop at the head (top) */
struct node *top = NULL;
void push(int x) {
struct node *n = malloc(sizeof *n);
n->data = x; n->next = top; top = n;
}
int pop(void) {
if (top == NULL) { printf("Underflow\n"); return -1; }
struct node *t = top; int x = t->data;
top = top->next; free(t); return x;
}
/* Linked queue: insert at rear, delete at front */
struct node *front = NULL, *rear = NULL;
void enqueue(int x) {
struct node *n = malloc(sizeof *n);
n->data = x; n->next = NULL;
if (rear == NULL) front = rear = n;
else { rear->next = n; rear = n; }
}
int dequeue(void) {
if (front == NULL) { printf("Underflow\n"); return -1; }
struct node *t = front; int x = t->data;
front = front->next; if (front == NULL) rear = NULL;
free(t); return x;
}- Linked versions never overflow (until memory runs out) and grow and shrink as needed, at the cost of a pointer per element.
Topic 9
Linked lists and comparison with arrays
A linked list is a linear collection of nodes, where each node contains data and a pointer (link) to the next node. The first node is reached through a pointer called START (or HEAD); the last node's link is NULL.
Memory
Consecutive locations
Scattered; nodes linked by pointers
Size
Fixed at declaration
Dynamic: grows and shrinks
Access
Direct by index: O(1)
Sequential from START: O(n)
Insertion/deletion
Slow: elements must be shifted
Fast: only pointers change
Extra memory
None
One pointer per node
Topic 10
Singly linked list operations
- Traversal: start with
ptr = start; whileptr != NULL, processptr->dataand moveptr = ptr->next. - Search: traverse and compare each node's data with the item: O(n).
- 1Create a new node
newnode = malloc(...)
- 2Store data
newnode->data = item
- 3Link to old first node
newnode->next = start
- 4Move START
start = newnode
- Insert at end: traverse to the last node and set
last->next = newnode, withnewnode->next = NULL. - Insert after a given node:
newnode->next = loc->next; loc->next = newnode; - Delete: make the previous node skip the deleted one:
prev->next = loc->next;thenfree(loc). Deleting the first node changes START.
Exam tip
Always handle the special cases: empty list (underflow), inserting into an empty list and deleting the first node.
Topic 11
Doubly and circular linked lists
- Doubly linked list: each node has two pointers, prev and next, so it can be traversed in both directions and a node can be deleted without searching for its predecessor. It uses more memory.
- Circular linked list: the last node points back to the first instead of NULL, so the list can be traversed from any node. It is useful for round-robin scheduling.
Singly
One (next)
Simple, least memory
Doubly
Two (prev, next)
Two-way traversal, easy deletion
Circular
One, last points to first
Continuous looping from any node
Key terms
- Stack
- LIFO list with insertion and deletion at the top
- Prefix notation
- Operators written before operands (Polish notation)
- Circular queue
- Queue whose last position wraps to the first
- Linked stack
- Stack implemented with a linked list
- Doubly linked list
- List with next and previous pointers
Quick revision
- Array stack: top, push, pop, peek; overflow and underflow.
- Infix, prefix, postfix; conversion with a stack; evaluation.
- Linear queue drawbacks; circular queue formulas (rear + 1) mod n.
- Linked stacks and queues.
- Singly, doubly, circular lists: traverse, insert, delete, search, reverse.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.Convert A × (B + C) − D to postfix and prefix.
- Q2.Evaluate the postfix 6 2 3 + − 3 8 2 ÷ + ×.
- Q3.What is the condition for a full circular queue?
- Q4.Why is a linked stack free from overflow?
- Q5.State two advantages of a doubly linked list.
- Q6.How do you detect the end of a circular list?
Long-answer questions
- Q1.Explain stack operations with array and linked implementations.
- Q2.Convert infix expressions to postfix and prefix and evaluate them.
- Q3.Explain linear and circular queues with algorithms.
- Q4.Explain operations on singly, doubly and circular linked lists.
Stuck on this unit?
Message SBS on WhatsApp for help with Data Structures, or to ask about studying M.Sc IT at Synetic.
