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

Similar documents
Queue ADT. CMPT 125 Feb. 28

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

Chapter 9 STACK, QUEUE

Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Ashish Gupta, Data JUET, Guna

Stacks and Queues. CSE 373 Data Structures Lecture 6

Stack. The Stack stores arbitrary objects Insertions and deletions follow the last-in firstout

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

SYSC 2006 CD. Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

Stacks and Queues. CSE Data Structures April 12, 2002

Matriculation number:

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Computer Science Foundation Exam

CMPSCI 377 Midterm (Sample) Name:

CMPT 125: Practice Midterm Answer Key

Programming. Lists, Stacks, Queues

.:: UNIT 4 ::. STACK AND QUEUE

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

File: /media/young/d1 (180713)/Work tive_tree_search/search_defs.h Page 1 of 1


Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Archivo: /home/young/work/cordic/binary_tree_search/search_defs.h Página 1 de 1

ECE250: Algorithms and Data Structures C++ Language Tutorial

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

8. Fundamental Data Structures

Data Structures and Algorithms for Engineers

List, Stack and Queue Implementation

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

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

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

CS24 Week 4 Lecture 2

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

15. Stacks and Queues

COMP 182 Algorithmic Thinking. Breadth-first Search. Luay Nakhleh Computer Science Rice University

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

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

Outlines: Graphs Part-2

Computer Science Foundation Exam

CS510 Concurrent Systems. Jonathan Walpole

EECS 482 Midterm (Fall 1998)

Algorithms, Data Structures, and Problem Solving

Algorithms and Data Structures

Parallelism and Data Synchronization

COP 3502 (Computer Science I) Test #3: Data Structures Date: 11/1/2013 VERSION A

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

Solution for Data Structure

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

Archivo: /home/young/work/cordic/mvr_exhaustive.c Página 1 de 6

CS 2412 Data Structures. Chapter 9 Graphs

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Data Structure Advanced

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

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

Stacks, Queues and Hierarchical Collections

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

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Lesson 8: Linked List Deque

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

Algorithms and Data Structures

DS L9: Queues

CS350 - Exam 1 (100 Points)

Abstract Data Types. Definitions, Implementations, and Uses

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

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

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

PROGRAMMAZIONE I A.A. 2017/2018

CP2 Revision. theme: dynamic datatypes & data structures

Linked Lists and other Dynamic Data Structures

Stack and Queue. Stack:

A Non-Blocking Concurrent Queue Algorithm

Lecture 3: Stacks & Queues

SYSC 2006 Winter 2012 Linear Collections: Queues

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

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

Cpt S 122 Data Structures. Data Structures

Information Science 2

CSE 100: GRAPH ALGORITHMS

Linked Lists in C and C++

CSC 1052 Algorithms & Data Structures II: Linked Queues

Lecture 9 Notes Stacks & Queues

Abstract Data Types 1

Programming Abstractions

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

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

CS 222: Linked Lists, Queues, Stacks

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

Ashish Gupta, Data JUET, Guna

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Abstract Data Types 1

Computer Programming

Tuesday Extra this week; candidates for tenure-track position Refreshments 4:00 pm in CS Commons; Talk 4:15 pm in 3821 Nicole Eikmeier, Purdue

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

ITI Introduction to Computing II

Transcription:

A queue is a special type of structure that can be used to maintain data in an organized way. This data structure is commonly implemented in one of two ways: as an array or as a linked list. In either case, the important rule is that when data is added to the queue, it is tacked onto the end, and so if an element needs to be removed, the element at the front is the only element that can legally be removed. First in, first out (FIFO)

There are only two operations that may legally be performed on a queue. Enqueue: Add a new element to the end of the queue. Dequeue: Remove the oldest element from the front of the queue.

typedef struct _queue { VALUE array[capacity]; int front; int size; } queue;

typedef struct _queue { VALUE array[capacity]; int front; int size; } queue;

typedef struct _queue { VALUE array[capacity]; int front; int size; } queue;

typedef struct _queue { VALUE array[capacity]; int front; int size; } queue;

typedef struct _queue { VALUE array[capacity]; int front; int size; } queue;

queue q;

queue q; q

queue q; q.front = 0; q.size = 0; q

queue q; q.front = 0; q.size = 0; 0 0 q

Enqueue: Add a new element to the end of the queue. In the general case, enqueue() needs to: Accept a pointer to the queue. Accept data of type VALUE to be added to the queue. Add that data to the queue at the end of the queue. Change the size of the queue.

void enqueue(queue* q, VALUE data); 0 0 q

enqueue(&q, 28); 0 0 q

enqueue(&q, 28); 28 0 0 q

enqueue(&q, 28); 28 0 1 q

enqueue(&q, 33); 28 0 1 q

enqueue(&q, 33); 28 33 0 1 q

enqueue(&q, 33); 28 33 0 2 q

enqueue(&q, 19); 28 33 0 2 q

enqueue(&q, 19); 28 33 19 0 2 q

enqueue(&q, 19); 28 33 19 0 3 q

Dequeue: Remove the most recent element from the front of the queue. In the general case, dequeue() needs to: Accept a pointer to the queue. Change the location of the front of the queue. Decrease the size of the queue. Return the value that was removed from the queue.

VALUE dequeue(queue* q); 28 33 19 0 3 q

int x = dequeue(&q); 28 33 19 0 3 q

int x = dequeue(&q); 28 x 0 3 33 19 q

int x = dequeue(&q); 28 x 28 33 19 0 3 q

int x = dequeue(&q); 28 x 28 33 19 1 2 q

int x = dequeue(&q); 28 x 28 33 19 1 2 q

int x = dequeue(&q); 33 x 28 33 19 1 2 q

int x = dequeue(&q); 33 x 28 33 19 2 1 q

enqueue(&q, 40); 28 33 19 2 1 q

enqueue(&q, 40); 28 33 19 40 2 1 q

enqueue(&q, 40); 28 33 19 40 2 2 q

Linked list-based implementation typedef struct _queue { VALUE val; struct _queue *prev; struct _queue *next; } queue;

Just make sure to always maintain pointers to the head and tail of the linked list! (probably global) To enqueue: Dynamically allocate a new node; Set its next pointer to NULL, set its prev pointer to the tail; Set the tail s next pointer to the new node; Move the tail pointer to the newly-created node.

head enqueue(tail, 10); tail 12 15 9 13

enqueue(tail, 10); head tail new 12 15 9 13

enqueue(tail, 10); head tail new 12 15 9 13 10

enqueue(tail, 10); head tail new 12 15 9 13 10

enqueue(tail, 10); head tail new 12 15 9 13 10

enqueue(tail, 10); head tail new 12 15 9 13 10

To dequeue: Traverse the linked list to its second element (if it exists); Free the head of the list; Move the head pointer to the (former) second element; Make that node s prev pointer point to NULL.

head dequeue(head); tail 12 15 9 13 10

trav Queues head dequeue(head); tail 12 15 9 13 10

trav Queues head dequeue(head); tail 12 15 9 13 10

trav Queues head dequeue(head); tail 15 9 13 10

trav Queues head dequeue(head); tail 15 9 13 10

trav Queues head dequeue(head); tail 15 9 13 10