Chapter 4 ADTs Stack and Queue
4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic unit. Therefore— We formalize the specification of our ADTs by using a Java interface. Benefits: –We can formally check the syntax of our specification. –We can formally verify the interface “contract.” –We can assume a consistent interface among alternate implementations of the ADT.
4-3 The Interface ListInterface Please click on the following link Programs/C04P251.jpg to view the appropriate program. Programs/C04P251.jpg
4-4 UML class diagram for list approach Please click on the following link Programs/C04P254.jpg to view the appropriate program. Programs/C04P254.jpg
4-5 Stack A structure in which elements are added and removed from only one end; a “last in, first out” (LIFO) structure
4-6 Operations on Stacks 1.push adds an element to the top of a stack. 2.pop removes the top element off the stack. 3.top returns the top element of a stack. 4.isEmpty returns a boolean value indicating whether or not the stack is empty.
4-7 The effects of push and pop operations
4-8 The code for the StackUnderflowException class
4-9 The code for the StackOverflowException class
Implementing ADTs “by copy” or “by reference”
4-11 “By Copy” Approach
4-12 “By Copy” Continued
4-13 “By Copy” Continued
4-14 “By Reference” Approach
4-15 “By Reference” Continued
4-16 “By Reference” Continued
4-17 Approaches Used for our Stacks “by reference” (not “by copy”) Throw exceptions (not “by contract”) Contains Objects
4-18 The Stack ADT Specification Please click on the following link Programs/C04P263.jpg to view the appropriate program. Programs/C04P263.jpg
4-19 A Stack-Based Application
4-20 Main Algorithm
4-21 The “Process the current character” command Please click on the following link Programs/C04P267.jpg to view the appropriate program. Programs/C04P267.jpg
4-22 The Code for the Program Balanced Please click on the following link Programs/C04P268.jpg to view the appropriate program. Programs/C04P268.jpg
4-23 test1.in file
4-24 Command: java Balanced test1.in test1.out
4-25 test1.out file
4-26
4-27 Stack Overflow The condition resulting from trying to push an element onto a full stack Client code example:
4-28
4-29
4-30 PUSH public void push(Object item) // Adds an element to the top of this stack { if (!isFull()) { topIndex++; stack[topIndex] = item; } else throw new StackOverflowException("Push attempted on a full stack."); }
4-31 POP public void pop() // Removes an element from the top of this stack { if (!isEmpty()) { stack[topIndex] = null; topIndex--; } else throw new StackUnderflowException("Pop attempted on an empty stack."); }
4-32 An ArrayList-Based Implementation Please click on the following link Programs/C04P278.jpg to view the appropriate program. Programs/C04P278.jpg
4-33 Queue A structure in which elements are added to the rear and removed from the front; a “first in, first out” (FIFO) structure
4-34 Operations on Queues enqueue Adds an element to the rear dequeue Removes and returns the front element isEmpty Returns true if the queue is empty and false otherwise
4-35 Approaches Used for our Queues: By “reference” (not “by copy”) Contains Objects By “contract” (no exceptions thrown) Uses classic “remove and return” dequeue operation
4-36 The Queue ADT Specification Please click on the following link Programs/C04P289.jpg to view the appropriate program. Programs/C04P289.jpg
4-37 A Queue-Based Application— Identifying Palindromes “A man, a plan, a canal—Panama!” “Able I was ere, I saw Elba.” “Won ton? Not now!” “Madam, I’m Adam.” “Eve.”
4-38 Main Algorithm
4-39 Process the current string
4-40 The code for the program Palindrome Please click on the following link Programs/C04P293.jpg to view the appropriate program. Programs/C04P293.jpg
4-41 Floating-Front Design Approach
4-42 Wrapping the queue elements around
4-43 Declarations and Constructors public class ArrayQueue implements QueueInterface { private Object[] queue; // Array that holds queue elements private int capacity; // Size of the array (capacity of the queue) private int numItems = 0; // Number of items on the queue private int front = 0; // Index of front of queue private int rear = -1; // Index of rear of queue // Constructors public ArrayQueue() { queue = new Object[100]; capacity = 100; } public ArrayQueue(int maxSize) { queue = new Object[maxSize]; capacity = maxSize; }
4-44 Enqueue public void enqueue(Object item) // Adds an element to the front of this queue { rear = (rear + 1) % capacity; queue[rear] = item; numItems = numItems + 1; }
4-45 Dequeue public Object dequeue() // Removes an element from the rear of this queue { Object toReturn = queue[front]; queue[front] = null; front = (front + 1) % capacity; numItems = numItems - 1; return toReturn; }
4-46 public boolean isEmpty() // Checks if this queue is empty { return (numItems == 0); } public boolean isFull() // Checks if this queue is full { return (numItems == capacity); }