You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

3.3 Implementation of Stacks and Queues using Arrays and Linked Lists

3 min readjuly 19, 2024

Stacks and queues are essential data structures with two main implementations: array-based and linked list-based. Each approach has its pros and cons, affecting performance and memory usage in different scenarios.

Understanding these implementations is crucial for choosing the right structure for your needs. We'll compare array and linked list versions of stacks and queues, examining their advantages, disadvantages, and performance characteristics.

Array-based and Linked List-based Implementations of Stacks and Queues

Stacks with arrays vs linked lists

Top images from around the web for Stacks with arrays vs linked lists
Top images from around the web for Stacks with arrays vs linked lists
  • Array-based implementation
    • Utilizes a fixed- array to store elements
    • Advantages
      • Straightforward implementation
      • Constant time (O(1)O(1)) access to elements by index
      • Memory-efficient as no extra space needed for pointers
    • Disadvantages
      • Fixed size may lead to if array is full
      • Inefficient memory use if stack size is unknown in advance
    • Utilizes a singly linked list to store stack elements
    • Advantages
      • Dynamic size allows stack to grow or shrink as needed
      • No stack overflow issues due to dynamic memory allocation
      • Efficient memory use as memory allocated only when required
    • Disadvantages
      • Extra memory overhead for storing pointers
      • Slightly slower element access compared to array-based

Queues with arrays vs linked lists

    • Utilizes a fixed-size array to store elements
    • Implements a for optimized space utilization
    • Advantages
      • Straightforward implementation
      • Constant time (O(1)O(1)) access to elements by index
      • Memory-efficient as no extra space needed for pointers
    • Disadvantages
      • Fixed size may lead to if array is full
      • Inefficient memory use if queue size is unknown in advance
    • Utilizes a singly linked list to store queue elements
    • Maintains pointers to the front and rear of the queue
    • Advantages
      • Dynamic size allows queue to grow or shrink as needed
      • No queue overflow issues due to dynamic memory allocation
      • Efficient memory use as memory allocated only when required
    • Disadvantages
      • Extra memory overhead for storing pointers
      • Slightly slower element access compared to array-based

Performance of stack and queue implementations

  • Time complexity
    • Array-based stack and queue
      • / operation: O(1)O(1)
      • / operation: O(1)O(1)
    • Linked list-based stack and queue
      • Push/Enqueue operation: O(1)O(1)
      • Pop/Dequeue operation: O(1)O(1)
  • Space complexity
    • Array-based stack and queue
      • Fixed size regardless of number of elements stored
      • May lead to inefficient memory use if size is unknown
    • Linked list-based stack and queue
      • Grows or shrinks dynamically based on number of elements
      • Requires extra memory for storing pointers

Error handling in stacks and queues

  • Empty stack or queue
    • Check if stack or queue is empty before pop or dequeue
    • Throw exception or return error message if not possible
  • Full stack or queue (array-based)
    • Check if stack or queue is full before push or enqueue
    • Throw exception or return error message if not possible
  • Null elements
    • Handle null elements based on application requirements
    • Allow null elements or throw exception if not permitted
  • Concurrent access
    • Implement synchronization (locks, semaphores) for thread safety
    • Prevent race conditions and ensure data consistency with multiple threads
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary