Stacks and Queues Based on D S Malik
Stacks and Queues Based on D. S. Malik, Java Programming: Program Design Including Data Structures 1
Stacks s Lists of homogeneous elements s Addition and deletion of elements occur only at one end, called the top of the stack s The middle elements of the stack are inaccessible s Computers use stacks to implement method calls s Real life examples? 2
Stacks Figure 17 -1 Various types of stacks 3
Stacks s Stacks are also called Last In First Out (LIFO) data structures s Operations performed on stacks s Push: adds an element to the stack s Pop: removes an element from the stack s Peek: looks at the top element of the stack 4
Stacks Figure 17 -3 Stack operations 5
Stacks Figure 17 -4 UML diagram of the interface Stack. ADT 6
Stack. Exception Class s Adding an element to a full stack and removing an element from an empty stack would generate errors or exceptions s Stack overflow exception s Stack underflow exception s Classes that handle these exceptions s Stack. Exception extends Run. Time. Exception s Stack. Overflow. Exception extends Stack. Exception s Stack. Underflow. Exception extends Stack. Exception 7
Implementation of Stacks as Arrays s The array implementing a stack is an array of reference variables s Each element of the stack can be assigned to an array slot s The top of the stack is the index of the last element added to the stack s To keep track of the top position, declare a variable called stack. Top 8
Implementation of Stacks as Arrays Figure 17 -5 UML class diagram of the class Stack. Class 9
Implementation of Stacks as Arrays Figure 17 -6 Example of a stack 10
Stacks (Methods) s Default constructor public Stack. Class() { max. Stack. Size = 100; stack. Top = 0; //set stack. Top to 0 //create the array list = (T[]) new Object[max. Stack. Size]; } s Method initialize. Stack public void initialize. Stack(){ for (int i = 0; i < stack. Top; i++) list[i] = null; stack. Top = 0; } 11
Stacks (Methods) s Method is. Empty. Stack public boolean is. Empty. Stack(){ return (stack. Top == 0); } s Method is. Full. Stack public boolean is. Full. Stack() { return (stack. Top == max. Stack. Size); } 12
Stacks (Methods) s Method push public void push(T new. Item) throws Stack. Overflow. Exception { if (is. Full. Stack()) throw new Stack. Overflow. Exception(); list[stack. Top] = new. Item; //add new. Item stack. Top++; //increment stack. Top } s Method pop public void pop() throws Stack. Underflow. Exception { if (is. Empty. Stack()) throw new Stack. Underflow. Exception(); stack. Top--; //decrement stack. Top list[stack. Top] = null; } 13
Stacks (Methods) s Method peek public T peek() throws Stack. Underflow. Exception { if (is. Empty. Stack()) throw new Stack. Underflow. Exception(); return (T) list[stack. Top - 1]; } 14
Linked List Implementation of Stacks s Arrays have fixed sizes s Only a fixed number of elements can be pushed onto the stack s Dynamically allocate memory using reference variables s Implement a stack dynamically s Similar to the array representation, stack. Top is used to locate the top element s stack. Top is now a reference variable 15
Linked Implementation of Stacks (continued) Figure 17 -13 Nonempty linked stack 16
Stacks (Methods) s Default constructor public Linked. Stack. Class() { stack. Top = null; } s Method initialize. Stack public void initialize. Stack() { stack. Top = null; } 17
Stacks (Methods) s Method is. Empty. Stack public boolean is. Empty. Stack() { return (stack. Top == null); } s Method is. Full. Stack public boolean is. Full. Stack() { return false; } 18
Stacks (Methods) s Method push public void push(T new. Element) { Stack. Node<T> new. Node; //reference variable new. Node = new Stack. Node<T>(new. Element, stack. Top); //insert before stack. Top = new. Node; } s Method pop public void pop() throws Stack. Underflow. Exception { if (stack. Top == null) throw new Stack. Underflow. Exception(); stack. Top = stack. Top. link; //advance stack. Top } 19
Stacks (Methods) s Method peek public T peek() throws Stack. Underflow. Exception { if (stack. Top == null) throw new Stack. Underflow. Exception(); return stack. Top. info; } 20
Queues s Data structure in which the elements are added at one end, called the rear, and deleted from the other end, called the front. s As in a stack, the middle elements of the queue are inaccessible. s A queue is a First In First Out (FIFO) data structure. 21
Queue Operations s Queue operations s s s initialize. Queue is. Empty. Queue is. Full. Queue front back add. Queue (OR: enqueue) delete. Queue (OR: dequeue) 22
Queue. Exception Class s Adding an element to a full queue and removing an element from an empty queue would generate errors or exceptions s Queue overflow exception s Queue underflow exception s Classes that handle these exceptions s Queue. Exception extends Run. Time. Exception s Queue. Overflow. Exception extends Queue. Exception s Queue. Underflow. Exception extends Queue. Exception 23
Implementation of Queues as Arrays s Instance variables s s An array to store the queue elements queue. Front: keeps track of the first element queue. Rear: keeps track of the last element max. Queue. Size: specifies the maximum size of the queues 24
Implementation of Queues as Arrays Figure 17 -42 Queue after three add. Queue operations 25
Implementation of Queues as Arrays Figure 17 -43 Queue after the delete. Queue operation CORRECTION: queue. Front = 1 26
Implementation of Queues as Arrays s Problems with this implementation s Arrays have fixed sizes s After various insertion and deletion operations, queue. Rear will point to the last array position s Giving the impression that the queue is full s Solutions s Slide all of the queue elements toward the first array position s Use a circular array 27
Implementation of Queues as Arrays Figure 17 -45 Circular queue 28
Implementation of Queues as Arrays Figure 17 -46 Queue with two elements at positions 98 and 99 29
Implementation of Queues as Arrays Figure 17 -47 Queue after one more add. Queue operation 30
Queues (Methods) s Default constructor public Queue. Class(){ max. Queue. Size = 100; queue. Front = 0; queue. Rear = max. Queue. Size - 1; count = 0; list = (T[]) new Object[max. Queue. Size]; } s Method initilialize. Queue public void initialize. Queue(){ for (int i = queue. Front; i < queue. Rear; i = (i + 1) % max. Queue. Size) list[i] = null; queue. Front = 0; queue. Rear = max. Queue. Size - 1; count = 0; } 31
Queues (Methods) s Method is. Empty. Queue public boolean is. Empty. Queue() { return (count == 0); } s Method is. Full. Queue public boolean is. Full. Queue() { return (count == max. Queue. Size); } 32
Queues (Methods) s Method front public T front() throws Queue. Underflow. Exception { if (is. Empty. Queue()) throw new Queue. Underflow. Exception(); return (T) list[queue. Front]; } s Method back public T back() throws Queue. Underflow. Exception { if (is. Empty. Queue()) throw new Queue. Underflow. Exception(); return (T) list[queue. Rear]; } 33
Enqueue /add. Queue s Method add. Queue public void add. Queue(T queue. Element)throws Queue. Overflow. Exception { if (is. Full. Queue()) throw new Queue. Overflow. Exception(); //circular array use % op. to advance queue. Rear = (queue. Rear + 1) % max. Queue. Size; count++; list[queue. Rear] = queue. Element; } 34
Dequeue /delete. Queue s Method delete. Queue public void delete. Queue() throws Queue. Underflow. Exception { if (is. Empty. Queue()) throw new Queue. Underflow. Exception(); count--; list[queue. Front] = null; //circular array use % op. to advance queue. Front = (queue. Front + 1) % max. Queue. Size; } 35
Linked Implementation of Queues s Simplifies many of the special cases of the array implementation s Because the memory to store a queue element is allocated dynamically, the queue is never full s Class Linked. Queue. Class implements a queue as a linked data structure s It uses nodes of type Queue. Node 36
Queues (Methods) s Method initialize. Queue public void initialize. Queue(){ queue. Front = null; queue. Rear = null; } s Method is. Empty. Queue public boolean is. Empty. Queue(){ return (queue. Front == null); } s Method is. Full. Queue public boolean is. Full. Queue(){ return false; } 37
Enqueue / add. Queue s Method add. Queue public void add. Queue(T new. Element) { Queue. Node<T> new. Node; new. Node = new Queue. Node<T>(new. Element, null); if (queue. Front == null) {//empty list? queue. Front = new. Node; queue. Rear = new. Node; } else { //not empty add new. Node at the end queue. Rear. link = new. Node; queue. Rear = queue. Rear. link; } } 38
Queues (Methods) s Method front public T front() throws Queue. Underflow. Exception { if (is. Empty. Queue()) throw new Queue. Underflow. Exception(); return queue. Front. info; } s Method back public T back() throws Queue. Underflow. Exception { if (is. Empty. Queue()) throw new Queue. Underflow. Exception(); return queue. Rear. info; } 39
Dequeue / delete. Queue s Method delete. Queue public void delete. Queue() throws Queue. Underflow. Exception { if (is. Empty. Queue()) throw new Queue. Underflow. Exception(); //advance queue. Front = queue. Front. link; //if after deletion the queue is empty if (queue. Front == null) queue. Rear = null; } 40
- Slides: 40