CS302 - Data Structures using C++

Similar documents
CS302 - Data Structures using C++

CS302 - Data Structures using C++

CS302 - Data Structures using C++

CS302 - Data Structures using C++

CSCD 326 Data Structures I Queues

CS302 Data Structures using C++

CS302 - Data Structures using C++

Chapter 18: Stacks And Queues

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

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

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

Announcements Queues. Queues

CS302 - Data Structures using C++

Chapter 18: Stacks And Queues

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

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

The Abstract Data Type Queue. Module 8. The Abstract Data Type Queue. The Abstract Data Type Queue. The Abstract Data Type Queue

An Introduction to Queues With Examples in C++

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

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

CS302 - Data Structures using C++

Data Structures and Algorithms for Engineers

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

ADTs Stack and Queue. Outline

Queue with Array Implementation

1 P age DS & OOPS / UNIT II

Queue Linked List Implementation

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

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

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

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Lab 2: ADT Design & Implementation

Another common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties

CSC 273 Data Structures

Queues. Queue ADT Queue Implementation Priority Queues

Queue rear tail front head FIFO

CSCE 110 PROGRAMMING FUNDAMENTALS

Object Oriented Programming COP3330 / CGS5409

ITI Introduction to Computing II

Class and Function Templates

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

Chapter 17: Linked Lists

Motivation for Templates. Class and Function Templates. One Way to Look at Templates...

Data Structures (INE2011)

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

CS302 - Data Structures using C++

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

ADT Sorted List Operations

Queues Fall 2018 Margaret Reid-Miller

CS24 Week 4 Lecture 2

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)

Stack and Queue. Stack:

Queues. Virtuelle Fachhochschule. Prof. Dr. Debora Weber-Wulff

LECTURE OBJECTIVES 6-2

Data Structure. Recitation VII

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

CSC 1052 Algorithms & Data Structures II: Linked Queues

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

SYSC 2006 Winter 2012 Linear Collections: Queues

ADTs Stack and Queue. Outline

csci 210: Data Structures Stacks and Queues

Announcements. mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1)

Your Topic Goes Here Queue

Data Structures and Algorithms

EECE.3220: Data Structures Spring 2017

CSC 1052 Algorithms & Data Structures II: Queues

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

CSCE 110 PROGRAMMING FUNDAMENTALS

CE204 Data Structures and Algorithms Part 2

Queues. CITS2200 Data Structures and Algorithms. Topic 5

CSC212:Data Structure

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

CSE 214 Computer Science II Heaps and Priority Queues

Link-Based Implementations. Chapter 4

Queue Definition. Ordered list with property:

CS302 - Data Structures using C++

CS302 - Data Structures using C++

Stacks and Queues III

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Lists, Stacks, and Queues

CMSC 341. Deques, Stacks and Queues 9/22/04 1

Chapter 5. ADTs Stack and Queue

Ashish Gupta, Data JUET, Guna

1. Introduction. Lecture Content

Stacks and Queues. Chapter 8 (Cont.)

What is a List? elements, with a linear relationship. first) has a unique predecessor, and. unique successor.

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

Programming Abstractions

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

Heaps. A complete binary tree can be easily stored in an array - place the root in position 1 (for convenience)

Largest Online Community of VU Students

ADT Unsorted List. Outline

Motivation for Templates

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

Chapter 17: Linked Lists

CMSC 341 Lecture 7. Announcements. Proj 2 up Project Preview tonight and tomorrow

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

Propedéutico de Programación

Linked Structures. See Section 3.2 of the text.

Transcription:

CS302 - Data Structures using C++ Topic: Queues and Priority Queues Kostas Alexis

Implementations of the ADT Queue Like stacks, queues can have Array-based or Link-based implementation Can also use implementation of ADT list Efficient to implement Might not be most time efficient as possible

An Implementation that uses the ADT list An implementation of the ADT queue that stores its entries in a list

An Implementation that uses the ADT list Header file for the class ListQueue #ifndef LIST_QUEUE_ #define LIST_QUEUE_ #include QueueInterface.h #include LinkedList.h #include PrecondViolatedExcept.h #include <memory> template<class ItemType> class ListQueue : public QueueInterface<ItemType> private: std::unique_ptr<linkedlist<itemtype>> listptr; // Pointer to list of queue items public: ListQueue(); ListQueue(const ListQueue& aqueue); ~ListQueue(); bool isempty() const; bool enqueue(const ItemType& newentry); bool dequeue(); // @throw PrecondViolatedExcept if this queue is empty ItemType peekfront() const throw(precondviolatedexcept); }; // end ListQueue #include ListQueue.cpp #endif

An Implementation that uses the ADT list The implementation file for the class ListQue #include ListQueue.h // Header file #include <memory> template<class ItemType> ListQueue<ItemType>::ListQueue() : listptr(std::make_unique<linkedlist<itemtype>>()) } // end default constructor template<class ItemType> ListQueue<ItemType>::ListQueue(const ListQueue& aqueue) : listptr(aqueue.listptr) } // end copy constructor template<class ItemType> ListQueue<ItemType>::~ListQueue() } // end destructor template<class ItemType> ListQueue<ItemType>::isEmpty(); return listptr->isempty(); } // end isempty template<class ItemType> ListQueue<ItemType>::enqueue(const ItemType& newentry) return listptr->insert(listptr->getlength() + 1, newentry); } // end enqueue

An Implementation that uses the ADT list The implementation file for the class ListQue template<class ItemType> ListQueue<ItemType>::dequeue() return listptr->remove(1); } // end dequeuer template<class ItemType> ListQueue<ItemType>::peekFront() const throw(precondviolatedexcept) if (isempty()) throw PrecondViolatedExcpt( peekfront() called with empty queue ); return listptr->getentry(1); } // end peekfront // end of implementation file

A Link-based Implementation Similar to other link-based implementations One difference: must be able to remove entries From front From back Requires a pointer to chain s last node Called the tail pointer

A Link-based Implementation A chain of linked nodes with head and tail pointers

A Link-based Implementation The header file for the class LinkedQueue #ifndef LINKED_QUEUE_ #define LINKED_QUEUE_ #include QueueInterface.h #include Node.h #include PrecondViolatedExcept.h #include <memory> template<class ItemType> class LinkedQueue : public QueueInterface<ItemType> private: // The queue is implemented as a chain of linked nodes that has two external pointers, a head pointer for the front of // the que and a tail pointer for the back of the queue std::shared_ptr<node<itemtype>> frontptr; std::shared_ptr<node<itemtype>> backptr; public: LinkedQueue(); LinkedQueue(const LinkedQueue& aqueue); ~LinkedQueue(); bool isempty() const; bool enqueue(const ItemType& newentry); bool dequeuer(); // @throw PrecondViolatedExcept if the queue is empty ItemType peekfront() const throw(precondviolatedexcept); }; // end LinkedQueue #include LinkedQueue.cpp #endif

A Link-based Implementation Adding an item to a nonempty queue

A Link-based Implementation Removing an item from a queue of more than one item

A Link-based Implementation Removing an item from a queue of more than one item (cont)

A Link-based Implementation Removing an item from a queue of more than one item (cont)

A Link-based Implementation Note: A circular chain of linked nodes with one external pointer One way to get it to work well with a single pointer

An Array-based Implementation If a fixed-size is no problem we may implement queues with arrays.

An Array-based Implementation If a fixed-size is no problem we may implement queues with arrays. At a minimum we need: static const int DEFAULT_CAPACITY = some value ItemType items[default_capacity] int front; int back;

An Array-based Implementation If a fixed-size is no problem we may implement queues with arrays. At a minimum we need: static const int DEFAULT_CAPACITY = some value ItemType items[default_capacity] int front; int back; Add item: increment back and place item in items[back] Remove item: increment front.

An Array-based Implementation If a fixed-size is no problem we may implement queues with arrays. At a minimum we need: static const int DEFAULT_CAPACITY = some value ItemType items[default_capacity] int front; int back; Add item: increment back and place item in items[back] Remove item: increment front. Problem: the queue is full when back equals DEFAULT_CAPACITY-1 and this may happen without the array being fully completed actually.

An Array-based Implementation A naïve array-based implementation of a queue for which rightward drift can cause the queue to appear full

An Array-based Implementation If a fixed-size is no problem we may implement queues with arrays. At a minimum we need: static const int DEFAULT_CAPACITY = some value ItemType items[default_capacity] int front; int back; Add item: increment back and place item in items[back] Remove item: increment front. Problem: the queue is full when back equals DEFAULT_CAPACITY-1 and this may happen without the array being fully completed actually. Possible Solution: Shift Elements

An Array-based Implementation If a fixed-size is no problem we may implement queues with arrays. At a minimum we need: static const int DEFAULT_CAPACITY = some value ItemType items[default_capacity] int front; int back; Add item: increment back and place item in items[back] Remove item: increment front. Problem: the queue is full when back equals DEFAULT_CAPACITY-1 and this may happen without the array being fully completed actually. Possible Solution: Shift Elements Alternative Elegant Solution: Treat the array as circular!

An Array-based Implementation A circular array as an implementation of a queue

An Array-based Implementation In the circular array implementation concept Remove item: increment the queue index front. Add item: increment back. When either front or back advances past DEFAULT_CAPACITY-1, then wrap around to 0. back = (back + 1) % DEFAULT_CAPACITY; items[back] = newentry;

An Array-based Implementation The effect of three consecutive operations on the queue

An Array-based Implementation Front and back as the queue becomes empty and as it becomes full

An Array-based Implementation Front and back as the queue becomes empty and as it becomes full (cont)

An Array-based Implementation The header file for the class ArrayQueue #ifndef ARRAY_QUEUE_ #define ARRAY_QUEUE_ #include QueueInterface.h #include PrecondViolatedExcept.h template<class ItemType> class ArrayQueue : public QueueInterface<ItemType> private: static const int DEFAULT_CAPACITY = 50; ItemType items[default_capacity]; // Array of queue items int front; // Index to front of queue int back; // Index to back of queue int count; // Number of items currently in the queue public: ArrayQueue(); bool isempty() const; bool enqueue(const ItemType& newentry); bool dequeue(); // @throw PrecondViolatedExcept if queue is empty ItemType peekfront() const throw(precondviolatedexcept); }; // end LinkedQueue #include ArrayQueue.cpp #endif

An Array-based Implementation The implementation file for the class ArrayQueue #include ArrayInterface.h template<class ItemType> ArrayQueue<ItemType>::ArrayQueue() : front(), back(default_capacity-1), count() } // end default constructor template<class ItemType> bool ArrayQueue<ItemType>::isEmpty() const return count == 0; } // end isempty template<class ItemType> bool ArrayQueue<ItemType>::enqueue(const ItemType& newentry) bool result = false; if (count < DEFAULT_CAPACITY) // Queue has room for another item back = (back + 1) % DEFAULT_CAPACITY; items[back] = newentry; count++; result = true; } // end if return result; } // end enqueue

An Array-based Implementation The implementation file for the class ArrayQueue template<class ItemType> ArrayQueue<ItemType>::dequeue() bool result = false; if (!isempty()) front = (front + 1) % DEFAULT_CAPACITY; count--; result = true; } // end if return result; } // end dequeue template<class ItemType> bool ArrayQueue<ItemType>::peekFront() const throw(precondviolatedexcept) // Enforce precondition if (isempty()) throw PrecondViolatedExcept( peekfront() called with empty queue ); // Queue is not empty; return front return items[front]; return count == 0; } // end peekfront // end of implementation file

Comparing Implementations Issues Fixed size (array-based) versus dynamic size (link-based) Reuse of already implemented class saves time

Assignment #3 Formal document release will take place (this is only an introduction) Task: Implemented the ADT Priority Queue Additional Tasks defined in the announcement

Assignment #3 Formal document release will take place (this is only an introduction) Main Task: Implemented the ADT Priority Queue Subject to the Header File we will provide Using a Sorted List Additional Tasks defined in the announcement

Assignment #3 Header file for the class SL_PriorityQueue Task: Implemented the ADT Priority Queue #ifndef PRIORITY_QUEUE_ #define PRIORITY_QUEUE _ Subject to the Header File we will provide #include PriorityQueueInterface.h #include LinkedSortedList.h #include PrecondViolatedExcept.h #include <memory> Using a Sorted List template<class ItemType> class SL_PriorityQueue : public PriorityQueueInterface<ItemType> private: std::unique_ptr<linkedsortedlist<itemtype>> slistptr; // Ptr to sorted list of items public: SL_PriorityQueue(); SL_PriorityQueue(const SL_PriorityQueue& pq); ~SL_PriorityQueue(); bool isempty() const; bool enqueue(const ItemType& newentry); bool dequeue(); // @throw PrecondViolatedExcept if priority queue is empty ItemType peekfront() const throw(precondviolatedexcept); }; // end SL_PriorityQueue #include ArrayQueue.cpp #endif

Thank you