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

Similar documents
LECTURE 03 LINKED LIST

LECTURE 17 GRAPH TRAVERSALS

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:30-7:00

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Dynamic Data Structures

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION /5:30-7:00

LECTURE 11 TREE TRAVERSALS

Chapter 18: Stacks And Queues

Chapter 17: Linked Lists

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

Chapter 18: Stacks And Queues

Abstract Data Types 1

Pushing at the back 28/11/2018. Problem. Our linked list class

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

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

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

CSCE 110 PROGRAMMING FUNDAMENTALS

Chapter 17: Linked Lists

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

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

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION /08:30-9:50 RCH 105, RCH 110

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

Abstract Data Types 1

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

An abstract tree stores data that is hierarchically ordered. Operations that may be performed on an abstract tree include:

Queue Implementations

Data Structures (INE2011)

void insert( Type const & ) void push_front( Type const & )

1 Short Answer (7 Points Each)

Example Final Questions Instructions

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

CSCE 110 PROGRAMMING FUNDAMENTALS

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked List using a Sentinel

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

ADTs: Stacks and Queues

CS 216 Exam 1 Fall SOLUTION

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

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

CS24 Week 4 Lecture 2

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

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

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

ADTs Stack and Queue. Outline

Standard Template Library

Introduction to the C programming language

Introduction to the C programming language

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Computer Science II CSci 1200 Lecture 24 C++ Inheritance and Polymorphism

CPSC 427: Object-Oriented Programming

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

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

CSC 222: Computer Programming II. Spring 2004

EECE.3220: Data Structures Spring 2017

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Associate Professor Dr. Raed Ibraheem Hamed

We will now look at a restriction of the Abstract Sorted List that most operations to run in (1) time: the abstract priority queue.

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

Stacks and their Applications

08 STACKS DATA STRUCTURES AND ALGORITHMS IMPLEMENTATION & APPLICATIONS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

The Stack and Queue Types

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

Templates and Vectors

CS 103 Unit 12 Slides

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

1 P age DS & OOPS / UNIT II

This examination has 11 pages. Check that you have a complete paper.

Example Final Questions Instructions

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

LECTURE 18 AVL TREES

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

Data Structures and Algorithms

Data Structures. Lecture 5 : The Queues

Arrays and Linked Lists

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

Data Structure (CS301)

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

the Queue queue ADT using the STL queue designing the simulation simulation with STL queue using STL list as queue using STL vector as queue

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

LECTURE 13 BINARY TREES

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

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

13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

CPSC 427: Object-Oriented Programming

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

STACKS AND QUEUES. Problem Solving with Computers-II

Linked Lists 28/11/2018. Nodes with member functions. The need for a linked list class

CS197c: Programming in C++

Unit 4 Basic Collections

List, Stack, and Queues

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

Lecture 7. Log into Linux New documents posted to course webpage

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

Transcription:

DATA STRUCTURES AND ALGORITHMS LECTURE 08 S IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD S ABSTRACT DATA TYPE An Abstract Queue (Queue ADT) is an abstract data type that emphasizes specific operations: Uses a explicit linear ordering Insertions and removals are performed individually There are no restrictions on objects inserted into (pushed onto) the queue that object is designated the back of the queue The object designated as the front of the queue is the object which was in the queue the longest The remove operation (popping from the queue) removes the current front of the queue 2 Lecture 08 - Queues 1

S ABSTRACT DATA TYPE Also called a first-in first-out (FIFO) data structure Graphically, we may view these operations as follows: Alternative terms may be used for the four operations on a queue, including: 3 S APPLICATIONS The most common application is in client-server models Multiple clients may be requesting services from one or more servers Some clients may have to wait while the servers are busy Those clients are placed in a queue and serviced in the order of arrival Grocery stores, banks, and airport security use queues The SSH Secure Shell and SFTP are clients Most shared computer services are servers: Web, file, ftp, database, mail, printers, WOW, etc. 4 Lecture 08 - Queues 2

S IMPLEMENTATION We will look at two implementations of queues: Singly linked lists Circular arrays 5 LINKED LIST IMPLEMENTATION The desired behavior of an Abstract Stack may be reproduced by performing all operations at the head. class Single_list { public: Single_list(); ~Single_list(); int size() const; bool empty() const; Type front() const; Type back() const; Single_node<Type> *head() const; Single_node<Type> *tail() const; int count( Type const & ) const; void push_front( Type const & ); void push_back( Type const & ); Type pop_front(); int erase( Type const & ); ; 6 Lecture 08 - Queues 3

LINKED LIST IMPLEMENTATION The queue class using a singly linked list has a single private member variable: a singly linked list class Queue{ ; private: Single_list<Type> list; public: bool empty() const; Type front() const; void push( Type const & ); Type pop(); 7 LINKED LIST IMPLEMENTATION bool Queue<Type>::empty() const { return list.empty(); void Queue<Type>::push( Type const &obj ) { list.push_back( obj ); Type Queue<Type>::front() const { if ( empty() ) { throw underflow(); return list.front(); Type Queue<Type>::pop() { if ( empty() ) { throw underflow(); return list.pop_front(); 8 Lecture 08 - Queues 4

ARRAY IMPLEMENTATION A one-ended array does not allow all operations to occur Using a two-ended array, its is possible by pushing at the back and popping from the front 9 ARRAY IMPLEMENTATION We need to store an array: In C++, this is done by storing the address of the first entry Type *array; We need additional information, including: The number of objects currently in the queue and the front and back indices int queue_size; int ifront; int iback; // index of the front entry // index of the back entry The capacity of the array int array_capacity; 10 Lecture 08 - Queues 5

ARRAY IMPLEMENTATION CLASS class Queue{ private: int queue_size; int ifront; int iback; int array_capacity; Type *array; public: Queue( int = 10 ); ~Queue(); bool empty() const; Type front() const; void push( Type const & ); Type pop(); ; 11 ARRAY IMPLEMENTATION CONSTRUCTOR Before we initialize the values, we will state that iback is the index of the most-recently pushed object ifront is the index of the object at the front of the queue To push, we will increment iback and place the new item at that location To make sense of this, we will initialize iback = -1; ifront = 0; After the first push, we will increment iback to 0, place the pushed item at that location, and now 12 Lecture 08 - Queues 6

ARRAY IMPLEMENTATION CONSTRUCTOR Again, we must initialize the values We must allocate memory for the array and initialize the member variables The call to new Type[array_capacity] makes a request to the operating system for array_capacity objects #include <algorithm> //... Queue<Type>::Queue( int n ): queue_size( 0 ), iback( -1 ), ifront( 0 ), array_capacity( std::max(1, n) ), array( new Type[array_capacity] ) { // Empty constructor 13 ARRAY IMPLEMENTATION CONSTRUCTOR Reminder: Initialization is performed in the order specified in the class declaration Queue<Type>::Queue( int n ): queue_size( 0 ), iback( -1 ), ifront( 0 ), array_capacity( std::max(1, n) ), array( new Type[array_capacity] ) { // Empty constructor class Queue { private: int queue_size; int iback; int ifront; int array_capacity; Type *array; public: Queue( int = 10 ); ~Queue(); bool empty() const; Type top() const; void push( Type const & ); Type pop(); ; 14 Lecture 08 - Queues 7

ARRAY IMPLEMENTATION DESTRUCTOR The destructor is unchanged from Stack-as-Array: Queue<Type>::~Queue() { delete [] array; 15 ARRAY IMPLEMENTATION MEMBER FUNCTIONS bool Queue<Type>::empty() const { return ( queue_size == 0 ); Type Queue<Type>::front() const { if ( empty() ) { throw underflow(); return array[ifront]; 16 Lecture 08 - Queues 8

ARRAY IMPLEMENTATION MEMBER FUNCTIONS void Queue<Type>::push( Type const &obj ) { if ( queue_size == array_capacity ) { throw overflow(); ++iback; array[iback] = obj; ++queue_size; Type Queue<Type>::pop() { if ( empty() ) { throw underflow(); --queue_size; ++ifront; return array[ifront - 1]; 17 CIRCULAR CYCLIC Instead of viewing the array on the range 0,, 15, consider the indices being cyclic:, 15, 0, 1,, 15, 0, 1,, 15, 0, 1, This is referred to as a circular array 18 Lecture 08 - Queues 9

CIRCULAR CYCLIC Now, the next push may be performed in the next available location of the circular array: ++iback; if ( iback == capacity() ) { iback = 0; 19 EXCEPTIONS As with a stack, there are a number of options which can be used if the array is filled If the array is filled, we have five options: Increase the size of the array Throw an exception Ignore the element being pushed Put the pushing process to sleep until something else pops the front of the queue Include a member function bool full() 20 Lecture 08 - Queues 10

STANDARD TEMPLATE LIBRARY #include <iostream> #include <queue> using namespace std; int main() { queue <int> iqueue; iqueue.push( 13 ); iqueue.push( 42 ); cout << "Head: " << iqueue.front() << endl; iqueue.pop(); // no return value cout << "Head: " << iqueue.front() << endl; cout << "Size: " << iqueue.size() << endl; return 0; 21 SUMMARY The queue is one of the most common abstract data structures Understanding how a queue works is trivial The implementation is only slightly more difficult than that of a stack Applications include: Queuing clients in a client-server model Breadth-first traversals of trees 22 Lecture 08 - Queues 11