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
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 7
Implementation of Stacks as Arrays Figure 17 -5 UML class diagram of the class Stack. Class 8
Implementation of Stacks as Arrays Figure 17 -6 Example of a stack 9
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; } 10
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); } 11
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; } 12
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]; } 13
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 14
Linked Implementation of Stacks (continued) Figure 17 -13 Nonempty linked stack 15
Stacks (Methods) s Default constructor public Linked. Stack. Class() { stack. Top = null; } s Method initialize. Stack public void initialize. Stack() { stack. Top = null; } 16
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; } 17
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 } 18
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; } 19
- Slides: 19