LINKED QUEUES P. 412 - 425. LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

DATA STRUCTURES USING C++ Chapter 5
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.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
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.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made 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.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
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.
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.
Queue Overview Queue ADT Basic operations of queue
Jerry Lebowitz.  Stacks  Queues 3 C++ Programming: From Problem Analysis to Program Design, Sixth Edition.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.
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.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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,
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.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Review of Lists, Stacks, and Queues CS 400/600 – Data Structures.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Linked List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
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.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
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.
CS505 Data Structures and Algorithms
Data Structure #1: The Array
Doubly Linked List Review - We are writing this code
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Algorithms and Data Structures
CMSC 341 Lecture 5 Stacks, Queues
Chapter 16-2 Linked Structures
Linked List (Part I) Data structure.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
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 Definition of a Queue Examples of Queues
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

LINKED QUEUES P

LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working on a list, which is limiting in its size again, the problem with the previous example of queues is that we are working on a list, which is limiting in its size using circular array can be inefficient if too many adds made before deletes (items overwritten) using circular array can be inefficient if too many adds made before deletes (items overwritten) just as with stacks and lists, we can link the queue to take advantage of virtual unlimited memory just as with stacks and lists, we can link the queue to take advantage of virtual unlimited memory but we lose dynamic access of queue elements but we lose dynamic access of queue elements 1) still need two indexes (represented as pointers now): 1) still need two indexes (represented as pointers now): front: where we take from the list front: where we take from the list back: where we add to the list back: where we add to the list 2) also, a number to keep track of the current size of list 2) also, a number to keep track of the current size of list

INTRO TO QUEUES (cont.) 4) when first value added, front and rear point to same place 4) when first value added, front and rear point to same place 5) each additional add: 5) each additional add: rear marker points to next item rear marker points to next item 6) each remove: 6) each remove: front marker points to previous item front marker points to previous item

Conceptual Design of a Queue so, if we design the Linked Queue as an object: so, if we design the Linked Queue as an object: LinkedQueue Object: Knows: pointer to space in memory containing the list the front the front the rear the rear Can Do: Retrieve an item (take out of front of queue) Add an item (add to end of queue) Add an item (add to end of queue) Tell which item is in front (without removing) Tell which item is in front (without removing) Clear the queue Clear the queue Tell if the queue is empty Tell if the queue is empty

Linked Queue Implementation typedef int Item; typedef Item * ItemPtr; class LinkedQueue { public:LinkedQueue(); Item Remove(); Item Remove(); Item First(); Item First(); int Size(); int Size(); void Insert(const Item &anItem); void Insert(const Item &anItem); bool IsEmpty() const; bool IsEmpty() const; void Clear(); void Clear();private: Node *Front; Node *Front; Node *Rear; Node *Rear; int total; int total;};

MORE MEMBER FUNCTIONS - The Default Constructor: - sets the Front and Rear pointers to point to the null address (nothing in the queue), and sets total to 0 LinkedQueue :: LinkedQueue() { Front = 0; Front = 0; Rear = 0; Rear = 0; total = 0; total = 0; } The IsFull Function - similar to linked stack, will return false always (dynamic memory) bool LinkedQueue :: IsFull() const { bool LinkedQueue :: IsFull() const { return (false); return (false);} The IsEmpty Function - will return true if the queue is empty (no items in it); bool LinkedQueue :: IsEmpty() const { bool LinkedQueue :: IsEmpty() const { return (Total == 0); return (Total == 0);}

MORE MEMBER FUNCTIONS The Size Function - will return the number of items currently in queue The Size Function - will return the number of items currently in queue int LinkedQueue :: Size() const { return(Total); return(Total);} The Insert Function - takes an item as input and adds it to the queue; then Front and Rear pointers are reset accordingly void LinkedQueue :: Insert(const Item &anItem) { Node *temp; temp = new Node; Node *temp; temp = new Node; temp->data = anItem; temp->link = 0; temp->data = anItem; temp->link = 0; if (isempty()) { if (isempty()) { Front = temp; Front = temp; Rear = temp; Rear = temp; } else { else { Rear->link = temp; Rear->link = temp; Rear = temp; Rear = temp; } total++; total++;}

MORE MEMBER FUNCTIONS Item LinkedQueue :: Remove() { Item value; Node *Front_Item; Item value; Node *Front_Item; if (IsEmpty()) { if (IsEmpty()) { cout << "Cannot remove - queue empty" << endl; cout << "Cannot remove - queue empty" << endl; return –999; return –999; } else { else { Front_Item = Front; Front_Item = Front; value = Front->data; value = Front->data; Front = Front->link; Front = Front->link; if (Front == 0) if (Front == 0) Rear = 0; Rear = 0; delete Front_Item; delete Front_Item; } total--; total--; return value; return value;}

AN EXAMPLE OF USING THIS LINKED QUEUE int main() { LinkedQueue TheQueue; int number; int count = 1; do { cout << "enter a number to add to the queue: "; cin >> number; TheQueue.Insert(number); } while (count++ < 5); int number_in_queue = TheQueue.Size(); int value_in_queue, evencout = 0; for (int i = 1; i <= number_in_queue; i++) { value_in_queue = TheQueue.Remove(); if (value_in_queue % 2 == 0) evencout++;} cout << "Total even numbers in queue: " << evencount << endl; }

QUESTIONS? Practice Test Posted Today Practice Test Posted Today TEST #2 – Fri. July 14 TEST #2 – Fri. July 14 Covers: Covers: Linked lists Linked lists Stacks Stacks Linked stacks Linked stacks Queues Queues Linked queues Linked queues STUDY SESSION ONLINE (Fri. July 12, 7 – 9 am) STUDY SESSION ONLINE (Fri. July 12, 7 – 9 am)