Notes on Implementing Stacks and Queues

COMP5017: Data Structures and Algorithms

Implementing Stacks and Queues

In this lecture by David Lightfoot at Oxford Brookes University, the key concepts related to implementing stacks and queues in Java are covered extensively, demonstrating how data structures can be implemented using both Array Lists and Arrays.

Implementing a Stack Using an Array List
  • Definition: A stack is a Last In First Out (LIFO) data structure where elements are added (pushed) and removed (popped) from the same end.
  • Java Implementation:
  public class StringStack {
      private ArrayList<String> list = new ArrayList<String>();
      public void push(String s) {list.add(s);}
      public String pop() {assert list.size() != 0; return list.remove(list.size()-1);}
      public String peek() {assert list.size() != 0; return list.get(list.size()-1);}
      public boolean isEmpty() {return list.isEmpty();}
  }
  • Capacity: The stack uses an ArrayList, meaning it is not bounded, and can grow as needed.
  • Time Complexity:
    • push: Average case O(1); however, it can occasionally be O(n) if the array needs to grow (i.e., when it's full and has to copy all elements to a new larger array).
    • pop: O(1); direct access to the last element.
    • peek: O(1); direct access to the last element.
Implementing a Queue Using an Array List
  • Definition: A queue is a First In First Out (FIFO) data structure where elements are added (enqueued) to the back and removed (dequeued) from the front.
  • Java Implementation:
  public class StringQueue {
      private ArrayList<String> list = new ArrayList<String>();
      public void add(String s) {list.add(s);}
      public String remove() {assert list.size() != 0; return list.remove(0);}
      public String peek() {assert list.size() != 0; return list.get(0);}
      public boolean isEmpty() {return list.isEmpty();}
  }
  • Capacity: The queue also uses an ArrayList, which means it is also not bounded.
  • Time Complexity:
    • add: O(1); except occasionally when the array needs to grow, which is O(n).
    • remove: O(n); this is required since all elements after the first need to be shifted forward in the array.
    • peek: O(1); direct access to the first element.
Implementing a Stack Using an Array
  • Java Implementation:
  public class StringStack {
      private final int capacity = 1000; // arbitrary maximum capacity
      private int size = 0; // size must be within bounds 0 <= size <= capacity
      private String[] list = new String[capacity];
      public void push(String s) {assert size != capacity; list[size] = s; size++;}
      public String pop() {assert size != 0; size--; return list[size];}
      public String peek() {assert size != 0; return list[size-1];}
      public boolean isEmpty() {return size == 0;}
  }
  • Capacity: This stack uses a fixed-size array (bounded).
  • Time Complexity:
    • push: O(1); direct element access.
    • pop: O(1); direct access to the element.
    • peek: O(1); direct access to the last element.
Visual Representation: Stack Operations
  • Push Example: Adding elements increases the size and positions in the array.
  • Pop Example: Example showing actual removal of an element at the current size, demonstrating size reduction.
Implementing a Queue Using an Array – Version 1
  • Java Implementation:
  public class StringQueue {
      private final int capacity = 1000; // maximum capacity
      private int size = 0;
      private int front = 0; // index for removal
      private int back = 0; // index for addition
      private String[] list = new String[capacity];
      public void add(String s) { assert back != capacity; list[back] = s; size++; back = back + 1; }
      public String remove() { assert size != 0; String result = list[front]; size--; front = front + 1; return result; }
      public String peek() { assert size != 0; return list[front]; }
      public boolean isEmpty() {return size == 0;}
  }
  • Queue Capacity: This implementation also has a fixed capacity.
  • Time Complexity: Adds and peeks are O(1); removes are also O(1), but this is valid under certain conditions.
Problem with Queue Implementation
  • Challenge: The back index can advance until it reaches the end of the array, causing potential overflow, even if the front moves forward with removes.
  • Circular Queue Solution: Use wrapping and modular arithmetic to keep pointers within bounds by applying modulo on the indices:
    • Adjust back and front using back = (back + 1) % capacity and front = (front + 1) % capacity.
    • Resulting in unlimited use of the array space as it efficiently wraps around.
Implementing a Queue Using an Array – Version 2
  • Java Implementation:
  public class StringQueue {
      // Same declarations as before
      public void add(String s) { assert size != capacity; list[back] = s; size++; back = (back + 1) % capacity; }
      public String remove() { assert size != 0; String result = list[front]; size--; front = (front + 1) % capacity; return result; }
      public String peek() { assert size != 0; return list[front]; }
      public boolean isEmpty() {return size == 0;}
  }
  • Time Complexity: Similar to previous implementations, with all operations remaining O(1).
Summary
  • The balance between convenience and efficiency is crucial, especially for large data structures.
  • Efficient implementations of stacks and queues can be achieved with arrays (though they are bounded), or alternatively, with linked lists for unbounded structures, which also provides O(1) access if a pointer to the end of the list is maintained alongside the head.