4.2.6

Stacks

Test yourself

Stacks

A stack is a data structure where data items are always added and removed from the top. It is often referred to as a ‘last in, first out’ data structure.

Illustrative background for StacksIllustrative background for Stacks ?? "content

Stacks

  • A stack is a data structure where data items are always added and removed from the top. It is often referred to as a ‘last in, first out’ data structure.
  • Stacks can be implemented using other data structures, such as arrays or linked lists. In either case, an extra variable is required to identify the top of the stack.
Illustrative background for Stack operationsIllustrative background for Stack operations ?? "content

Stack operations

  • Several operations can be carried out on stacks:
    • push() - adds an item to the top of a stack.
    • pop() - removes and displays the item at the top of a stack
    • peek() - displays the item at the top of a stack without removing it
    • isEmpty() - checks to see if the stack is empty
    • isFull() - checks to see if the stack is full
Illustrative background for UsesIllustrative background for Uses ?? "content

Uses

  • The most common use of stacks is to reverse the order of a set of data items, as they are ‘popped’ in the opposite order that they were ‘pushed’ to a stack.
  • They are also used by processors to handle interrupts.
  • Stacks can be ‘static’ (fixed in size) or ‘dynamic’ (able to grow in size as necessary)
Illustrative background for ErrorsIllustrative background for Errors ?? "content

Errors

  • Stack overflow errors can occur in static stacks when attempting to push an item onto a stack that is full.
  • Similarly, when trying to pop from an empty stack a stack underflow error occurs.

Jump to other topics

1Components of a Computer

2Software & Software Development

3Exchanging Data

4Data Types, Data Structures & Algorithms

5Legal, Moral, Cultural & Ethical Issues

6Elements of Computational Thinking

6.1Thinking Abstractly

6.2Thinking Ahead

6.3Thinking Procedurally

6.4Thinking Logically

6.5Thinking Concurrently

7Problem Solving & Programming

8Algorithms

Unlock your full potential with Seneca Premium

  • Unlimited access to 10,000+ open-ended exam questions

  • Mini-mock exams based on your study history

  • Unlock 800+ premium courses & e-books

Get started with Seneca Premium