Circular Queue can be created in three ways they are: Using single linked list Using double linked list Using arrays

Similar documents
Data Structures II Lesson 1 (Circular Queue)

The Queues. Front. Rear. Front. Rear. Rear = 0 Front = 0. Fig Queue is empty. Fig push(10) Rear = 1 Front = 0. Fig. 4.3.


Data Structures. Lecture 5 : The Queues

Stack and Queue. Stack:

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

ITI Introduction to Computing II

Data Structures. Lecture 4 : The Queues. Dr. Essam Halim Houssein Lecturer, Faculty of Computers and Informatics, Benha University

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Data Structure Advanced

.:: UNIT 4 ::. STACK AND QUEUE

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Queue rear tail front head FIFO

1 P age DS & OOPS / UNIT II

Ashish Gupta, Data JUET, Guna

Data Structures and Algorithms for Engineers

Linked List. April 2, 2007 Programming and Data Structure 1

Insert. SEMCOM Page 1 of 11

Queue: Queue Representation: Basic Operations:

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

CSC 1052 Algorithms & Data Structures II: Linked Queues

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Queue Definition. Ordered list with property:

1. Introduction. Lecture Content

CSE 214 Computer Science II Heaps and Priority Queues

Data Structure. Recitation VII

Introduction to Data Structures and Algorithms

LECTURE OBJECTIVES 6-2

CSEN 301 Data Structures and Algorithms

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

Part I: Short Answer (12 questions, 65 points total)

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Lecture Data Structure Stack

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Queues. Queue ADT Queue Implementation Priority Queues

CSCE 110 PROGRAMMING FUNDAMENTALS

DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

Linked List using a Sentinel

Introduction to Computing II (ITI 1121) Final Examination

Example Final Questions Instructions

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

Linked List Tree Graph. Stack, Queue etc.

UNIT-2 Stack & Queue

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

03/29/2004. A dispenser has three essential features: adding, removing, accessing. Dispensers

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

Introduction to the C programming language

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Introduction to the C programming language

CS 216 Exam 1 Fall SOLUTION

Introduction to Computing II (ITI 1121) Final Examination

Queue with Array Implementation

Name CPTR246 Spring '17 (100 total points) Exam 3

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

CMSC Introduction to Algorithms Spring 2012 Lecture 7

SYSC 2006 Winter 2012 Linear Collections: Queues

10 MCQ of Data Structure Paper-2(CS/IT) For NIC SSC Scientific Assistant IBPS IT Officer Exam 2017

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.


LIFO : Last In First Out

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Information Science 2

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Data Structures Lab II. Binary Search Tree implementation

CHAPTER 5. QUEUE v2 by

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

CE204 Data Structures and Algorithms Part 2

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Dynamic Data Structures

CSCE 110 PROGRAMMING FUNDAMENTALS

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

1 Short Answer (7 Points Each)

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

DS Assignment II. Full Sized Image

Lecture 4 Stack and Queue

Data Structures And Algorithms

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

Separate Compilation and Namespaces Week Fall. Computer Programming for Engineers

CSE 100: GRAPH ALGORITHMS

Basically queue is nothing but an array or a vector with a maximum capacity of size. Front=1 Front=1 REAR=2. Front=1 REAR=3 Front=1 REAR=4 Q is Full

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

CSE 373 Spring Midterm. Friday April 21st

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

No Aids Allowed. Do not turn this page until you have received the signal to start.

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Transcription:

Circular Queue: Implementation and Applications In linear queue, when we delete any element, only front increment by one but position is not used later. So, when we perform more add and delete operations, memory wastage increases. But in circular queue, memory is utilized if we delete any element that position is used later due to its circular representation. In a standard queue data structure re-buffering problem occurs for each dequeue operation. To solve this problem by joining the front and rear ends of a queue to make the queue as a circular queue Circular queue is a linear data structure. It follows FIFO principle. In circular queue the last node is connected back to the first node to make a circle. Circular linked list fallow the First In First Out principle Elements are added at the rear end and the elements are deleted at front end of the queue Both the front and the rear pointers points to the beginning of the array. It is also called as Ring buffer. Items can inserted and deleted from a queue in O(1) time. Circular Queue can be created in three ways they are: Using single linked list Using double linked list Using arrays Using single linked list: It is an extension for the basic single linked list. In circular linked list Instead of storing a Null value in the last node of a single linked list, store the address of the 1st node (root) forms a circular linked list. Using circular linked list it is possible to directly traverse to the first node after reaching the last node. The following figure shows circular single linked list: Algorithm for Circular Queue: Declare and initialize necessary variables such as head = 0, tail = 0 etc. For enqueue operation, If head = (tail+1)%maxsize

print "Queue is Full" queue[tail] = item tail = (tail+1)%maxsize For enqueue of next data item, goto step 2 For dequeue operation, If head = tail print "Queue is Empty" Remove item i.e. item = queue[head] Set head = (head + 1)%MAXSIZE For next dequeue operation, goto step 4 Stop Source Code for Circular Queue: * C++ Program to Implement Circular Queue #include <iostream> #define MAX 5 using namespace std; * Class Circular Queue.class Circular_Queue private: int *cqueue_arr; int front, rear; public: Circular_Queue() cqueue_arr = new int [MAX]; rear = front = -1; * Insert into Circular Queue void insert(int item) if ((front == 0 && rear == MAX-1) (front == rear+1)) cout<<"queue Overflow \n"; return;

if (front == -1) front = 0; rear = 0; if (rear == MAX - 1) rear = 0; else rear = rear + 1; cqueue_arr[rear] = item ; * Delete from Circular Queue * void del() if (front == -1) cout<<"queue Underflow\n"; return ; cout<<"element deleted from queue is : "<<cqueue_arr[front]<<endl; if (front == rear) front = -1; rear = -1; if (front == MAX - 1) front = 0; else front = front + 1; * Display Circular Queue void display() int front_pos = front, rear_pos = rear; if (front == -1)

cout<<"queue is empty\n"; return; cout<<"queue elements :\n"; if (front_pos <= rear_pos) while (front_pos <= rear_pos) cout<<cqueue_arr[front_pos]<<" "; front_pos++; while (front_pos <= MAX - 1) cout<<cqueue_arr[front_pos]<<" "; front_pos++; front_pos = 0; while (front_pos <= rear_pos) cout<<cqueue_arr[front_pos]<<" "; front_pos++; cout<<endl; ; * Main int main() int choice, item; Circular_Queue cq; Do cout<<"1.insert\n"; cout<<"2.delete\n"; cout<<"3.display\n"; cout<<"4.quit\n"; cout<<"enter your choice : "; cin>>choice; switch(choice)

case 1: cout<<"input the element for insertion in queue : "; cin>>item; cq.insert(item); case 2: cq.del(); case 3: cq.display(); case 4: default: cout<<"wrong choice\n"; End of switch while(choice!=4); return0; Input the element for insertion in queue: 3 Input the element for insertion in queue: 2 Input the element for insertion in queue: 6 Input the element for insertion in queue: 4 Input the element for insertion in queue: 1 Enter your choice: 3 Queue elements: 3 2 6 4 1

Enter your choice: 2 Element deleted from queue is: 3 Enter your choice: 3 Queue elements: 2 6 4 1 Enter your choice: 4 Applications: Adding large integers: Large integers can be added very easily using circular queues. Here the rightmost digit is placed in the front node and leftmost digit is placed in the rear node. Memory management: The unused memory locations in the case of ordinary queues can be utilized in circular queues. Computer controlled traffic system: In computer controlled traffic system, circular queues are used to switch on the traffic lights one by one repeatedly as per the time set.