Queue C and Data Structures Baojian Hua

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

Stacks, Queues, and Linked Lists
C and Data Structures Baojian Hua
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
Linked List Variations
Data Structure Lecture-5
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structure & Abstract Data Type
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Extensible Array C and Data Structures Baojian Hua
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
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 are queues? Queue Implementation Using Linked Lists. Applications of Queues.
Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Stack C and Data Structures Baojian Hua
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Binary Search Tree C and Data Structures Baojian Hua
Queues.
Relation Discrete Mathematics and Its Applications Baojian Hua
String C and Data Structures Baojian Hua
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Graph C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
C and Data Structures Baojian Hua
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queue C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Linked List C and Data Structures Baojian Hua
Tree C and Data Structures Baojian Hua
Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Functional List C and Data Structures Baojian Hua
String C and Data Structures Baojian Hua
Graph Traversal Discrete Mathematics and Its Applications Baojian Hua
Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Graph Discrete Mathematics and Its Applications Baojian Hua
Hash C and Data Structure Baojian Hua
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.
Binary Search Tree C and Data Structures Baojian Hua
Data Structures - Queues
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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’
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.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
The Queue Data Structure Mugurel Ionu Andreica Spring 2012.
Stacks And Queues Chapter 18.
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.
Hash C and Data Structure Baojian Hua
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Extensible Tree Discrete Mathematics and Its Applications Baojian Hua
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Implementing Queues Eric Roberts CS 106B February 13, 2013.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
CS505 Data Structures and Algorithms
QueueStack CS1020.
Queue data structure.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 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.
Discrete Mathematics and
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Discrete Mathematics and
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Presentation transcript:

Queue C and Data Structures Baojian Hua

Linear List Recall that a linear list has the form: The delete and insert operations may insert or delete an arbitrary element e_i If the delete is restricted at one end and insert at the other end, we get a queue

Example: Queue of Char ‘a’‘a’ insert ‘b’‘b’ ‘a’‘a’ ‘b’‘b’ ‘a’‘a’ ‘c’‘c’ delete ‘b’‘b’ ‘c’‘c’

Abstract Data Types in C: Interface // in file “queue.h” #ifndef QUEUE_H #define QUEUE_H typedef struct queue *queue; queue new (); int size (queue q); int isEmpty (queue q); void enQueue (queue q, poly x); poly deQueue (queue q); poly getHead (queue q); #endif

Implementation Using Extensible Array // in file “arrayQueue.c” #include “queue.h” struct queue { arrayList l; }; // Recall the box strategy: l q

Operations: “ new ” queue new () { queue q = checkedMalloc (sizeof (*q)); q->l = newArrayList (); return q; } 0 n-1 array max tail l q

Operations: “ size ” int size (queue q) { return arrayListLength (q->l); } 0 n-1 array max tail l q

Operations: “ isEmpty ” int isEmpty (queue q) { return arrayListIsEmpty (q->l); } 0 n-1 array max tail l q

Operations: “ enQueue ” void enQueue (queue q, poly x) { arrayListInsertLast (stk->l, x); return; } 0 n-1 array max tail l q

Operations: “ deQueue ” poly deQueue (queue q) { if (! arrayListIsEmpty (q->l)) return arrayListDeleteFirst (q->l); error (“empty queue”); return NULL; }

Operations: “ deQueue ” 0 n-1 array max tail l q

Operations: “ deQueue ” 0 n-1 array max tail l q

Analysis What ’ s the complexity? enQueue: O(1) deQueue: O(n) data movement Can we do better? Lazy approach better amortized performance Circular queue

Lazy Approach Instead of moving data when “ deQueue ”, we move data only when reaching the tail of the queue O(n) on n operations which has O(1) amortized cost

Lazy Approach What ’ s necessary modification? Leave this as a programming assignment 0 n-1 array max tail l q

Circular Queue A refinement of the lazy approach is the circular queue 0 n-1 array max tail l q The formula: tail = (tail+1)%max; head = (head+1)%max; head

Implementation Using Linked List // in file “linkedQueue.c” #include “queue.h” struct queue { linkedList l; }; l q data next data next data next …

Operations: “ new ” queue new () { queue q = checkedMalloc (sizeof (*q)); q->l = newLinkedList (); return q; } l q /\

Operations: “ size ” int size (queue q) { return linkedListLength (q->l); } l q data next data next data next …

Operations: “ isEmpty ” int isEmpty (queue q) { return linkedListIsEmpty (q->l); } l q data next data next data next …

Operations: “ enQueue ” void enQueue (queue q, poly x) { // note the difference with extensible array- // based representation linkedListInsertLast (q->l, x); return; } l q data next data next data next …

Operations: “ deQueue ” poly deQueue (queue) { if (! linkedListIsEmpty (q->l)) return linkedListDeleteFirst (q->l); error (“empty queue”); return NULL; } l q data next data next data next …

Analysis What ’ s the complexity of these operations? enQueue: O(n) search the last element deQueue: O(1) Improvement: Circular linked list leave as programming assignment