Queue: Queue Representation: Basic Operations:

Similar documents
UNIT-2 Stack & Queue

Lecture Data Structure Stack

Introduction to Data Structures and Algorithms

Abstract Data Type: Stack


Graphs: Graph Data Structure:

UNIT 3: QUEUE Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

DATA STRUCTURES USING C

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Stack and Queue. Stack:

LIFO : Last In First Out

Lecture 4 Stack and Queue

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

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

An Introduction to Queues With Examples in C++

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

ITI Introduction to Computing II

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

CSC 1052 Algorithms & Data Structures II: Linked Queues

Linked List Tree Graph. Stack, Queue etc.

Data Structures & Algorithms Quick Guide. Data Structures & Algorithms Overview

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

Data Structures. Lecture 5 : The Queues

Ashish Gupta, Data JUET, Guna

CSEN 301 Data Structures and Algorithms

Chapter 18: Stacks And Queues

Data Structure - Stack and Queue-

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

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

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

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Data Structures II Lesson 1 (Circular Queue)

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

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Chapter 18: Stacks And Queues

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 16 EXAMINATION Model Answer Subject Code:

Two abstract data types. performed on them) Stacks Last In First Out (LIFO) Queues First In First Out (FIFO)


DS Assignment II. Full Sized Image

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

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

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Wipro Elite NLTH Coding / Programming Questions

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

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

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

BBM 201 DATA STRUCTURES

1 P age DS & OOPS / UNIT II

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Data Structures and Algorithms for Engineers

ADTs: Stacks and Queues

Model Solution for QP CODE : ( 3 Hours )

CSE 214 Computer Science II Heaps and Priority Queues

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

Announcements Queues. Queues

UNIT VI. STACKS AND QUEUES

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

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

CSCD 326 Data Structures I Queues

Queues. CITS2200 Data Structures and Algorithms. Topic 5

CSC 1052 Algorithms & Data Structures II: Queues

Associate Professor Dr. Raed Ibraheem Hamed

1. Introduction. Lecture Content

BBM 201 DATA STRUCTURES

Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan.

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

A queue is a linear collection whose elements are added on one end and removed from the other

MODULE V: POINTERS & PREPROCESSORS

Lecture 3: Stacks & Queues

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CS PROGRAMMING & DATA STRUCTURES. UNIT I Part - A. 2. What is the difference between if and while statement?

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

Stack & Queue on Self-Referencing Structures

Queue with Array Implementation

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

CMSC 132: Object-Oriented Programming II. Stack and Queue

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

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Programming. Lists, Stacks, Queues

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Stacks and Queues. CSE 373 Data Structures Lecture 6

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Stack & Queue on Self-Referencing Structures

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

School of Electrical Engineering & Computer Science Oregon State University. CS Recitation 3 Spring 2012

Unit 4: Stacks and Queues

Chapter 9 STACK, 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

Linear Data Structure

BBM 201 DATA STRUCTURES

CSCI 2132 Software Development. Lecture 20: Program Organization

Linked Structures. See Section 3.2 of the text.

List, Stack and Queue Implementation

BBM 201 DATA STRUCTURES

Largest Online Community of VU Students

Queue. Data Structure

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

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

Transcription:

Queue: Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first. A real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits first. More real-world examples can be seen as queues at the ticket windows and bus-stops. Queue Representation: As we now understand that in queue, we access both ends for different reasons. The following diagram given below tries to explain queue representation as data structure As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures. For the sake of simplicity, we shall implement queues using one-dimensional array. Basic Operations: Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. Here we shall try to understand the basic operations associated with queues enqueue() add (store) an item to the queue. dequeue() remove (access) an item from the queue. 1

Few more functions are required to make the above-mentioned queue operation efficient. These are peek() Gets the element at the front of the queue without removing it. isfull() Checks if the queue is full. isempty() Checks if the queue is empty. In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or storing) data in the queue we take help of rear pointer. Let's first learn about supportive functions of a queue peek(): This function helps to see the data at the front of the queue. The algorithm of peek() function is as follows begin procedure peek return queue[front] Implementation of peek() function in C programming language int peek() { return queue[front]; isfull(): As we are using single dimension array to implement queue, we just check for the rear pointer to reach at MAXSIZE to determine that the queue is full. In case we maintain the queue in a circular linked-list, the algorithm will differ. Algorithm of isfull() function begin procedure isfull if rear equals to MAXSIZE else return true 2

endif return false Implementation of isfull() function in C programming language bool isfull() { if(rear == MAXSIZE - 1) return true; else return false; isempty(): Algorithm of isempty() function begin procedure isempty if front is less than MIN OR front is greater than rear return true else return false endif If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence empty. Here's the C programming code bool isempty() { if(front < 0 front > rear) else return true; return false; 3

Enqueue Operation: Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively difficult to implement than that of stacks. The following steps should be taken to enqueue (insert) data into a queue Step 1 Check if the queue is full. Step 2 If the queue is full, produce overflow error and exit. Step 3 If the queue is not full, increment rear pointer to point the next empty space. Step 4 Add data element to the queue location, where the rear is pointing. Step 5 Return success. Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseen situations. 4

Algorithm for enqueue Operation: procedure enqueue(data) if queue is full return overflow endif rear rear + 1 queue[rear] data return true Implementation of enqueue() in C programming language int enqueue(int data) if(isfull()) return 0; rear = rear + 1; queue[rear] = data; return 1; Dequeue Operation: Accessing data from the queue is a process of two tasks access the data where front is pointing and remove the data after access. The following steps are taken to perform dequeue operation Step 1 Check if the queue is empty. Step 2 If the queue is empty, produce underflow error and exit. Step 3 If the queue is not empty, access the data where front is pointing. Step 4 Increment front pointer to point to the next available data element. Step 5 Return success. 5

Algorithm for dequeue Operation: procedure dequeue if queue is empty return underflow end if data = queue[front] front front + 1 return true Implementation of dequeue() in C programming language int dequeue() { if(isempty()) return 0; int data = queue[front]; front = front + 1; return data; 6

Queue Program in C: Implementation in C: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #define MAX 6 int intarray[max]; int front = 0; int rear = -1; int itemcount = 0; int peek(){ return intarray[front]; bool isempty(){ return itemcount == 0; bool isfull(){ return itemcount == MAX; int size(){ return itemcount; void insert(int data){ if(!isfull()){ 7

if(rear == MAX- 1){ rear = -1; intarray[++rear] = data; itemcount++; int removedata(){ int data = intarray[front++]; if(front == MAX){ front = 0; itemcount--; return data; int main() { /* insert 5 items */ insert(3); insert(5); insert(9); insert(1); insert(12); // front : 0 // rear : 4 // ------------------ // index : 0 1 2 3 4 // ------------------ // queue : 3 5 9 1 12 insert(15); // front : 0 // rear : 5 8

// --------------------- // index : 0 1 2 3 4 5 // --------------------- // queue : 3 5 9 1 12 15 if(isfull()){ printf("queue is full!\n"); // remove one item int num = removedata(); printf("element removed: %d\n",num); // front : 1 // rear : 5 // ------------------- // index : 1 2 3 4 5 // ------------------- // queue : 5 9 1 12 15 // insert more items insert(16); // front : 1 // rear : -1 // ---------------------- // index : 0 1 2 3 4 5 // ---------------------- // queue : 16 5 9 1 12 15 // As queue is full, elements will not be inserted. insert(17); insert(18); // ---------------------- // index : 0 1 2 3 4 5 // ---------------------- 9

// queue : 16 5 9 1 12 15 printf("element at front: %d\n",peek()); printf("----------------------\n"); printf("index : 5 4 3 2 1 0\n"); printf("----------------------\n"); printf("queue: "); while(!isempty()){ int n = removedata(); printf("%d ",n); If we compile and run the above program, it will produce the following result Queue is full! Element removed: 3 Element at front: 5 ---------------------- index : 5 4 3 2 1 0 ---------------------- Queue: 5 9 1 12 15 16 References: 1. Data tructures & Algorith, Tutorials Poit Idia https://www.tutorialspoit.co 10