Implementing a Queue as a Linked Structure CS 308 – Data Structures.

Slides:



Advertisements
Similar presentations
Sorted Lists CS Data Structures Sections 4.1, 4.2 & 4.3.
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
What is a Queue? A queue is a FIFO “first in, first out” structure.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Implementing an Unsorted List as a Linked Structure CS 308 – Data Structures.
Heaps CS 308 – Data Structures.
Queues.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
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.
Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
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.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Doubly Linked Lists CS 308 – Data Structures. Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info.
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.
Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Heaps and Priority Queues CS 3358 – Data Structures.
C++ Classes and Data Structures Jeffrey S. Childs
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
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,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
1 Data Structures and Algorithms Stacks and Queues.
CE 221 Data Structures and Algorithms
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
CS 302 – Data Structures Sections 3.1, 3.2, 3.4 & 3.5
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
C++ Plus Data Structures
Stack and Queue APURBO DATTA.
Chapter 5 ADTs Stack and Queue.
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
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.
Queues Jyh-Shing Roger Jang (張智星)
CSI 1340 Introduction to Computer Science II
Queues.
CSCS-200 Data Structure and Algorithms
Presentation transcript:

Implementing a Queue as a Linked Structure CS 308 – Data Structures

Implementing queues using arrays Simple implementation The size of the queue must be determined when a stack object is declared Space is wasted if we use less elements We cannot "enqueue" more elements than the array can hold

Implementing queues using linked lists Allocate memory for each new element dynamically Link the queue elements together Use two pointers, qFront and qRear, to mark the front and rear of the queue

Queue class specification // forward declaration of NodeType (like function prototype) template struct NodeType; template class QueueType { public: QueueType(); ~QueueType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType); void Dequeue(ItemType&); private: NodeType * qFront; NodeType * qRear; };

Enqueuing (non-empty queue)

Enqueuing (empty queue) We need to make qFront point to the new node also New Node newNode qFront = NULL qRear = NULL

Function Enqueue template void QueueType ::Enqueue(ItemType newItem) { NodeType * newNode; newNode = new NodeType ; newNode->info = newItem; newNode->next = NULL; if(qRear == NULL) qFront = newNode; else qRear->next = newNode; qRear = newNode; }

Dequeueing (the queue contains more than one element)

Dequeueing (the queue contains only one element) We need to reset qRear to NULL also Node qFront qRear After dequeue: qFront = NULL qRear = NULL

Function Dequeue template void QueueType ::Dequeue(ItemType& item) { NodeType * tempPtr; tempPtr = qFront; item = qFront->info; qFront = qFront->next; if(qFront == NULL) qRear = NULL; delete tempPtr; }

qRear, qFront revisited The relative positions of qFront and qRear are important!

Other Queue functions template QueueType() QueueType ::QueueType() { qFront = NULL; qRear = NULL; } template MakeEmpty() void QueueType ::MakeEmpty() { NodeType * tempPtr; while(qFront != NULL) { tempPtr = qFront; qFront = qFront->next; delete tempPtr; } qRear=NULL; }

Other Queue functions (cont.) template IsEmpty() bool QueueType ::IsEmpty() const { return(qFront == NULL); } template IsFull() bool QueueType ::IsFull() const { NodeType * ptr; ptr = new NodeType ; if(ptr == NULL) return true; else { delete ptr; return false; }

Other Queue functions (cont.) template QueueType() QueueType ::~QueueType() { MakeEmpty(); }

A circular linked queue design

Comparing queue implementations Memory requirements –Array-based implementation Assume a queue (size: 100) of strings (80 bytes each) Assume indices take 2 bytes Total memory: (80 bytes x 101 slots) + (2 bytes x 2 indexes) = 8084 bytes –Linked-list-based implementation Assume pointers take 4 bytes Total memory per node: 80 bytes + 4 bytes = 84 bytes

Comparing queue implementations (cont.)

Comparing queue implementations Memory requirements –Array-based implementation Assume a queue (size: 100) of short integers (2 bytes each) Assume indices take 2 bytes Total memory: (2 bytes x 101 slots) + (2 bytes x 2 indexes) = 206 bytes –Linked-list-based implementation Assume pointers take 4 bytes Total memory per node: 2 bytes + 4 bytes = 6 bytes (cont.)

Comparing queue implementations (cont.)

Comparing queue implementations Big-O Comparison of Queue Operations OperationArray Implementation Linked Implementation Class constructorO(1) MakeEmptyO(1)O(N) IsFullO(1) IsEmptyO(1) EnqueueO(1) DequeueO(1) DestructorO(1)O(N)

Exercises 5, 6