1 / 13

Stacks

Stacks. Ellen Walker CPSC 201 Data Structures Hiram College. 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

Mia_John
Download Presentation

Stacks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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> { 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<Character> readAndCorrect(String input){ Stack<Character> myStack = new Stack<Character>(); 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<char> 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<Character> reverse (Stack<Character> stack1){ stack2 = new Stack<Character>(); 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<Character> st1 = readAndCorrect(line); Stack<Character> 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))

More Related