Another common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties

Similar documents
The Abstract Data Type Queue. Module 8. The Abstract Data Type Queue. The Abstract Data Type Queue. The Abstract Data Type Queue

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

Queue with Array Implementation

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

Queue Linked List Implementation

Ashish Gupta, Data JUET, Guna

1 P age DS & OOPS / UNIT II

LIFO : Last In First Out


Stack and Queue. Stack:

Stacks and Queues. CSE 373 Data Structures Lecture 6

Your Topic Goes Here Queue

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

What Can You Use a Queue For?

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

.:: UNIT 4 ::. STACK AND QUEUE

Queues. 1 Introduction. 2 The Queue ADT. Prof. Stewart Weiss. CSci 235 Software Design and Analysis II Queues

PA3 Design Specification

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CHAPTER 5. QUEUE v2 by

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

Name CPTR246 Spring '17 (100 total points) Exam 3

csci 210: Data Structures Stacks and Queues

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

Lecture 4 Stack and Queue

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

CS302 - Data Structures using C++

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Data Structure. Recitation VII

CSC 1052 Algorithms & Data Structures II: Linked Queues

ITI Introduction to Computing II

Lecture Data Structure Stack

CSC 1052 Algorithms & Data Structures II: Queues

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

CSCD 326 Data Structures I Queues

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

CSI33 Data Structures

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

Linked List. April 2, 2007 Programming and Data Structure 1

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

Postfix (and prefix) notation

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

COSC 1P03. Ch 6 Generics. Introduction to Data Structures 7.1

CS24 Week 4 Lecture 2

Lecture 3: Stacks & Queues

CSEN 301 Data Structures and Algorithms

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Separate Compilation and Namespaces Week Fall. Computer Programming for Engineers

Information Science 2

Data Structures. Lecture 5 : The Queues

8. Fundamental Data Structures

DS L9: Queues

Stacks and Queues. David Greenstein Monta Vista

An Introduction to Queues With Examples in C++

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

BBM 201 DATA STRUCTURES

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

Basic Data Structures

Data Structures and Algorithms

CSC148 Week 2. Larry Zhang

Chapter 5. ADTs Stack and Queue

3. Fundamental Data Structures

Chapter 18: Stacks And Queues

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

Basic Data Structures 1 / 24

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

! A data type for which: ! An ADT may be implemented using various. ! Examples:

STACKS AND QUEUES. Problem Solving with Computers-II

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Dynamic Data Structures

ADTs Stack and Queue. Outline

Data Structure - Stack and Queue-

Stacks and Queues. CSE Data Structures April 12, 2002

DATA STRUCTURE UNIT I

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

CS 206 Introduction to Computer Science II

Chapter 18: Stacks And Queues

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

CS301 - Data Structures Glossary By

Abstract Data Types! Documentation!

12 Abstract Data Types

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

CMSC Introduction to Algorithms Spring 2012 Lecture 7

ADTs Stack and Queue. Outline

Propedéutico de Programación

15. Stacks and Queues

Sequence Abstract Data Type

Programming. Lists, Stacks, Queues

What is an algorithm?

Transcription:

Queues FIFO queue ADT Examples using queues reading character string in order recognize palindromes Queue implementations LL pointer based List ADT based array based tradeoffs 1 ADT queue operations Create an empty queue Destroy a queue Determine whether a queue is empty Add a new item to the queue Remove the item that was added earliest Retrieve the item that was added earliest 3 Another common linear data structure similar to the stack Queue is an ADT with following properties elements are kept in their order of arrival new items enter at the back, or rear, of the queue items leave from the front of the queue Thus queue has first-in, first-out (FIFO) property nicely models several real-world processes line to buy movie tickets, or queue jobs and print requests Operation Contract for the ADT Queue isempty():boolean {query} enqueue(in newitem:queueitemtype) dequeue() dequeue(out queuefront:queueitemtype) getfront(out queuefront:queueitemtype) {query} 2 4

Figure 7-2 Some queue operations 5 Example2: Recognizing Palindromes A palindrome is a string of characters that reads the same backwards and forwards RADAR, MADAM, EYE, etc. Observations stack reverses the order of occurrences queue preserves the order of occurrences A palindrome stored in both stack and queue will display a match when retrieved 7 Example 1: Ordering Character String A queue can retain characters in the order in which they are typed aqueue.createqueue() while (not end of line) { Read a new character ch aqueue.enqueue(ch) } // end while Once the characters are in a queue, the system can process them as necessary Example2: Recognizing Palindromes A nonrecursive recognition algorithm for palindromes traverse character string from left to right insert each character into both a queue and a stack compare the characters at the front of the queue and the top of the stack see C7-palin.cpp 6 8

Implementations of the ADT Queue Linked list based queue implementation can maintain pointers to front and back of Queue circular linked list with one external reference also possible Using ADT List class to implement queue possible less efficient, but simple An array-based queue implementation problem of rightward-drift Operations in LL Implementation Figure 7-5 Inserting an item into a nonempty queue Figure 7-6 Inserting an item into an empty queue: (a) before insertion; (b) after insertion Figure 7-7 Deleting an item from a queue of more than one item see C7-QueueP.cpp 9 11 Linked List Implementations Figure 7-4 A pointer-based implementation of a queue: (a) a linear linked list with two external pointers; (b) a circular linear linked list with one external pointer List Based Queue Implementation Queue operations map well to ADT List operations enqueue(item) insert(getlength()+1, item) dequeue() remove(1) getfront(qfront) retrieve(1, qfront) We can built the queue ADT as a wrapper over the List ADT see C7-QueueL.cpp 10 12

An Array-Based Implementation Using arrays is slightly more complex naïve implementation causes rightward drift queue appears full even when array does not hold MAX_QUEUE-1 elements Solutions to rightward drift always copy array elements to left expensive maintain circular array how to detect queue full/empty? An Array-Based Implementation Initialize the queue, front = 0, back = MAX_QUEUE 1, count = 0 Inserting into a queue back = (back+1) % MAX_QUEUE; items[back] = newitem; ++count; Deleting from a queue front = (front+1) % MAX_QUEUE; --count; see C7-QueueA.cpp 13 15 Circular Array Implementation Problem: front == (back+1) is true for both queue full & empty Solution: use integer counter to hold size of queue update on each enqueue/dequeue Array Implementation Variations Use a flag isfull to distinguish between the full and empty conditions Declare MAX_QUEUE + 1 locations for the array items, but use only MAX_QUEUE of them for queue items 14 16

Comparing Implementations Static arrays Vs. dynamically allocated LLs enqueue operation cannot add item if array is full no size restriction with LL (unless memory full) LL Vs List bases array implementations LL-based implementation is more efficient ADT list approach reuses already implemented class much simpler to write saves programming time A Summary of Position-Oriented ADTs Stacks and queues are very similar Operations of stacks and queues can be paired off createstack and createqueue Stack isempty and queue isempty push and enqueue pop and dequeue Stack gettop and queue getfront 17 19 A Summary of Position-Oriented ADTs Position-oriented ADTs List Stack Queue Stacks and queues Only the end positions can be accessed Lists All positions can be accessed Summary ADT queue has first-in, first-out (FIFO) behavior Circular array eliminates the problem of rightward drift in array-based implementation To distinguish between the queue-full and queueempty conditions in a circular array count the number of items in the queue use an isfull flag leave one array location empty LL and List ADT based implementations possible 18 20