Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks Ellen Walker CPSC 201 Data Structures Hiram College.

Similar presentations


Presentation on theme: "Stacks Ellen Walker CPSC 201 Data Structures Hiram College."— Presentation transcript:

1 Stacks Ellen Walker CPSC 201 Data Structures Hiram College

2 Stack Collect data Access only “most recently added” item –When “most recently added” item is removed, the former 2nd most recently added item becomes available! Why is it called a stack? –Stack of plates in the cafeteria –Stack of books…

3 Operations on a Stack Create an empty stack (constructor) Is the stack empty? (isEmpty) Add a new item to the stack. (push) Remove the item that was most recently added (pop) Retrieve the item that was most recently added (peek)

4 Example Create an empty stack Push “A” Push “B” Peek (returns B) Pop (returns B) Push “C” Pop (returns C) Pop (returns A) Stack is now empty

5 LIFO vs. FIFO A stack is a LIFO (last in, first out) data structure –New addition makes older items inaccessible –Models interruptions, like call waiting Alternative is FIFO (first in, first out) –We will see this later, in Queue data structure –Queues are fair when someone has to wait

6 Stack Interface (see p. 151) Public interface StackInt { E push(E obj); //returns object inserted E peek(); //returns top object (no change) E pop(); //returns & removes top object boolean empty(); //is it empty? }

7 Interpreting Backspaces with a Stack Stack readAndCorrect(String input){ Stack myStack = new Stack (); for(int j=0;j<input.length();j++){ if(input.charAt(j) != ‘\08’){ //backspace is ‘\08’ myStack.push(input.charAt(j)); else myStack.pop(); //pop last character } return myStack; }

8 Printing the Line //Print the line (as it was in the stack) void printStack(Stack st){ while !st.empty(){ System.out.print(st.pop()); } System.out.println(“”); }

9 But we have a problem… The most recent entry to the stack is the last character on the line! Reverse the values using two stacks Stack reverse (Stack stack1){ stack2 = new Stack (); while(!stack1.empty()){ stack2.push(stack1.pop()); } return stack2; }

10 Putting it all together //program to read a file with backspaces and //print each line as corrected public static void main(String[] args){ BufferedReader in = new BufferedReader (new FileReader (args[1])); String line = null; while (line = in.readLine()) != null){ Stack st1 = readAndCorrect(line); Stack st2 = reverse(st1); printStack(st2); }}

11 Recognizing a Palindrome A Palindrome reads the same forward and backward –Madam I’m Adam –Able was I ere I saw Elba Solution: –Push every character of the string, excluding spaces and punctuation onto a stack –Build the reversed string by popping each element off the stack (using StringBuilder) –Compare the original string (excluding punctuation) to the reversed string

12 Another Example: Balancing Parens A very useful application for Lisp or Scheme! Algorithm –For each character: – If it’s not a paren, ignore it – If it’s a left paren, push it – If it’s a right paren – If the stack is empty, return false (too few left) – else pop the stack –If the stack is not empty, return false (too few right) –Return true (balanced parentheses)

13 Balancing Examples (defun v(x y) (or (and x y) x y)) (cond ((null x) nil) ((t nil)) (cons (car (cdr x)) y))


Download ppt "Stacks Ellen Walker CPSC 201 Data Structures Hiram College."

Similar presentations


Ads by Google