Containers: Queue and List

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Advertisements

1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
CSE Lecture 12 – Linked Lists …
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Chapter 3: Arrays, Linked Lists, and Recursion
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
STACKS & QUEUES for CLASS XII ( C++).
Lecture 6 of Computer Science II
Cpt S 122 – Data Structures Abstract Data Types
CS505 Data Structures and Algorithms
CC 215 Data Structures Queue ADT
Chapter 15 Lists Objectives
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
EEL 4854 IT Data Structures Linked Lists
Briana B. Morrison Adapted from Alan Eugenio
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
Chapter 4 Linked Lists
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CMSC 341 Lecture 5 Stacks, Queues
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Linked Lists.
C++ Plus Data Structures
Object Oriented Programming COP3330 / CGS5409
Chapter 18: Linked Lists.
CSC 143 Queues [Chapter 7].
Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
Doubly Linked List Implementation
Search algorithms for vectors
Stacks and Queues Prof. Michael Tsai 2017/02/21.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Jordi Cortadella and Jordi Petit Department of Computer Science
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Visit for more Learning Resources
Lab4 problems More about templates Some STL
CS210- Lecture 6 Jun 13, 2005 Announcements
Queues Definition of a Queue Examples of Queues
Doubly Linked List Implementation
Stacks and Linked Lists
Standard Template Library
Data Structures & Programming
Data Structures & Programming
Presentation transcript:

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

Queue A container in which insertion is done at one end (the tail) and deletion is done at the other end (the head). Also called FIFO (First-In, First-Out) Queue push (enqueue) pop (dequeue) Containers © Dept. CS, UPC

Queue usage Queue<int> Q; // Constructor Q.push(5); // Inserting few elements Q.push(8); Q.push(6); int n = Q.size(); // n = 3 while (not Q.empty()) { int elem = Q.front(); // Get the first element cout << elem << endl; Q.pop(); // Delete the element } Containers © Dept. CS, UPC

The class Queue template<typename T> class Queue { public: Queue(); // Constructor ~Queue(); // Destructor Queue(const T& Q); // Copy constructor Queue& operator= (const Queue& Q); // Assignment operator void push(const& T x); // Enqueues an element void pop(); // Dequeues the first element T front() const; // Returns the first element int size() const; // Number of elements in the queue bool empty() const; // Is the queue empty? }; Containers © Dept. CS, UPC

Implementation with linked lists first last elem next elem next elem next elem next elem next template<typename T> class Queue { private: struct Node { T elem; Node* next; }; Node *first; // Pointer to the first element Node *last; // Pointer to the last element int n; // Number of elements Containers © Dept. CS, UPC

Implementation with linked lists first, last 5 next first, last Q.push(5) Q.push(8) 8 next 5 first last 6 next 8 5 last first Q.push(6) 6 next 8 last first Q.pop() Containers © Dept. CS, UPC

Queue: some methods /** Returns the number of elements. */ int size() const { return n; } /** Checks whether the queue is empty. */ bool empty() const { return size() == 0; } /** Inserts a new element at the end of the queue. */ void push(const T& x) { Node* p = new Node {x, nullptr}; if (n++ == 0) first = last = p; else last = last->next = p; } /** Removes the first element. Pre: the queue is not empty. */ void pop() { assert(not empty()); Node* old = first; first = first->next; delete old; if (--n == 0) last = nullptr; } /** Returns the first element. Pre: the queue is not empty. */ T front() const { assert(not empty()); return first->elem; } Containers © Dept. CS, UPC

Queue: constructors and destructor /** Default constructor: an empty queue. */ Queue() : first(nullptr), last(nullptr), n(0) { } /** Copy constructor. */ Queue(const Queue& Q) { copy(Q); } /** Assignment operator. */ Queue& operator= (const Queue& Q) { if (&Q != this) { free(); copy(Q); } return *this; } /** Destructor. */ ~Queue() { free(); } private: /** Frees the linked list of nodes in the queue. */ void free() { Node* p = first; while (p) { Node* old = p; p = p->next; delete old; } } Containers © Dept. CS, UPC

Queue: copy (private) Q: *this: Q.first p1 first p2 /** Copies a queue. */ void copy(const Queue& Q) { n = Q.n; if (n == 0) { first = last = nullptr; } else { Node* p1 = Q.first; Node* p2 = first = new Node {p1->elem}; while (p1->next) { p1 = p1->next; p2 = p2->next = new Node {p1->elem}; } p2->next = nullptr; last = p2; } } Containers © Dept. CS, UPC

Implementation with circular buffer A queue can also be implemented with an array (vector) of elements. It is a more efficient representation if the maximum number of elements in the queue is known in advance. 7 1 2 3 4 5 6 7 6 1 5 2 4 3 Containers © Dept. CS, UPC

Implementation with circular buffer read a 7 b 6 1 5 2 c 4 3 d write Containers © Dept. CS, UPC

Implementation with circular buffer read a 7 b 6 1 5 2 c write 4 3 e d after Q.push(e) Containers © Dept. CS, UPC

Implementation with circular buffer 7 b read 6 1 5 2 c write 4 3 e d after Q.pop() Containers © Dept. CS, UPC

Implementation with circular buffer write c 7 b 6 1 5 a 2 read 4 3 Containers © Dept. CS, UPC

Implementation with circular buffer d 7 write b 6 1 5 a 2 read 4 3 after Q.push(d) Containers © Dept. CS, UPC

The class Queue template<typename T> class Queue { public: Queue(int capacity = 1000); // Constructor (with capacity) ~Queue(); // Destructor Queue(const T& Q); // Copy constructor Queue& operator= (const Queue& Q); // Assignment operator void push(const& T x); // Enqueues an element void pop(); // Dequeues the element at the head T front() const; // Returns the first element int size() const; // Number of elements in the queue int capacity() const; // Returns the capacity of the queue bool empty() const; // Is the queue empty? bool full() const; // Is the queue full? }; Containers © Dept. CS, UPC

The class Queue (incomplete) template<typename T> class Queue { private: vector<T> buffer; // The buffer to store elements int read, write; // The read/write indices int n; // The number of elements public: /** Constructor with capacity of the queue. */ Queue(int capacity=1000) : buffer(capacity), read(0), write(0), n(0) {} /** Returns the size of the queue. */ int size() const { return n; } /** Returns the capacity of the queue. */ int capacity() const { return buffer.size(); } … Containers © Dept. CS, UPC

The class Queue (incomplete) /** Checks whether the queue is full. */ bool full() const { return size() == capacity(); } /** Enqueues a new element. Pre: the queue is not full. */ void push(const T& x) { assert(not full()); ++n; buffer[write] = x; inc(write); } /** Dequeues the first element. Pre: the queue is not empty. */ void pop() { assert(not empty()); inc(read); --n; } /** Returns the first element. Pre: the queue is not empty. */ T front() const { assert(not empty()); return buffer[read]; } private: /** Increases index circularly. */ void inc(int& i) { if (++i == capacity()) i = 0; } Containers © Dept. CS, UPC

Queue: Complexity All operations in queues can run in constant time, except for: Copy: linear in the size of the list. Delete: linear in the size of the list. Queues do not allow to access/insert/delete elements in the middle of the queue. Containers © Dept. CS, UPC

Exercise Extend the vector-based implementation of the queue to remove the constraint on maximum capacity. How: Increase capacity of the vector. Reorganize the elements in the queue. Containers © Dept. CS, UPC

List List: a container with sequential access. It allows to insert/erase elements in the middle of the list in constant time. A list can be considered as a sequence of elements with one or several cursors (iterators) pointing at internal elements. For simplicity, we will only consider lists with one iterator. Check the STL list: it can be visited by any number of iterators. Containers © Dept. CS, UPC

List: graphical representation first last 5 8 3 4 3 9 1 4 8 L.insert(7) 5 8 3 4 9 1 7 L.move_right() 5 8 3 4 9 1 7 L.erase() 5 8 3 4 1 7 Containers © Dept. CS, UPC

List implementations 5 8 3 4 9 1 4 8 5 8 3 4 9 1 Two stacks 5 8 3 4 9 4 9 1 4 8 5 8 3 4 9 1 Two stacks before the cursor after the cursor 5 8 3 4 9 1 cursor Doubly linked nodes cursor: pointer at the first node after the cursor Sentinel  Containers © Dept. CS, UPC

The class List: private representation template <typename T> class List { /** Doubly linked node of the list. */ struct Node { Node* prev; /** Pointer to the previous node. */ T elem; /** The element of the list. */ Node* next; /** Pointer to the next element. */ }; Node* sentinel; /** Sentinel of the list. */ Node* cursor; /** Node after the cursor. */ int n; /** Number of elements (without sentinel). */ Containers © Dept. CS, UPC

The class List: public methods public: /** Constructor of an empty list. */ List() : sentinel(new Node), cursor(sentinel), n(0) { sentinel->next = sentinel->prev = sentinel; } /** Destructor. */ ~List() { free(); } /** Copy constructor. */ List(const List& L) { copy(L); } /** Assignment operator. */ List& operator= (const List& L) { if (&L != this) { free(); copy(L); } return *this; } /** Returns the number of elements in the list. */ int size() const { return n; } /** Checks whether the list is empty. */ bool empty() const { return size() == 0; } Containers © Dept. CS, UPC

The class List: public methods public: /** Checks whether the cursor is at the beginning of the list. */ bool is_at_front() const { return cursor == sentinel->next; } /** Checks whether the cursor is at the end of the list. */ bool is_at_end() const { return cursor == sentinel; } /** Moves the cursor one position backward. Pre: the cursor is not at the beginning of the list. */ void move_backward() { assert(not is_at_front()); cursor = cursor->prev; } /** Moves the cursor one position forward. Pre: the cursor is not at the end of the list. */ void move_forward() { assert(not is_at_end()); cursor = cursor->next; } Containers © Dept. CS, UPC

The class List: public methods cursor public: /** Moves the cursor to the beginning of the list. */ void move_to_front() { cursor = sentinel->next; } /** Moves the cursor to the end of the list. */ void move_to_end() { cursor = sentinel; } /** Inserts an element x before the cursor. */ void insert(const T& x) { Node* p = new Node {cursor->prev, x, cursor}; cursor->prev = cursor->prev->next = p; ++n; } x c d b a cursor x p Containers © Dept. CS, UPC

The class List: public methods public: /** Erases the element after the cursor. Pre: cursor is not at the end. */ void erase() { assert(not is_at_end()); Node* p = cursor; p->next->prev = p->prev; cursor = p->prev->next = p->next; delete p; --n; } /** Returns the element after the cursor. Pre: the cursor is not at the end. */ T front() const { assert(not is_at_end()); return cursor->elem; } c d b a cursor cursor p c d b a Exercises: implement the private methods copy() and free(). Containers © Dept. CS, UPC

Exercises for lists Design the method reverse() that reverses the contents of the list: No auxiliary lists should be used. No copies of the elements should be performed. Solve the Josephus problem, for 𝑛 people and executing every 𝑘-th person, using a circular list: https://en.wikipedia.org/wiki/Josephus_problem Containers © Dept. CS, UPC

Exercises for lists Design the method merge(const List& L) that merges the list with another list L, assuming that both lists are sorted. Assume that a pair of elements can be compared with the operator <. Design the method sort() that sorts the list according to the < operator. Consider merge sort and quick sort as possible algorithms. Extend the previous methods with the compare function as a parameter of each method. Containers © Dept. CS, UPC

Higher-order functions A higher-order function is a function that can receive other functions as parameters or return a function as a result. Most languages support higher-order functions (C++, python, R, Haskell, Java, JavaScript, …). The have different applications: sort in STL is a higher-order function (the compare function is a parameter). functions to visit the elements of containers (lists, trees, etc.) can be passed as parameters. Mathematics: functions for composition and integration receive a function as parameter. etc… Containers © Dept. CS, UPC

Higher-order functions: example template <typename T> class List { … /** Transforms every element of the list using f. It returns a reference to the list. */ List<T>& transform(void f(T&)); /** Returns a list with the elements for which f is true */ List<T> filter(bool f(const T&)) const; /** Applies f sequentially to the list and returns a single value. For the list [ 𝒙 𝟏 , 𝒙 𝟐 , 𝒙 𝟑 ,…, 𝒙 𝒏 ] it returns 𝒇(…𝒇 𝒇 𝒙 𝟏 , 𝒙 𝟐 , 𝒙 𝟑 …, 𝒙 𝒏 ). If the list has one element, it returns 𝒙 𝟏 . The list is assumed to be non-empty */ T reduce(T f(const T&, const T&)) const; } Containers © Dept. CS, UPC

Higher-order functions: example /** Checks whether a number is prime */ bool isPrime(int n) {…} /** Adds two numbers */ int add(int x, int y) { return x + y; } /** Returns the square of a number */ int square(int x) { return x*x; } /** The following code computes: 𝒙∈𝑳, 𝒙 is prime 𝒙 𝟐 Note: it assumes that there is at least one prime in the list. */ int n = L.filter(isPrime).transform(square).reduce(add); Containers © Dept. CS, UPC

Higher-order functions: example List<T>& transform(void f(T&)) { Node* p = sentinel->next; while (p != sentinel) { // Visit all elements and apply f to each one f(p->elem); p = p->next; } return *this; } List<T> filter(bool f(const T&)) const { List<T> L; Node* p = sentinel->next; while (p != sentinel) { // Pick elements only if f is asserted if (f(p->elem)) L.insert(p->elem); p = p->next; } return L; } T reduce(T f(const T&, const T&)) const { assert(L.size() > 0); T x = sentinel->next->elem; // First element (and result) Node* p = sentinel->next->next; while (p != sentinel) { x = f(x, p->elem); // Composition with next element p = p->next; } return x; } Containers © Dept. CS, UPC