Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.

Similar presentations


Presentation on theme: "Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in."— Presentation transcript:

1 stack

2

3

4

5

6

7

8

9

10 ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first- out scheme Think of a spring-loaded PEZ dispenser Main stack operations: –push(object): inserts an element –object pop(): removes and returns the last inserted element Auxiliary stack operations: –object top(): returns the last inserted element without removing it –integer size(): returns the number of elements stored –boolean isEmpty(): indicates whether no elements are stored

11 ADS2 Lecture 1011 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first- out scheme Think of a spring-loaded PEZ dispenser Main stack operations: –push(object): inserts an element –object pop(): removes and returns the last inserted element Auxiliary stack operations: –object top(): returns the last inserted element without removing it –integer size(): returns the number of elements stored –boolean isEmpty(): indicates whether no elements are stored

12 ADS2 Lecture 1012 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first- out scheme Think of a spring-loaded PEZ dispenser Main stack operations: –push(object): inserts an element –object pop(): removes and returns the last inserted element Auxiliary stack operations: –object top(): returns the last inserted element without removing it –integer size(): returns the number of elements stored –boolean isEmpty(): indicates whether no elements are stored

13 ADS2 Lecture 1013 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first- out scheme Think of a spring-loaded PEZ dispenser Main stack operations: –push(object): inserts an element –object pop(): removes and returns the last inserted element Auxiliary stack operations: –object top(): returns the last inserted element without removing it –integer size(): returns the number of elements stored –boolean isEmpty(): indicates whether no elements are stored

14

15 The Stack Example What is the stack formed by carrying out the following sequence of instructions: push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A;

16 The Stack Example What is the stack formed by carrying out the following sequence of instructions: push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; E

17 The Stack Example What is the stack formed by carrying out the following sequence of instructions: push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; E N

18 N The Stack Example What is the stack formed by carrying out the following sequence of instructions: E push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A;

19 C The Stack Example What is the stack formed by carrying out the following sequence of instructions: E push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A;

20 C The Stack Example What is the stack formed by carrying out the following sequence of instructions: E push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; H

21 The Stack Example What is the stack formed by carrying out the following sequence of instructions: E push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; H C

22 push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C

23 push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C L

24 push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C L L

25 push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C L L

26 push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C A L

27 push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C A L

28 push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C D L

29 push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C D L

30 push E; push N; pop; push C; push H; pop; push I; push L; pop; push A; pop; push D; pop; push A; The Stack Example What is the stack formed by carrying out the following sequence of instructions: E I C A L

31

32 ADS2 Lecture 1032 Stack Interface in Java Java interface corresponding to our Stack ADT Requires the definition of class EmptyStackException Different from the built-in Java class java.util.Stack public interface Stack { /** *Return the number of elements in the stack */ public int size(); /** * Return whether the stack is empty */ public boolean isEmpty();

33 ADS2 Lecture 1033 /** * Inspect the element at the top of the stack */ public E top() throws EmptyStackException; /** * Insert an element at the top of the stack */ public void push (E element); /** * Remove the top element from the stack */ public E pop() throws EmptyStackException; } Stack interface contd.

34 ADS2 Lecture 1034 Exceptions Attempting the execution of an operation of ADT may sometimes cause an exception Exception is thrown by an operation that cannot be executed In the Stack ADT, operations pop and top cannot be performed if the stack is empty Attempting the execution of pop or top on an empty stack throws an EmptyStackException public class EmptyStackException extends RuntimeException { public EmptyStackException(String err){ super(err); } bit of revision coming up..

35 ADS2 Lecture 1035 Applications of Stacks Direct applications –Page-visited history in a Web browser –Undo sequence in a text editor –Chain of method calls in the Java Virtual Machine Indirect applications –Auxiliary data structure for algorithms –Component of other data structures

36

37

38 ADS2 Lecture 1038 Array-based Stack A simple way of implementing the Stack ADT uses an array We add elements from left to right (top is on the right) A variable tos keeps track of the index of the top of stack element S 012 t …

39 ArrayStack

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54 Demo ArrayStack stack grows from left to right tos is on right hand side test out underflow test out overflow

55 ADS2 Lecture 1055 Performance and Limitations Performance –Let m be the size of the array –The space used is O(m) (independent of no. of elements actually in stack) –Each operation runs in time O(1) Limitations –The maximum size of the stack must be defined a priori and cannot be changed –Trying to push a new element into a full stack causes an implementation-specific exception

56 Example … a stack of characters used for checking parentheses Stack is implemented as an array

57 ADS2 Lecture 1057

58 ADS2 Lecture 1058

59 ADS2 Lecture 1059

60 ADS2 Lecture 1060

61 ADS2 Lecture 1061

62 ADS2 Lecture 1062

63 Analysis ADS2 Lecture 1063 Running time for methods in realization of stack by an array: MethodTime sizeO(1) isEmptyO(1) topO(1) pushO(1) popO(1) All methods run in constant time. Each consists of a constant number of operations. Pop also calls isEmpty, which is itself constant time.


Download ppt "Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in."

Similar presentations


Ads by Google