Download presentation

Presentation is loading. Please wait.

Published byCael Lackland Modified over 2 years ago

1
Lecture Stacks

2
A stack is a Last-In-First-Out (LIFO) or a First-In-Last-Out (FILO) abstract data type E.g. a deck of cards in which cards may be added to or removed from the deck only at the top The most recent card to be added is the first to be retrieved Stacks are implemented using linked lists or arrays for various computing tasks

3
Operation On a Stack: Informal Abstract Definition An abstract data type is defined by its operation Create: i.e. initialise an empty stack Push: add a data item to the top of the stack Pop: remove the data item on the top of the stack. (if the stack happens to be empty an exception handler needs to be used) IsEmpty: determine whether the stack is empty or not

4
Linked List Stack A stack is a constrained version of a linked list Nodes can only be added to and removed from the front of the list (Fig. 5.1). Therefore, we can implement a stack class by reusing class StudentList (from week 4). This can be achieved through two forms of reusability, namely inheritance and composition

5
Linked List Stack LIFO (FirstNode) FILO (LastNode)

6
class StackInheritance extends StudentList { public StackInheritance() { super("Stack"); } public void push (String nameIn, int ageIn) { insertAtFront(nameIn, ageIn); } public ListNode pop () throws EmptyListException { return removeFromFront(); } public boolean isEmpty () { return super.isEmpty(); } public void printStack () { printList(); } } Linked List Stack: Inheritance (“is a”) Example

7
StackInheritance theStack = new StackInheritance(); // Use the push method theStack.push("Alice", 20 ); theStack.printStack(); theStack.push("John", 22 ); theStack.printStack(); theStack.push("Rita", 21); theStack.printStack(); theStack.push("Bob", 23); theStack.printStack(); Test driver for class StackInheritance

8
ListNode removedObj = null; try { while ( true ) { removedObj = theStack.pop(); System.out.println( removedObj.name.toString() + " popped" ); theStack.printStack(); } catch ( EmptyListException e ) { System.err.println( "\n" + e.toString() ); }

9
Inheritance Class StackInheritance inherits all methods and attributes from base StudentList StackInheritance class extends the functionality of the StudentList Note however, methods insertAtBack and removeFromBack are also accessible by class StackInheritance but should not be used!!!

10
class StackComposition { private StudentList s; public StackComposition() { s = new StudentList("Stack"); } public void push (String nameIn, int ageIn) { s.insertAtFront(nameIn,ageIn); } public ListNode pop () throws EmptyListException { return s.removeFromFront(); } public boolean isEmpty () { return s.isEmpty(); } public void printStack () { s.printList(); } } Linked List Stack: Composition (“has a”) Example

11
Composition Class StackComposition uses a private reference s (instance variable) to a StudentList object StackComposition hides StudentList methods (insertAtBack and removeFromBack) that should be not be used in our stack implementation. Class StackComposition only provides public methods ‘delegated’ to use only the required StudentList methods.

12
Stack Implementation: Arrays Together with the stack we need a stack pointer (index) Stack pointer indicates the next free element Additionally, we will need to determine if the stack is full An array stack Stack Implementation: Arrays Bob 20 Alice 19 Trudy 23 [3] [2] [1] [0] index

13
Applications of Stack Storing the return addresses of machine code routines (i.e. method calls) Evaluation of arithmetic expression swapping two values, achieved through a small stack area (registers) inside the CPU for greater speed Recursion is implemented using stacks (data frames)

14
Evaluation of Arithmetic expression Example Given (2 + 3 ) * 4 (infix notation) Convert to postfix (reverse polish) notation using Dijkstra’s Shunting Algorithm Postfix: * Evaluate expression using stack and a suitable algorithm

15
push(2) push(3) 3232 (1) stack +(3,2) = 5 push(5) 5 (3) stack pop(3) pop(2) (2) stack *(5,4) = 20 push(20) 20 (6) stack 4545 push(4) (4) stack Postfix (Reverse Polish): * pop(4) pop(5) (5) stack

16
pop(20) 20 (7) Postfix (Reverse Polish): *

17
Queues What is it? Informal Abstract Definition? Implementation? Applications?

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google