Unit 1 of 4 · M.Sc IT Sem 2

Unit 1: Stacks, queues and lists

Data Structures notes · PTU syllabus (PGCA1913)

4 min read11 topics10 exam questions
On this page
  1. Unit summary
  2. Stacks: contiguous implementation
  3. Stack operations
  4. Polish notations and conversion
  5. Infix to prefix conversion
  6. Evaluating postfix and prefix expressions
  7. Linear queues
  8. Circular queues
  9. Linked implementation of stacks and queues
  10. Linked lists and comparison with arrays
  11. Singly linked list operations
  12. Doubly and circular linked lists
  13. Key terms
  14. Quick revision
  15. 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
ComparisonInfix, prefix and postfix
Example
Evaluated with

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

1

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.
2

Topic 2

Stack operations

ProcessPUSH(ITEM) using an array
  1. 1Check overflow

    If TOP = MAX − 1, print "Overflow" and stop

  2. 2Increase TOP

    TOP = TOP + 1

  3. 3Insert

    STACK[TOP] = ITEM

ProcessPOP() using an array
  1. 1Check underflow

    If TOP = −1, print "Underflow" and stop

  2. 2Take the item

    ITEM = STACK[TOP]

  3. 3Decrease TOP

    TOP = TOP − 1

Other operations: PEEK (read the top without removing it), isEmpty and isFull. All are O(1).

3

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.

ProcessInfix to postfix using a stack
  1. 1

    Scan left to right

  2. 2

    Operand

    Send to output

  3. 3

    Left bracket

    Push

  4. 4

    Operator

    Pop operators of higher or equal precedence to output, then push it

  5. 5

    Right bracket

    Pop to output until the left bracket

  6. 6

    End

    Pop all remaining operators

Example

A + B × C → A B C × +, because × has higher precedence than +. (A + B) × C → A B + C ×.

4

Topic 4

Infix to prefix conversion

ProcessInfix to prefix
  1. 1Reverse the infix expression, swapping ( and )
  2. 2Convert the result to postfix (for equal precedence, pop only operators of higher precedence)
  3. 3Reverse the postfix to get the prefix

Example

(A + B) × C − D: prefix = − × + A B C D; postfix = A B + C × D −.

5

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.

SymbolActionStack
5Push5
3Push5, 3
+Pop 3 and 5, push 88
2Push8, 2
×Pop 2 and 8, push 1616

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.
6

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.
ComparisonStack vs queue
Stack
Queue

Principle

LIFO

FIFO

Insertion

At TOP (PUSH)

At REAR (enqueue)

Deletion

At TOP (POP)

At FRONT (dequeue)

Example

Undo history

Printer jobs

7

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.

8

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.
9

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.

ComparisonArray vs linked list
Array
Linked list

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

10

Topic 10

Singly linked list operations

  • Traversal: start with ptr = start; while ptr != NULL, process ptr->data and move ptr = ptr->next.
  • Search: traverse and compare each node's data with the item: O(n).
ProcessInserting a node at the beginning
  1. 1Create a new node

    newnode = malloc(...)

  2. 2Store data

    newnode->data = item

  3. 3Link to old first node

    newnode->next = start

  4. 4Move START

    start = newnode

  • Insert at end: traverse to the last node and set last->next = newnode, with newnode->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; then free(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.

11

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.
ComparisonTypes of linked list
Pointers per node
Main advantage

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

  1. Q1.Convert A × (B + C) − D to postfix and prefix.
  2. Q2.Evaluate the postfix 6 2 3 + − 3 8 2 ÷ + ×.
  3. Q3.What is the condition for a full circular queue?
  4. Q4.Why is a linked stack free from overflow?
  5. Q5.State two advantages of a doubly linked list.
  6. Q6.How do you detect the end of a circular list?

Long-answer questions

  1. Q1.Explain stack operations with array and linked implementations.
  2. Q2.Convert infix expressions to postfix and prefix and evaluate them.
  3. Q3.Explain linear and circular queues with algorithms.
  4. 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.

WhatsApp us