Premium Chapter

Arrays, stacks, queues, linked lists

Computer Science & Applications (Paper 2) (Code 87) · Unit 7 · ~20 min read · Original chapter

Free previewPremium full chapter

Free preview

Data structures are ways of organising data so that the operations you need are fast. The choice is always a trade-off: a structure that makes one operation fast usually makes another slow. Four structures carry most of this topic. An array stores elements in contiguous memory, so any element is reached in constant time by index, but inserting in the middle forces shifting. A stack is last-in, first-out (LIFO): you push on top and pop from the top, like a pile of plates. A queue is first-in, first-out (FIFO): you enqueue at the rear and dequeue from the front, like a ticket line. A linked list stores elements in nodes joined by pointers, so insertion and deletion at a known node take constant time, but reaching the k-th element requires walking from the head.

Worked example — evaluating a postfix expression with a stack. Postfix writes the operator after its operands, so no brackets are needed. Evaluate "5 2 3 + ": read 5, push it; read 2, push it; read 3, push it; read +, pop 3 and 2, compute 2 + 3 = 5, push 5; read , pop 5 and 5, compute 5 × 5 = 25. The answer is 25. The stack works because every operator finds its operands waiting on top, in the right order. Note the order for subtraction and division: for "a b −", pop b first, then a, and compute a − b, not b − a.

Checking premium access…