Chapter 7 Stacks II CS Data Structures I COSC 2006

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, Queues, and Linked Lists
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
CS Data Structures II Review COSC 2006 April 14, 2017
Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CMPT 225 Stacks.
Chapter 3 Stacks.
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.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
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.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
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.
1 Lecture 26 Abstract Data Types –III Overview  Creating and manipulating Linked List.  Linked List Traversal.  Linked List Traversal using Iterator.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Summary of lectures (1 to 11)
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
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.
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.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
CSC 205 Programming II The ADT Stack. Recap: ADT Abstract Data Type A collection of data (objects) A set of operations on that data Add Remove Retrieve.
1 Lecture 14: Queues Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
CS Data Structures I Chapter 7 Queue I. 2 Topics Introduction Queue Application Implementation Linked List Array ADT List.
CS2006- Data Structures I Chapter 5 Linked Lists III.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
April 27, 2017 COSC Data Structures I Review & Final Exam
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
“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.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Stack: a Linked Implementation
Stacks and Queues Chapter 4.
CC 215 Data Structures Stack ADT
Week 3 - Friday CS221.
Stacks.
Stack and Queue APURBO DATTA.
Pointers and Linked Lists
Stacks.
ADT list.
Ch. 8 Queue Stack Queue milk queue stack
Ch. 8 Queue Stack Queue milk queue stack
Stacks CS-240 Dick Steflik.
Ch. 7 Stack 도입을 위한 예 “←” in keyboard input line
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Stack Implementations
Presentation transcript:

Chapter 7 Stacks II CS2006 - Data Structures I COSC 2006 April 22, 2017 CS2006 - Data Structures I Chapter 7 Stacks II Chapter 6: Stacks

Topics Implementation of ADT Stack Using Linked lists Using ADT List

Linked List Stack Implementation COSC 2006 April 22, 2017 Linked List Stack Implementation Stacks can grow and shrink dynamically No particular linked list variations are required: The head pointer can also serve as the top of the stack. Pushing becomes inserting the first node in a linked list. Popping becomes removing the first node. Chapter 6: Stacks

Linked List Stack Implementation COSC 2006 April 22, 2017 Linked List Stack Implementation The head of List is the top of the stack head 1 3 7 9 1 top 3 7 9 Chapter 6: Stacks

LL Stack Implementation COSC 2006 April 22, 2017 LL Stack Implementation // Assumes that the classes StackInterface and Node are available public class StackRefereenceBased implements StackInterface { private Node top; // class Node is the same used in the LinkedList class except it stores // Objects rather than Comparables public StackRefereenceBased (){ top = null; } // end default constructor public boolean isEmpty() { return (top == null); } // end isEmpty Chapter 6: Stacks

LL Stack Implementation COSC 2006 April 22, 2017 LL Stack Implementation // Assumes that the classes StackInterface and Node are available public class StackRefereenceBased implements StackInterface { private Node top; // class Node is the same used in the LinkedList class except it stores // Objects rather than Comparables public StackRefereenceBased (){ top = null; } // end default constructor public boolean isEmpty() { return (top == null); } // end isEmpty Chapter 6: Stacks

LL Stack Implementation (2) COSC 2006 April 22, 2017 LL Stack Implementation (2) public void push(Object newItem) { top = new Node(newItem, top); } // end push public Object pop() throws StackException { if (!isEmpty()) { Object temp = top.getItem(); top = top.getNext(); return temp; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop Chapter 6: Stacks

LL Stack Implementation (2) COSC 2006 April 22, 2017 LL Stack Implementation (2) public void push(Object newItem) { top = new Node(newItem, top); } // end push public Object pop() throws StackException { if (!isEmpty()) { Object temp = top.getItem(); top = top.getNext(); return temp; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop Chapter 6: Stacks

LL Stack Implementation (3) COSC 2006 April 22, 2017 LL Stack Implementation (3) public void popAll() { top=null; } // end popAll public Object peek() throws StackException { if (!isEmpty()) { return top.getItem(); } else { throw new StackException("StackException on " + "peek: stack empty"); } // end else } // end peek } // end StackReferencedBased Chapter 6: Stacks

LL Stack Implementation (3) COSC 2006 April 22, 2017 LL Stack Implementation (3) public void popAll() { top=null; } // end popAll public Object peek() throws StackException { if (!isEmpty()) { return top.getItem(); } else { throw new StackException("StackException on " + "peek: stack empty"); } // end else } // end peek } // end StackReferencedBased Chapter 6: Stacks

ADT Stack ADT List Implementation All stack operations can be implemented using the ADT List's operations The implementation is simpler to write than the array-based and makes used of previously-written code (code reuse) Position 1 of the list represents the top position of the stack

ADT Stack ADT List Implementation top List position 10 80 60 . 5 1 2 3 List site()

ADT List Stack Implementation COSC 2006 April 22, 2017 ADT List Stack Implementation // Assumes that the classes ListInterface and Node are available public class StackListBased implements StackInterface { private ListInterface list; public StackListBased() { list = new ListReferenceBased(); } // end default constructor public boolean isEmpty() { return list.isEmpty (); } // end isEmpty Chapter 6: Stacks

ADT List Stack Implementation COSC 2006 April 22, 2017 ADT List Stack Implementation // Assumes that the classes ListInterface and Node are available public class StackListBased implements StackInterface { private ListInterface list; public StackListBased() { list = new ListReferenceBased(); } // end default constructor public boolean isEmpty() { return list.isEmpty (); } // end isEmpty Chapter 6: Stacks

List Stack Implementation (2) COSC 2006 April 22, 2017 List Stack Implementation (2) public void push(Object newItem) { list.add (1, newItem); } // end push public Object pop() throws StackException { if (!list.isEmpty()) { Object temp = list.get(1); list.remove(1); return temp; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop Chapter 6: Stacks

List Stack Implementation (2) COSC 2006 April 22, 2017 List Stack Implementation (2) public void push(Object newItem) { list.add (1, newItem); } // end push public Object pop() throws StackException { if (!list.isEmpty()) { Object temp = list.get(1); list.remove(1); return temp; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop Chapter 6: Stacks

List Stack Implementation (3) COSC 2006 April 22, 2017 List Stack Implementation (3) public void popAll() { list.removeAll(); } // end popAll public Object peek() throws StackException { if (!list.isEmpty()) { return list.get(1); } else { throw new StackException("StackException on " + "peek: stack empty"); } // end else } // end peek } // end StackListBased Chapter 6: Stacks

ADT Stack Implementation Comparison Array-Based   No overhead of reference manipulation   Prevents adding elements if the array is full LL-Based More complicated than ADT List Most flexible, No size restrictions ADT List-Based Simpler to write Not as efficient as using a linked list directly

Review The ______ method of the ADT stack retrieves and then removes the top of the stack. createStack push pop peek

Review The ______ method of the ADT stack retrieves the top of the stack, but does not change the stack. createStack push pop peek

Review Which of the following methods of the ADT stack accepts a parameter? push pop createStack peek

Review If a stack is used by an algorithm to check for balanced braces, which of the following is true once the end of the string is reached? the stack is empty the stack has one “{” the stack has one “}” the stack has one “{” and one “}”

Review Which of the following operations of the ADT stack does not throw a StackException? push pop popAll peek

Review The pop operation throws a StackException when it tries to ______. add an item to an empty stack add an item to an array-based implementation of a stack that is already full delete an item from an array-based implementation of a stack that is already full delete an item from an empty stack

Review The push operation throws a StackException when it tries to ______. add an item to an empty stack add an item to an array-based implementation of a stack that is already full delete an item from an array-based implementation of a stack that is already full delete an item from an empty stack

Review In the StackInterface class, the pop method returns an item that is an instance of ______. Integer Double String Object