Processing math: 100%
Close
Close Window

Show Source |    | About   «  30.1. Data Structures and Algorithm Analysis Introduction   ::   Contents   ::   30.3. Program Management  »

30.2. Stacks and Queues

30.2.1. Container Class Design Issues

  • Storing a record vs. Storing a reference to a record
  • Homogeneity: Allow different record types? Check and block?
  • Deletion: What happens to the record?

30.2.2. Stacks

LIFO: Last In, First Out.

Restricted form of list: Insert and remove only at front of list.

Notation:

  • Insert: PUSH
  • Remove: POP
  • The accessible element is called TOP.

30.2.3. Stack ADT

public interface Stack { // Stack class ADT
  // Reinitialize the stack.
  public void clear();

  // Push "it" onto the top of the stack
  public boolean push(Object it);

  // Remove and return the element at the top of the stack
  public Object pop();

  // Return a copy of the top element
  public Object topValue();

  // Return the number of elements in the stack
  public int length();
  
  // Return true if the stack is empty 
  public boolean isEmpty();
}

30.2.4. Array-Based Stack (1)

Issues:

  • Which end is the top?
  • Where does “top” point to?
  • What are the costs of the operations?

30.2.5. Array-Based Stack (2)

class AStack implements Stack {
  private Object stackArray[];    // Array holding stack
  private static final int DEFAULT_SIZE = 10;
  private int maxSize;            // Maximum size of stack
  private int top;                // First free position at top

  // Constructors
  AStack(int size) {
    maxSize = size;
    top = 0;
    stackArray = new Object[size]; // Create stackArray
  }
  AStack() { this(DEFAULT_SIZE); }

30.2.6. Linked Stack

// Linked stack implementation
class LStack implements Stack {
  private Link top;               // Pointer to first element
  private int size;               // Number of elements

  // Constructors
  LStack() { top = null; size = 0; }
  LStack(int size) { top = null; size = 0; }

What are the costs of the operations?

How do space requirements compare to the array-based stack implementation?

30.2.7. Queues

FIFO: First in, First Out

Restricted form of list: Insert at one end, remove from the other.

Notation:

  • Insert: Enqueue
  • Delete: Dequeue
  • First element: Front
  • Last element: Rear

30.2.8. Queue Implementation (1)

1 / 5 Settings
<<<>>>

Assume that there are n elements in the queue. By analogy to the array-based list implementation, we could require that all elements of the queue be stored in the first n positions of the array.

front
rear
  1. 4
listSize
  1. 120
  2. 451
  3. 52
  4. 813
  5. 4
  6. 5
  7. 6
  8. 7
Proficient Saving... Error Saving
Server Error
Resubmit

30.2.9. Queue Implementation (2)

1 / 9 Settings
<<<>>>

A more efficient implementation can be obtained by relaxing the requirement that all elements of the queue must be in the first n positions of the array. We still require that the queue be stored be in contiguous array positions, but the contents of the queue will be permitted to drift within the array.

Proficient Saving... Error Saving
Server Error
Resubmit

30.2.10. Queue Implementation (3)

1 / 7 Settings
<<<>>>

This implementation raises a new problem. When elements are removed from the queue, the front index increases.

  1. 0
  2. 1
  3. 2
  4. 173
  5. 34
  6. 305
  7. 46
  8. 7
  9. 8
  10. 9
  1. 3
front
  1. 6
rear
  1. 4
listSize
Proficient Saving... Error Saving
Server Error
Resubmit

30.2.11. Circular Queue (1)

1 / 5 Settings
<<<>>>

The "drifting queue" problem can be solved by pretending that the array is circular and so allow the queue to continue directly from the highest-numbered position in the array to the lowest-numbered position.

Created with Raphaël 2.1.2
0
1
2
3
4
5
6
7
8
9
10
11
Proficient Saving... Error Saving
Server Error
Resubmit

30.2.12. Circular Queue (2)

1 / 8 Settings
<<<>>>

There remains one more serious, though subtle, problem to the array-based queue implementation. How can we recognize when the queue is empty or full?

Created with Raphaël 2.1.2
0
1
2
3
4
5
6
7
8
9
10
11
Proficient Saving... Error Saving
Server Error
Resubmit

   «  30.1. Data Structures and Algorithm Analysis Introduction   ::   Contents   ::   30.3. Program Management  »

nsf
Close Window