Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Advertisements

Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
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.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Queues.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Chapter.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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’
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure.
الطابور QUEUE (abstract data type) واحد من هياكل البيانات الخطية الشائعة الاستخدام داخل البرامج. يحتوي علي عناصر من نفس النوع. من أنواع البيانات الخطية.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Cpt S 122 – Data Structures Abstract Data Types
CS505 Data Structures and Algorithms
UNIT II Queue.
Chapter 12 – Data Structures
CC 215 Data Structures Queue ADT
Chapter 15 Lists Objectives
Abstract Data Types Polynomials CSCI 240
Data Structures and Algorithms
C++ Plus Data Structures
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Stacks and Queues CMSC 202.
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
Abstract Data Types Sparse Matrices CSCI 240
Stack and Queue APURBO DATTA.
Basic Data Types Queues
Pointers and Linked Lists
Queues.
Queues.
CSC 143 Queues [Chapter 7].
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Data Structures and Algorithms
Stacks and Queues 1.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
Stacks and Queues.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Queues Definition of a Queue Examples of Queues
Stacks, Queues, and Deques
Abstract Data Types Stacks CSCI 240
Data Structures & Programming
Presentation transcript:

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer

Dale Roberts Queues Queue Similar to a supermarket checkout line First-in, first-out (FIFO) Nodes are removed only from the head Nodes are inserted only at the tail Insert and remove operations Enqueue (insert) and dequeue (remove) Queue Model queue is a list, with insertion done only at one end and deletion done at the other end. Linked list implementation of queues operating as a list constant time for enqueue & dequeue (keeping pointer to both the head and tail of the list)

Dale Roberts DYNAMICALLY LINKED STACKS AND QUEUES   NULL    top element link (a) Linked Stack   NULL    front element link (b) Linked queue rear *Figure 4.10: Linked Stack and queue (p.147)

Dale Roberts FIFO queue ADT interface template template class QUEUE { private: private: // Implementation-dependent code // Implementation-dependent code public: public: QUEUE(int); QUEUE(int); int empty(); int empty(); void put(Item); void put(Item); Item get(); Item get(); }; };

Dale Roberts FIFO queue linked-list implementation template template class QUEUE { private: private: struct node struct node { Item item; node* next; { Item item; node* next; node(Item x) node(Item x) { item = x; next = 0; } { item = x; next = 0; } }; }; typedef node *link; typedef node *link; link head, tail; link head, tail; public: public: QUEUE(int) QUEUE(int) { head = 0; } { head = 0; } int empty() const int empty() const { return head == 0; } { return head == 0; } void put(Item x) void put(Item x) { link t = tail; { link t = tail; tail = new node(x); tail = new node(x); if (head == 0) if (head == 0) head = tail; head = tail; else t->next = tail; else t->next = tail; } Item get() Item get() { Item v = head->item; link t = head->next; { Item v = head->item; link t = head->next; delete head; head = t; return v; } delete head; head = t; return v; } }; };

Dale Roberts FIFO queue array implementation template template class QUEUE { private: private: Item *q; int N, head, tail; Item *q; int N, head, tail; public: public: QUEUE(int maxN) QUEUE(int maxN) { q = new Item[maxN+1]; { q = new Item[maxN+1]; N = maxN+1; head = N; tail = 0; } N = maxN+1; head = N; tail = 0; } int empty() const int empty() const { return head % N == tail; } { return head % N == tail; } void put(Item item) void put(Item item) { q[tail++] = item; tail = tail % N; } { q[tail++] = item; tail = tail % N; } Item get() Item get() { head = head % N; return q[head++]; } { head = head % N; return q[head++]; } }; }; We can implement get and put operations for the FIFO queue ADT in constant time, using either arrays or linked-lists. If head = tail, then empty; if put would make them equal, then full. Array is 1 larger to allow checks.

Dale Roberts First-class ADT Sedgewick Definition 4.4: A first-class data type is one for which we can have potentially many different instances, and which we can assign to variables which we declare to hold the instances. A first-class data type is one for which we can have potentially many different instances, and which we can assign to variables which we declare to hold the instances. Our Fraction ADT is a first-class ADT.

Dale Roberts First-class Queue ADT template template class QUEUE { private: private: // Implementation-dependent code // Implementation-dependent code public: public: QUEUE(int); QUEUE(int); QUEUE(const QUEUE&); QUEUE(const QUEUE&); QUEUE& operator=(const QUEUE&); QUEUE& operator=(const QUEUE&); ~QUEUE(); ~QUEUE(); int empty() const; int empty() const; void put(Item); void put(Item); Item get(); Item get(); }; }; Notice how each and every interface operations now includes a references to a particular Q. We can create as many queues as we need.

Dale Roberts Acknowledgements All of this code is from Horowitz, Sahni, and Anderson-Freed, Fundamentals of Data Structures in C. Some slides were originally developed by Chen, Hsin-His.