Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
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.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
CHAPTER 7 Queues.
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.”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
CS Data Structures II Review COSC 2006 April 14, 2017
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
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.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
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.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
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 7 Stacks II CS Data Structures I COSC 2006
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 3 Introduction to Collections – Stacks Modified
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Data Structures Using C++
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Chapter 5 The Queue ADT. Chapter 5: The Queue ADT 5.1 – Queues 5.2 – Formal Specification 5.3 – Array-Based Implementations 5.4 – Application: Palindromes.
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.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
Chapter 5 The Queue ADT. 5.1 Queues Queue A structure in which elements are added to the rear and removed from the front; a “first in, first out” (FIFO)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Sections 3.4 Formal Specification
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Stacks.
Stacks and Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Chapter 19: Stacks and Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
ADT list.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Stacks and Queues 1.
ADT Queue (Array Implementation)
Lecture 16 Stacks and Queues CSE /26/2018.
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
Lecture 16 Stacks and Queues CSE /26/2018.
Data Structures & Programming
Presentation transcript:

Chapter 4 ADTs Stack and Queue

4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic unit. Therefore— We formalize the specification of our ADTs by using a Java interface. Benefits: –We can formally check the syntax of our specification. –We can formally verify the interface “contract.” –We can assume a consistent interface among alternate implementations of the ADT.

4-3 The Interface ListInterface Please click on the following link Programs/C04P251.jpg to view the appropriate program. Programs/C04P251.jpg

4-4 UML class diagram for list approach Please click on the following link Programs/C04P254.jpg to view the appropriate program. Programs/C04P254.jpg

4-5 Stack A structure in which elements are added and removed from only one end; a “last in, first out” (LIFO) structure

4-6 Operations on Stacks 1.push adds an element to the top of a stack. 2.pop removes the top element off the stack. 3.top returns the top element of a stack. 4.isEmpty returns a boolean value indicating whether or not the stack is empty.

4-7 The effects of push and pop operations

4-8 The code for the StackUnderflowException class

4-9 The code for the StackOverflowException class

Implementing ADTs “by copy” or “by reference”

4-11 “By Copy” Approach

4-12 “By Copy” Continued

4-13 “By Copy” Continued

4-14 “By Reference” Approach

4-15 “By Reference” Continued

4-16 “By Reference” Continued

4-17 Approaches Used for our Stacks “by reference” (not “by copy”) Throw exceptions (not “by contract”) Contains Objects

4-18 The Stack ADT Specification Please click on the following link Programs/C04P263.jpg to view the appropriate program. Programs/C04P263.jpg

4-19 A Stack-Based Application

4-20 Main Algorithm

4-21 The “Process the current character” command Please click on the following link Programs/C04P267.jpg to view the appropriate program. Programs/C04P267.jpg

4-22 The Code for the Program Balanced Please click on the following link Programs/C04P268.jpg to view the appropriate program. Programs/C04P268.jpg

4-23 test1.in file

4-24 Command: java Balanced test1.in test1.out

4-25 test1.out file

4-26

4-27 Stack Overflow The condition resulting from trying to push an element onto a full stack Client code example:

4-28

4-29

4-30 PUSH public void push(Object item) // Adds an element to the top of this stack { if (!isFull()) { topIndex++; stack[topIndex] = item; } else throw new StackOverflowException("Push attempted on a full stack."); }

4-31 POP public void pop() // Removes an element from the top of this stack { if (!isEmpty()) { stack[topIndex] = null; topIndex--; } else throw new StackUnderflowException("Pop attempted on an empty stack."); }

4-32 An ArrayList-Based Implementation Please click on the following link Programs/C04P278.jpg to view the appropriate program. Programs/C04P278.jpg

4-33 Queue A structure in which elements are added to the rear and removed from the front; a “first in, first out” (FIFO) structure

4-34 Operations on Queues enqueue Adds an element to the rear dequeue Removes and returns the front element isEmpty Returns true if the queue is empty and false otherwise

4-35 Approaches Used for our Queues: By “reference” (not “by copy”) Contains Objects By “contract” (no exceptions thrown) Uses classic “remove and return” dequeue operation

4-36 The Queue ADT Specification Please click on the following link Programs/C04P289.jpg to view the appropriate program. Programs/C04P289.jpg

4-37 A Queue-Based Application— Identifying Palindromes “A man, a plan, a canal—Panama!” “Able I was ere, I saw Elba.” “Won ton? Not now!” “Madam, I’m Adam.” “Eve.”

4-38 Main Algorithm

4-39 Process the current string

4-40 The code for the program Palindrome Please click on the following link Programs/C04P293.jpg to view the appropriate program. Programs/C04P293.jpg

4-41 Floating-Front Design Approach

4-42 Wrapping the queue elements around

4-43 Declarations and Constructors public class ArrayQueue implements QueueInterface { private Object[] queue; // Array that holds queue elements private int capacity; // Size of the array (capacity of the queue) private int numItems = 0; // Number of items on the queue private int front = 0; // Index of front of queue private int rear = -1; // Index of rear of queue // Constructors public ArrayQueue() { queue = new Object[100]; capacity = 100; } public ArrayQueue(int maxSize) { queue = new Object[maxSize]; capacity = maxSize; }

4-44 Enqueue public void enqueue(Object item) // Adds an element to the front of this queue { rear = (rear + 1) % capacity; queue[rear] = item; numItems = numItems + 1; }

4-45 Dequeue public Object dequeue() // Removes an element from the rear of this queue { Object toReturn = queue[front]; queue[front] = null; front = (front + 1) % capacity; numItems = numItems - 1; return toReturn; }

4-46 public boolean isEmpty() // Checks if this queue is empty { return (numItems == 0); } public boolean isFull() // Checks if this queue is full { return (numItems == capacity); }