Stacks Ellen Walker CPSC 201 Data Structures Hiram College.

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Stacks Chapter 11.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Queues Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures - Stacks. What are data structures? Different ways to organize data What data structures have we used before? lists / arrays Deck (AceyDeucey)
CHAPTER 3 Stacks. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek  empty  To understand.
Topic 15 Implementing and Using Stacks
Data Structures & Algorithms
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Topic 15 Implementing and Using Stacks
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Class 4: Queues. cis 335 Fall 2001 Barry Cohen What is a queue? n A stack is an ordered sequence of items. n As in lists and stacks, each node contains.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 3 Stacks.
CHAPTER 3 Stacks MIDTERM OCTOBER 17 IN LAB. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
Stacks and Queues Introduction to Computing Science and Programming I.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks and Queues Pepper. Why History Simplicity Operating Systems – Function Call Stack.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Object-Oriented Programming Simple Stack Implementation.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
Circular Linked List Singly Circular Linked List Doubly Circular Linked List.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Click to edit Master text styles Stacks Data Structure.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Linked Data Structures
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 6.
Revised based on textbook author’s notes.
Stacks and Queues.
Building Java Programs
Pointers and Linked Lists
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
5.1 The Stack Abstract Data Type
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Stacks.
Topic 15 Implementing and Using Stacks
Stacks CS-240 Dick Steflik.
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
Chapter 5 Stack (part 1).
Presentation transcript:

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 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 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; }

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(“”); }

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; }

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); }}

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))