130 likes | 496 Views
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
E N D
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 • Stack of books…
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)
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
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
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? }
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; }
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(“”); }
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; }
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); }}
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
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)
Balancing Examples • (defun v(x y) (or (and x y) x y)) • (cond ((null x) nil) ((t nil)) • (cons (car (cdr x)) y))