Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
-
Upload
sabrina-shepherd -
Category
Documents
-
view
212 -
download
0
Transcript of Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
![Page 1: Stacks Ellen Walker CPSC 201 Data Structures Hiram College.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697bfa51a28abf838c97a1c/html5/thumbnails/1.jpg)
Stacks
Ellen Walker
CPSC 201 Data Structures
Hiram College
![Page 2: Stacks Ellen Walker CPSC 201 Data Structures Hiram College.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697bfa51a28abf838c97a1c/html5/thumbnails/2.jpg)
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…
![Page 3: Stacks Ellen Walker CPSC 201 Data Structures Hiram College.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697bfa51a28abf838c97a1c/html5/thumbnails/3.jpg)
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)
![Page 4: Stacks Ellen Walker CPSC 201 Data Structures Hiram College.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697bfa51a28abf838c97a1c/html5/thumbnails/4.jpg)
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
![Page 5: Stacks Ellen Walker CPSC 201 Data Structures Hiram College.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697bfa51a28abf838c97a1c/html5/thumbnails/5.jpg)
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
![Page 6: Stacks Ellen Walker CPSC 201 Data Structures Hiram College.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697bfa51a28abf838c97a1c/html5/thumbnails/6.jpg)
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?
}
![Page 7: Stacks Ellen Walker CPSC 201 Data Structures Hiram College.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697bfa51a28abf838c97a1c/html5/thumbnails/7.jpg)
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;}
![Page 8: Stacks Ellen Walker CPSC 201 Data Structures Hiram College.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697bfa51a28abf838c97a1c/html5/thumbnails/8.jpg)
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(“”);
}
![Page 9: Stacks Ellen Walker CPSC 201 Data Structures Hiram College.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697bfa51a28abf838c97a1c/html5/thumbnails/9.jpg)
But we have a problem…
• The most recent entry to the stack is the last character on the line!
• Reverse the values using two stacksStack<Character> reverse (Stack<Character> stack1){
stack2 = new Stack<Character>();
while(!stack1.empty()){
stack2.push(stack1.pop());
}
return stack2;
}
![Page 10: Stacks Ellen Walker CPSC 201 Data Structures Hiram College.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697bfa51a28abf838c97a1c/html5/thumbnails/10.jpg)
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);
}}
![Page 11: Stacks Ellen Walker CPSC 201 Data Structures Hiram College.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697bfa51a28abf838c97a1c/html5/thumbnails/11.jpg)
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
![Page 12: Stacks Ellen Walker CPSC 201 Data Structures Hiram College.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697bfa51a28abf838c97a1c/html5/thumbnails/12.jpg)
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)
![Page 13: Stacks Ellen Walker CPSC 201 Data Structures Hiram College.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697bfa51a28abf838c97a1c/html5/thumbnails/13.jpg)
Balancing Examples
• (defun v(x y) (or (and x y) x y))
• (cond ((null x) nil) ((t nil))
• (cons (car (cdr x)) y))