Download presentation
Presentation is loading. Please wait.
Published byDamian Ward Modified over 8 years ago
1
Chapter 4 ADTs Stack and Queue
2
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.
3
4-3 The Interface ListInterface Please click on the following link Programs/C04P251.jpg to view the appropriate program. Programs/C04P251.jpg
4
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
5
4-5 Stack A structure in which elements are added and removed from only one end; a “last in, first out” (LIFO) structure
6
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.
7
4-7 The effects of push and pop operations
8
4-8 The code for the StackUnderflowException class
9
4-9 The code for the StackOverflowException class
10
Implementing ADTs “by copy” or “by reference”
11
4-11 “By Copy” Approach
12
4-12 “By Copy” Continued
13
4-13 “By Copy” Continued
14
4-14 “By Reference” Approach
15
4-15 “By Reference” Continued
16
4-16 “By Reference” Continued
17
4-17 Approaches Used for our Stacks “by reference” (not “by copy”) Throw exceptions (not “by contract”) Contains Objects
18
4-18 The Stack ADT Specification Please click on the following link Programs/C04P263.jpg to view the appropriate program. Programs/C04P263.jpg
19
4-19 A Stack-Based Application
20
4-20 Main Algorithm
21
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
22
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
23
4-23 test1.in file
24
4-24 Command: java Balanced test1.in test1.out
25
4-25 test1.out file
26
4-26
27
4-27 Stack Overflow The condition resulting from trying to push an element onto a full stack Client code example:
28
4-28
29
4-29
30
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."); }
31
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."); }
32
4-32 An ArrayList-Based Implementation Please click on the following link Programs/C04P278.jpg to view the appropriate program. Programs/C04P278.jpg
33
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
34
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
35
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
36
4-36 The Queue ADT Specification Please click on the following link Programs/C04P289.jpg to view the appropriate program. Programs/C04P289.jpg
37
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.”
38
4-38 Main Algorithm
39
4-39 Process the current string
40
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
41
4-41 Floating-Front Design Approach
42
4-42 Wrapping the queue elements around
43
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; }
44
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; }
45
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; }
46
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); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.