Queue & List Data Structures & Algorithm. 2012-20132 Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Chapter 3 Lists Dr Zeinab Eid.
Queues and Linked Lists
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
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.
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.
Doubly Linked Lists. One powerful variation of a linked list is the doubly linked list. The doubly linked list structure is one in which each node has.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
CS Data Structures II Review COSC 2006 April 14, 2017
Stacks, Queues, Linked Lists, Deques
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Queues.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
Chapter 3: Arrays, Linked Lists, and Recursion
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
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.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Queues CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
Algorithms and Data Structures Lecture VI
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Linked List, Stacks Queues
Elementary Data Structures
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Double-Ended Queues Chapter 5.
Queues Rem Collier Room A1.02
Linked List Stacks, Linked List Queues, Dequeues
EEL 4854 IT Data Structures Linked Lists
Stacks and Queues.
Queues Queues Queues.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Circular queue.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
CS212D: Data Structures Week 5-6 Linked List.
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,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
CS210- Lecture 6 Jun 13, 2005 Announcements
Stacks, Queues, and Deques
Stacks and Linked Lists
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

Queue & List Data Structures & Algorithm

Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances, with: a specific interface – a collection of signatures of methods that can be invoked on an instance, a set of axioms that define the semantics of the methods (i.e., what the methods do to instances of the ADT, but not how)

October 18, Queues A queue differs from a stack in that its insertion and removal routines follows the first-in-first-out (FIFO) principle. Elements may be inserted at any time, but only the element which has been in the queue the longest may be removed. Elements are inserted at the rear (enqueued) and removed from the front (dequeued) Front Rear Queue

October 18, Queues (2) The queue supports three fundamental methods: Enqueue(S: ADT, o: element ): ADT - Inserts object o at the rear of the queue Dequeue(S: ADT ): ADT - Removes the object from the front of the queue; an error occurs if the queue is empty Front(S: ADT ): element - Returns, but does not remove, the front element; an error occurs if the queue is empty

October 18, Queues (3) These support methods should also be defined: New(): ADT – Creates an empty queue Size(S: ADT ): integer IsEmpty(S: ADT ): boolean Axioms: Front(Enqueue(New(), v)) = v Dequeque(Enqueue(New(), v)) = New() Front(Enqueue(Enqueue(Q, w), v)) = Front(Enqueue(Q, w)) Dequeue(Enqueue(Enqueue(Q, w), v)) = Enqueue(Dequeue(Enqueue(Q, w)), v)

October 18, An Array Implementation Create a queue using an array in a circular fashion A maximum size N is specified. The queue consists of an N-element array Q and two integer variables: f, index of the front element (head – for dequeue) r, index of the element after the rear one (tail – for enqueue)

October 18, An Array Implementation (2) “wrapped around” configuration what does f=r mean?

October 18, An Array Implementation (3) Pseudo code Algorithm size() return (N-f+r) mod N Algorithm isEmpty() return (f=r) Algorithm front() if isEmpty() then return Error return Q[f] Algorithm dequeue() if isEmpty() then return Error Q[f]=null f=(f+1)modN Algorithm enqueue(o) if size() = N - 1 then return Error Q[r]=o r=(r +1)modN

9 Implementing a Queue with a Singly Linked List Nodes connected in a chain by links The head of the list is the front of the queue, the tail of the list is the rear of the queue. Why not the opposite?

October 18, Dequeue - advance head reference Inserting at the head is just as easy Linked List Implementation

October 18, Enqueue - create a new node at the tail chain it and move the tail reference How about removing at the tail? Linked List Implementation (2)

12 Implementing Deques with Doubly Linked Lists Deletions at the tail of a singly linked list cannot be done in constant time. To implement a deque, we use a doubly linked list. with special header and trailer nodes A node of a doubly linked list has a next and a prev link By using a doubly linked list, all the methods of a deque run in O(1) time.

13 Implementing Deques with Doubly Linked Lists (cont.) When implementing a doubly linked lists, we add two special nodes to the ends of the lists: the header and trailer nodes. The header node goes before the first list element. It has a valid next link but a null prev link. The trailer node goes after the last element. It has a valid prev reference but a null next reference. NOTE: the header and trailer nodes are sentinel or “dummy” nodes because they do not store elements. Here’s a diagram of our doubly linked list:

14 Implementing Deques with Doubly Linked Lists (cont.) Here’s a visualization of the code for removeLast().

October 18, Double-Ended Queue A double-ended queue, or deque, supports insertion and deletion from the front and back The deque supports six fundamental methods InsertFirst(S: ADT, o: element ): ADT - Inserts e at the beginning of deque InsertLast(S: ADT, o: element ): ADT - Inserts e at end of deque RemoveFirst(S: ADT ): ADT – Removes the first element RemoveLast(S: ADT ): ADT – Removes the last element First(S: ADT ): element and Last(S: ADT ): element – Returns the first and the last elements

October 18, Stacks with Deques Implementing ADTs using implementations of other ADTs as building blocks Stack MethodDeque Implementation size() isEmpty() top()last() push(o)insertLast(o) pop()removeLast()

October 18, Queues with Deques Queue MethodDeque Implementation size() isEmpty() front()first() enqueue(o)insertLast(o) dequeue()removeFirst()

18 The Adaptor Pattern Using a deque to implement a stack or queue is an example of the adaptor pattern. Adaptor patterns implement a class by using methods of another class In general, adaptor classes specialize general classes Two such applications: Specialize a general class by changing some methods. Ex: implementing a stack with a deque. Specialize the types of objects used by a general class. Ex: Defining an IntegerArrayStack class that adapts ArrayStack to only store integers.

October 18, Circular Lists No end and no beginning of the list, only one pointer as an entry point Circular doubly linked list with a sentinel is an elegant implementation of a stack or a queue