.. 2 3  A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 24 Lists, Stacks, and Queues
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.
Stack & Queues COP 3502.
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.
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.
Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.
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.
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.
CHAPTER 7 Queues.
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.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor.
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
1 Queues and Priority Queues Chapter 8. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Stacks and Queues.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Motivation * Doubly linked lists are useful for playing video and sound files with “rewind”
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.
Stacks, Queues, and Deques
Chapter 16 Stack and Queues part2
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
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.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
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,
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
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)
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linear Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
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.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
LINKED LISTS.
 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.
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.
Programming Circular Linked List.
Review Array Array Elements Accessing array elements
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009
CS505 Data Structures and Algorithms
Chapter 15 Lists Objectives
Stacks and Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Lists.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Getting queues right … finally (?)
Presentation transcript:

.

2

3

 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting to purchase a ticket for a film.  A line of planes waiting to take off at an airport.  A line of vehicles at a toll booth.

 Difference between Stack and Queues:  Stack is last-in-first-out (LIFO)  Queue is first-in-first-out (FIFO)

 Unlike stacks in which elements are popped and pushed only at the ends of the list, Collection of data elements:  items are removed from a queue at one end, called the FRONT of the queue;  and elements are added at the other end, called the BACK

 Basic operations  Construct a queue  Check if empty  Enqueue (add element to back)  Front (retrieve value of element from front)  Dequeue (remove element from front)

 Consider an array in which to store a queue  Note additional variables needed  myFront, myBack  Picture a queue object like this

 Empty Queue Enqueue(70)

 Enqueue(80)  Enqueue(50)

 Dequeue()

 Enqueue(90)  Enqueue(60)

 Problems  We quickly "walk off the end" of the array  Possible solutions  Shift array elements  Use a circular queue  Note that both empty and full queue gives myBack == myFront

 Use Linear Array to implement a queue.  Waste of memory: The deleted elements can not be re-used.  Solution: to use circular queue.  Two implementations:  Using n-1 space.  Using n space + full tag

 Create(Q) Q: Array[0…n-1] front = rear = 0 //initialize  Enqueue(item, Q)  Queue begin rear = (rear+1) mod n rear = (rear+1) mod n; //rear moves forward; if rear = front QueueFull; // Queue is full. rear = rear-1 mod n; // rear back to the previous position; else Q[rear]=item; end; 012… n-1 R  R = (R+1) mod n

 Dequeue(Q)  item begin front=rear if front=rear QueueEmpty; else front = (front+1) mod n; item = Q[front]; end; (n-1 )  Note: only (n-1 ) space used; F R X X X XX X X X X XX X

 If an item were stored in the last position, and an Enqueure() occurred.  myBack would be incremented by 1, giving it the same value as myFront.  However, myFront == myBack indicates the queue is empty.  Thus, we cannot distinguish between empty and full.  We may avoid this situation by maintaining one empty position, so that myFront will never equal to myBack unless the queue is empty.

 Construct: Create an array, set capacity, myFront=myBack=0  Empty: test myFront==myBack  Front : if not empty: print array[myFront]

 A parameter “Tag” is introduced to help to make sure the queue is Empty or Full:  Boolean  If Tag = True, combined with other conditions => queue is Full  If Tag = False, combined with other conditions => queue is Null  “Tag” can determine the states of the queue solely!

 1. Set newBack == (myBack+1)%Queue_capacity  2. If newBack == myFront Signal “Queue Full” otherwise: Set Array[myBack] == value Set myBack == newBack

 If queue is empty signal “Queue Empty” Otherwise Set myFront=(myFront+1)%Queue_capacity

Circular Linklist

 A Circular Linked List is a special type of Linked List  It supports traversing from the end of the list to the beginning by making the last node point back to the head of the list  A Rear pointer is often used instead of a Head pointer Rear

 Circular linked lists are usually sorted  Circular linked lists are useful for playing video and sound files in “looping” mode  They are also a stepping stone to implementing graphs, an important topic in comp171

#include using namespace std; struct Node{ int data; Node* next; }; typedef Node* NodePtr;

 Nodes contain references to other nodes  Example  Issues  Easy to find succeeding elements  Start from head of list for preceding elements

 Reference-based linked list  Insertion / deletion = O(1)  Indexing = O(n)  Easy to dynamically increase size of list  Array  Insertion / deletion = O(n)  Indexing = O(1)  Compact, uses less space  Easy to copy, merge  Better cache locality

1. Original list & new element temp 2. Modify temp.next  cursor.next

3. Modify cursor.next  temp 4. Modify cursor  temp

1. Find before such that before.next = cursor 2. Modify before.next  cursor.next

3. Delete cursor 4. Modify cursor  before.next

* insertNode(NodePtr& Rear, int item) //add new node to ordered circular linked list * deleteNode(NodePtr& Rear, int item) //remove a node from circular linked list * print(NodePtr Rear) //print the Circular Linked List once

void print(NodePtr Rear){ NodePtr Cur; if(Rear != NULL){ Cur = Rear->next; do{ cout data << " "; Cur = Cur->next; }while(Cur != Rear->next); cout << endl; } Rear

 Insert into an empty list Rear 10 New NotePtr New = new Node; New->data = 10; Rear = New; Rear->next = Rear;

 Insert to head of a Circular Linked List Rear New New->next = Cur; // same as: New->next = Rear- >next; Prev->next = New; // same as: Rear->next = New; PrevCur

 Insert to middle of a Circular Linked List between Pre and Cur Prev New New->next = Cur; Prev->next = New; RearCur

 Insert to end of a Circular Linked List Rear New New->next = Cur;// same as: New->next = Rear->next; Prev->next = New;// same as: Rear->next = New; Rear = New; Prev Cur

 Delete a node from a single-node Circular Linked List Rear = Cur = Prev Rear = NULL; delete Cur; 10

 Delete the head node from a Circular Linked List Rear Cur Prev->next = Cur->next;// same as: Rear->next = Cur->next delete Cur; Prev

 Delete a middle node Cur from a Circular Linked List Prev Rear Cur Prev->next = Cur->next; delete Cur;

 Delete the end node from a Circular Linked List Rear Prev->next = Cur->next; // same as: Rear->next; delete Cur; Rear = Prev; Prev Cur

.