stacks l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Stacks PowerPoint Presentation
Download Presentation
Stacks

Loading in 2 Seconds...

play fullscreen
1 / 13

Stacks - PowerPoint PPT Presentation


  • 161 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Stacks' - Mia_John


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
stacks

Stacks

Ellen Walker

CPSC 201 Data Structures

Hiram College

stack
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
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
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
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
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
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
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
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
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
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
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
Balancing Examples
  • (defun v(x y) (or (and x y) x y))
  • (cond ((null x) nil) ((t nil))
  • (cons (car (cdr x)) y))