Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 3333 Data Structures Stacks.

Similar presentations


Presentation on theme: "CSCI 3333 Data Structures Stacks."— Presentation transcript:

1 CSCI 3333 Data Structures Stacks

2 Acknowledgement Dr. Bun Yue Mr. Charles Moen Dr. Wei Ding
Ms. Krishani Abeysekera Dr. Michael Goodrich

3 Stacks A Last In First Out (LIFO) data structure.

4 Simple Stack ADT Example
int size() Return the number of elements in the stack bool isEmpty() Indicate whether the stack is empty void push( Object element ) Insert element at the top of the stack Object top() Return the top element on the stack without removing it; an error occurs if the stack is empty. pop() Remove and return the top element on the stack; an error occurs if the stack is empty

5 Exceptions Attempting the execution of an operation of ADT may sometimes cause an error condition, called an exception Exceptions are said to be “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

6 C++ STL Stack See C++ STL reference. Methods:
Stack constructors : construct a new stack Empty: true if the stack has no elements pop : removes the top element of a stack Push: adds an element to the top of the stack Size: returns the number of items in the stack Top: returns the top element of the stack

7 Applications of Stacks
Direct applications Page-visited history in a Web browser Undo sequence in a text editor Chain of method calls in view stack in visual studio Indirect applications Auxiliary data structure for algorithms Component of other data structures

8 Runtime Stacks main() { int i = 5; foo(i); }
foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … } A running C++ program has a private stack, called the run-time stack, which is a chain of active methods with a stack When a method is called, it is pushed on the stack, which is used to keep track of local variables and other information on functions. These descriptors are called frames. Local variables and return value Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack Allows for recursion bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5

9 Array-based Stack Algorithm size() return t + 1 Algorithm pop()
A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable keeps track of the index of the top element Algorithm size() return t + 1 Algorithm pop() if isEmpty() then throw StackEmptyException else o = s[t] t  t  1 return o //pg. 161 S 1 2 t N-element array S where t gives the index of the top element

10 Array-based Stack (cont.)
The array storing the stack elements may become full A push operation will then throw a FullStackException Limitation of the array-based implementation Not intrinsic to the Stack ADT Algorithm push(o) if t = S.length  1 then throw StackFullException else t  t + 1 S[t]  o S 1 2 t

11 Performance and Limitations
Let n be the number of elements in the stack The space used is O(n) 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

12 Questions and Comments?


Download ppt "CSCI 3333 Data Structures Stacks."

Similar presentations


Ads by Google