Skip to content
GATEiit

GATE CSE · Programming and Data Structures

Arrays, stacks, queues and linked lists

Array address calculation, stacks (expression conversion and evaluation, valid pop orders), queues (circular, deque, queue from stacks) and linked lists, with the traces and operation costs GATE asks.

Before you start

This topic comes before: Context-free grammars and push-down automata, CPU and I/O scheduling, File systems, Graphs, Intermediate code generation, Lexical analysis, parsing and syntax-directed translation, Principles of layering, Recursion, Runtime environments, Searching, sorting and hashing, Trees, binary search trees and binary heaps

Concept notes

1. Arrays and address calculation

An array stores elements of one type in consecutive memory. Element access is O(1) because its address is computed directly. Inserting or deleting in the middle is O(n) because elements must shift.

For a 1-D array A[l..u] with base address B and element size w: addr(A[i])=B+(i−l) w.

For a 2-D array A[l1..u1][l2..u2] with m=u1−l1+1 rows and n=u2−l2+1 columns:

  • Row-major (C): rows are laid out one after another, addr=B+[(i−l1) n+(j−l2)] w.
  • Column-major (Fortran): columns one after another, addr=B+[(j−l2) m+(i−l1)] w.

The idea: count how many elements come before the target, then multiply by w. For 3-D arrays apply the same idea: in row-major A[i][j][k] the number before it is (i⋅n2+j) n3+k (with zero-based indices).

In C, a[i] means *(a + i), and for int a[m][n], a[i][j] is at offset (i n+j) elements from a. Watch for lower bounds that are not 0 or 1, including negative ones.

2. Stacks

A stack is last-in first-out (LIFO). push and pop work on the top in O(1), using an array with a top index or a linked list with insertion at the head.

Where GATE uses stacks:

  • Infix to postfix: operands go straight to the output. For an operator, pop to the output every stacked operator of higher or equal precedence (for left-associative operators), then push it. ( is pushed; ) pops until the matching (. At the end pop everything. For right-associative ^, pop only strictly higher precedence.
  • Postfix evaluation: push operands; on an operator pop the top two, compute second-popped op first-popped, push the result.
  • Balanced brackets, function calls and recursion, undo, and DFS.

Valid pop orders. If 1,2,…,n are pushed in order with pops allowed in between, an output is possible exactly when it has no pattern …c…a…b… with a<b<c (a larger value popped before two smaller ones that then come out in increasing order). The number of possible outputs is the Catalan number Cn=1n+1(2nn): 5 for n=3, 14 for n=4.

An array stack of size n overflows on a push when top = n - 1 and underflows on a pop when it is empty. Two stacks can share one array by growing from opposite ends; overflow happens only when the tops meet.

3. Queues and their variants

A queue is first-in first-out (FIFO): enqueue at the rear, dequeue at the front, both O(1).

Circular queue in an array of size n: indices wrap with % n. With the common convention (front = index of the first element, rear = next free slot):

  • empty when front == rear;
  • full when (rear + 1) % n == front, so it holds at most n−1 elements (one slot is left empty to tell full from empty);
  • number of elements =(rear−front+n) mod n.

Other conventions (a count variable, or rear pointing at the last element) are also used, so read the code in the question.

Variant Rule
Deque insert and delete at both ends
Input-restricted deque insert at one end only, delete at both
Output-restricted deque insert at both ends, delete at one only
Priority queue delete the highest-priority element (usually a heap)

Queue from two stacks: push onto S1 for enqueue; for dequeue, if S2 is empty, move everything from S1 to S2, then pop S2. Each element is pushed and popped at most twice, so n operations cost O(n) in total (O(1) amortised). A stack from two queues makes either push or pop cost O(n).

4. Linked lists

A linked list stores each element in a node with a pointer to the next node. It grows without reallocation and inserts in O(1) once you have the position, but it has no random access.

Operation Singly (head only) Singly (head + tail) Doubly (head + tail)
Insert at front O(1) O(1) O(1)
Insert at end O(n) O(1) O(1)
Delete first O(1) O(1) O(1)
Delete last O(n) O(n) O(1)
Delete a node given a pointer to it O(n) O(n) O(1)
Access k-th element O(k) O(k) O(k)

Deleting the last node of a singly list needs its predecessor, which takes a walk from the head even with a tail pointer.

Standard techniques: reverse a list with three pointers (prev, curr, next) in O(n); find the middle with a slow pointer (1 step) and a fast pointer (2 steps); detect a cycle with the same two pointers (Floyd): they meet if and only if there is a cycle. In a circular list the last node points back to the first; keeping a pointer to the last node gives O(1) access to both ends.

Binary search on a linked list gains nothing: finding the middle already costs O(n).

head102030datanextNULL
A singly linked list: each node holds data and a pointer to the next node; the last pointer is NULL

Formulas to remember

1-D array address
addr(A[i])=B+(i−l) w
Row-major addressn = number of columns
addr(A[i][j])=B+[(i−l1) n+(j−l2)] w
Column-major addressm = number of rows
addr(A[i][j])=B+[(j−l2) m+(i−l1)] w
Valid stack pop orders of 1..nCatalan number: 1, 2, 5, 14, 42 for n=1 to 5
Cn=1n+1(2nn)
Circular queue sizefront = first element, rear = next free slot
count=(rear−front+n) mod n
Circular queue full
(rear+1) mod n=front

Common mistakes

  • Using the number of rows instead of columns in the row-major formula (and the reverse for column-major).
  • Forgetting a non-zero or negative lower bound when computing an array address.
  • Evaluating postfix with the operands in the wrong order: for - and / it is second-popped op first-popped.
  • Assuming a tail pointer makes deleting the last node of a singly linked list O(1).
  • Saying a circular array queue of size n holds n elements under the one-empty-slot convention; it holds n−1.
  • During infix-to-postfix conversion, popping a stacked operator of lower precedence when a higher-precedence operator arrives; pop only operators of higher or equal precedence.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Row-major and column-major addresses

numerical

An array A[1..10][1..15] of 4-byte integers starts at address 1000. Find the address of A[5][8] in row-major and in column-major order.

  1. Step 1: Find the number of rows and columns.

    Try it on paper first.

Example 2: Infix to postfix, then evaluate a postfix expression

numerical

(a) Convert A + B * (C - D) / E to postfix. (b) Evaluate the postfix expression 8 2 3 * - 4 2 / +.

  1. Step 1: Scan A + B * ( C - D and track the stack.

    Try it on paper first.

Example 3: Tracing a circular queue

numerical

A circular queue uses an array of size 6 (indices 0 to 5). Initially front = rear = 0; enqueue writes at rear then sets rear = (rear + 1) % 6; dequeue reads at front then sets front = (front + 1) % 6. Perform: enqueue 10, 20, 30, 40, 50; dequeue three times; enqueue 60, 70, 80. Give front, rear, the element at the front, and whether the queue is full.

  1. Step 1: After the five enqueues and three dequeues, where are front and rear?

    Try it on paper first.

Example 4: Which list operations are constant time?

conceptual

A singly linked list has both a head and a tail pointer. Which of these take O(1) time: insert at the front, insert at the end, delete the first node, delete the last node?

  1. Step 1: Consider the front operations.

    Try it on paper first.

Previous year questions

19 official GATE CSE questions on this topic, newest first.

Practise these 19 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.