Presentation is loading. Please wait.

Presentation is loading. Please wait.

Click to edit Master text styles Stacks Data Structure.

Similar presentations


Presentation on theme: "Click to edit Master text styles Stacks Data Structure."— Presentation transcript:

1 Click to edit Master text styles Stacks Data Structure

2 Click to edit Master text styles Stack Collection A stack is a sequence of items that are accessible only from the top of the stack. 1 A Stack (of plates) push pop Other operations: is Empty peek size

3 Click to edit Master text styles Stack Operation An item popped from the stack is the last element that was pushed. A stack has LIFO (last-in-first-out) ordering.

4 Click to edit Master text styles Stack Operation

5 Click to edit Master text styles Stack Abstract Data Type push(e) : Adds element e to the top of the stack. pop( ) : Removes and returns the top element from the stack (or null if the stack is empty). Additionally, a stack supports the following accessor methods for convenience: top( ) : Returns the top element of the stack, without removing it (or null if the stack is empty). size( ) : Returns the number of elements in the stack. isEmpty( ) : Returns a Boolean indicating whether the stack is empty.

6 Click to edit Master text styles Example: The following table shows a series of stack operations and their effects on an initially empty stack S of integers.

7 Click to edit Master text styles The java.util.Stack Class Provides a side-by-side comparison of the interface for out stack ADT and the java.util.Stack class. Methods pop and peek of the java.util.Stack class throw a custom EmptyStackException (StackX.java)

8 Click to edit Master text styles The Array List Stack Class (ALStack) A stack can be implemented using a List class the ALStack class uses an ArrayList to implement the Stack interface.

9 Click to edit Master text styles A Simple Array-Based Stack Implement Arrays start at index 0 in Java, when the stack holds elements from data[0] to data[t] inclusive, it has size t+1. When the stack is empty it will have t equal to -1 (and thus has size t+1, which is 0). (StackApp.java)

10 Click to edit Master text styles StackX.java Class Methods The constructor creates a new stack of a size specified in its argument. The push() method increments top so it points to the space just above the previous top, and stores a data item there. The pop() method returns the value at top. The peek() method simply returns the value at top. The isEmpty() and isFull() methods return true if the stack is empty or full, The top variable is at -1 if the stack is empty and maxSize-1 if the stack is full.

11 Click to edit Master text styles Stack Example 1 : Reversing a Word First the characters are extracted one by one from the input string and pushed onto the stack. Then they’re poped off the stack and displayed. The stack reverses the order of the characters. Shows the code for the Reverser.java program.

12 Click to edit Master text styles Stack Example 2: Delimiter Matching Typically the strings are lines of code in a computer language, and the programs parsing them are compilers. The delimiters are the braces ‘{‘and’}’, brackets ‘[‘and’]’, and parentheses ‘(‘and’)’. Each opening or left delimiter should be matched by a closing or right delimiter.

13 Click to edit Master text styles Stack Example 2: Delimiter Matching Typically the strings are lines of code in a computer language, and the programs parsing them are compilers. The delimiters are the braces ‘{‘and’}’, brackets ‘[‘and’]’, and parentheses ‘(‘and’)’. Each opening or left delimiter should be matched by a closing or right delimiter.


Download ppt "Click to edit Master text styles Stacks Data Structure."

Similar presentations


Ads by Google