DATA STRUCTURE - QUEUE



Similar documents
DATA STRUCTURE - STACK

Programming with Data Structures

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Chapter 3: Restricted Structures Page 1

Application of Stacks: Postfix Expressions Calculator (cont d.)

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Data Structures and Algorithms Stacks and Queues

CmpSci 187: Programming with Data Structures Spring 2015

Module 2 Stacks and Queues: Abstract Data Types

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

St S a t ck a ck nd Qu Q eue 1

Common Data Structures

Linked Lists Linked Lists, Queues, and Stacks

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Analysis of a Search Algorithm

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

STACKS,QUEUES, AND LINKED LISTS

PES Institute of Technology-BSC QUESTION BANK

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

10CS35: Data Structures Using C

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Universidad Carlos III de Madrid

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Basic Data Structures and Algorithms

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Stacks. Linear data structures

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Data Structures and Algorithms Lists

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Lecture Notes on Stacks & Queues

Data Structures UNIT III. Model Question Answer

DATA STRUCTURES USING C

CompSci-61B, Data Structures Final Exam

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Cpt S 223. School of EECS, WSU

C++ INTERVIEW QUESTIONS

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

Sequential Data Structures

Evolving Data Structures with Genetic Programming

Introduction to Data Structures and Algorithms

G. H. RAISONI COLLEGE OF ENGG NAGPUR-16 Session DEPARTMENT CSE Semester IV SUBJECT DSPD

EECE 276 Embedded Systems

Algorithms and Data Structures Written Exam Proposed SOLUTION

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Data Structures and Algorithms

Data Structures and Algorithms

Data Structures in the Java API

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

CS 2412 Data Structures. Chapter 3 Queues

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Data Structures Fibonacci Heaps, Amortized Analysis

Data Structures and Algorithms C++ Implementation

Node-Based Structures Linked Lists: Implementation

3 Abstract Data Types and Search

CSE 211: Data Structures Lecture Notes VII

Lecture Notes on Linear Search

1 Description of The Simpletron

Abstract Data Types. Chapter 2

14 Stacks, Queues, And Linked Lists

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Algorithms and Data Structures

COMPUTER SCIENCE. Paper 1 (THEORY)

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

6. Standard Algorithms

Lecture Notes on Binary Search Trees

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

What Is Recursion? Recursion. Binary search example postponed to end of lecture

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

! stack, queue, priority queue, dictionary, sequence, set e.g., a Stack is a list implements a LIFO policy on additions/deletions.

Lecture 2: Data Structures Steven Skiena. skiena

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

LINEAR INEQUALITIES. less than, < 2x + 5 x 3 less than or equal to, greater than, > 3x 2 x 6 greater than or equal to,

STACK Data Structure:

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

7.1 Our Current Model

Transcription:

DATA STRUCTURE - QUEUE http://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm Copyright tutorialspoint.com Queue is an abstract data structure, somewhat similar to stack. In contrast to stack, queue is opened at both end. 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 example can be seen as queues at ticket windows & bus-stops. Queue Representation As we now understand that in queue, we access both ends for different reasons, a diagram given below tries to explain queue representation as data structure Same as stack, queue can also be implemented using Array, Linked-list, Pointer and Structures. For the sake of simplicity we shall implement queue using one-dimensional array. Basic Operations Queue operations may involve initializing or defining the queue, utilizing it and then completing erasing it from memory. Here we shall try to understand basic operations associated with queues enqueue add store an item to the queue. dequeue remove access an item from the queue. Few more functions are required to make above mentioned queue operation efficient. These are peek get the element at front of the queue without removing it. isfull checks if queue is full. isempty checks if queue is empty. In queue, we always dequeue oraccess data, pointed by front pointer and while enqueing orstoring data in queue we take help of rear pointer. Let's first learn about supportive functions of a queue

peek Like stacks, this function helps to see the data at the front of the queue. Algorithm of peek function 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 queue is full. In case we maintain queue in a circular linkedlist, the algorithm will differ. Algorithm of isfull function begin procedure isfull if rear equals to MAXSIZE return false Implementation of isfull function in C programming language bool isfull() { if(rear == MAXSIZE - 1) ; return false; isempty Algorithm of isempty function begin procedure isempty if front is less than MIN OR front is greater than rear return false If value of front is less than MIN or 0, it tells that queue is not yet initialized, hence empty. Here's the C programming code bool isempty() { if(front < 0 front > rear) ;

return false; Enqueue Operation As queue maintains two data pointers, front and rear, its operations are comparatively more difficult to implement than stack. The following steps should be taken to enqueue insert data into a queue Step 1 Check if queue is full. Step 2 If queue is full, produce overflow error and exit. Step 3 If queue is not full, increment rear pointer to point next empty space. Step 4 Add data element to the queue location, where rear is pointing. Step 5 return success. Sometimes, we also check that if queue is initialized or not to handle any unforeseen situations. Algorithm for enqueue operation procedure enqueue(data) if queue is full return overflow rear rear + 1 queue[rear] data 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 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 queue is empty. Step 2 If queue is empty, produce underflow error and exit. Step 3 If queue is not empty, access data where front is pointing. Step 3 Increment front pointer to point next available data element. Step 5 return success. Algorithm for dequeue operation procedure dequeue if queue is empty return underflow end if data = queue[front] front front - 1 Implementation of dequeue in C programming language int dequeue() { if(isempty()) return 0; int data = queue[front]; front = front + 1; return data;

For a complete stack program in C programming language, please click here. Loading [MathJax]/jax/output/HTML-CSS/jax.js