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

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

Insertion sort 20/11/2018. The idea. Example

Member functions 21/11/2018. Accessing member variables. Accessing member variables

Console input 26/09/2018. Background. Background

Templates 12/11/2018. Redundancy. Redundancy

Selection sort 20/11/2018. The idea. Example

Polymorphism 02/12/2018. Member functions. Member functions

Pointer arithmetic 20/11/2018. Pointer arithmetic. Pointer arithmetic

Logical operators 20/09/2018. The unit pulse. Background

Anatomy of a program 06/09/2018. Pre-processor directives. In this presentation, we will: Define the components of a program

Main memory 05/10/2018. Main memory. Main memory

Dynamic memory allocation

The call stack and recursion and parameters revisited

Throwing exceptions 02/12/2018. Throwing objects. Exceptions

The structured programming theorem

Binary and hexadecimal numbers

Integer primitive data types

Strings 20/11/2018. a.k.a. character arrays. Strings. Strings

LECTURE 03 LINKED LIST

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

And Even More and More C++ Fundamentals of Computer Science

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

1 Short Answer (7 Points Each)

Data Structures and Algorithms

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

Chapter 17: Linked Lists

Chapter 17: Linked Lists

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

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

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory

CSI33 Data Structures

Linked List using a Sentinel

Common Misunderstandings from Exam 1 Material

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

Stacks and their Applications

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Abstract Data Types 1

Data Structures (INE2011)

Dynamic Data Structures

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

Chapter 18: Stacks And Queues

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

Data Structures And Algorithms

CSCE 110 PROGRAMMING FUNDAMENTALS

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

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

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

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

Special Member Functions

Abstract Data Types 1

Data Structures and Algorithms

MIDTERM EXAMINATION Douglas Wilhelm Harder EIT 4018 x T09:30:00P1H20M Rooms: RCH-103 and RCH-302

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

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

CSCE 110 PROGRAMMING FUNDAMENTALS

C++ Templates. David Camp

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)

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

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

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

ECE 250 Algorithms and Data Structures

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

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

The C++ Object Lifecycle. EECS 211 Winter 2019

COMP6771 Advanced C++ Programming

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

Traversing Trees with Iterators

Traversing Trees with Iterators

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

ECE 2400 Computer Systems Programming Fall 2017 Topic 14: C++ Inheritance

Chapter 18: Stacks And Queues

Object Oriented Programming COP3330 / CGS5409

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

ECE 2400 Computer Systems Programming Fall 2017 Topic 15: C++ Templates

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

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

An Introduction to Trees

EECE.3220: Data Structures Spring 2017

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

Refresher: Interface Specifications. ADT Documentation. Set Represented as an Array. Representation Invariant, Abstraction Function

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

CS24 Week 4 Lecture 2

CSC 172 Data Structures and Algorithms. Lecture #8 Spring 2018

Data Structures. Jia-Liang Lu. Slides based on the course notes of C.A. Shaffer

Representation Invariant, Abstraction Function

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

Associate Professor Dr. Raed Ibraheem Hamed

Data Structures and Algorithms

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

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

Data Structures (CS301) LAB

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

Student Name and ID CS 32, WINTER 2015, PRACTICE MIDTERM I.

ADTs Stack and Queue. Outline

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

Queue Implementations

Part X. Advanced C ++

COMP6771 Advanced C++ Programming

Transcription:

ECE 150 Fundamentals of Programming Outline 2 In this lesson, we will: Understand how to push a new entry onto the back of the linked list Determine how we can speed this up Understand that the cost is A negligible slow-downs in other member functions One more member variable Douglas Wilhelm Harder, M.Math Prof. Hiren Patel, Ph.D. hdpatel@uwaterloo.ca dwharder@uwaterloo.ca 2018 by Douglas Wilhelm Harder and Hiren Patel. Some rights reserved. 3 4 Our linked list class Problem Reviewing our linked list class class Linked_list { Linked_list(); // Constructor ~Linked_list(); // Destructor bool empty() const; std::size_t size() const; int front() const; void print() const; std::size_t find( int datum ) const; ; void push_front( int datum ); bool pop_front(); void clear(); Node *p_list_head; // Pointer to head node std::size_t list_size; We can only push at the front of the linked list What happens if we want to push a new node at the back? We require a push_back() member function Where might we use this? Suppose you were waiting in line You given a unique ID number Your ID number is pushed on the back of the linked list When it comes time to service someone who is waiting, the ID at the front of the linked list is called and then popped This allows for first-come first-served (FIFO) 1

5 6 of the linked list of an empty linked list We may be either: Pushing onto the back of an empty linked list This is the same as pushing at the front Pushing onto the back of a non-empty linked list of an empty linked list: else { of a non-empty linked list 7 of a non-empty linked list 8 Suppose the linked list is not empty and we want to push 42 onto the back of the linked list In a nutshell, we require: We must: Get a pointer to the last node Update that node to point to a new node storing the value 42 2

of an empty linked list 9 10 of an empty linked list of an empty linked list: else { Node *p_current_list_tail{p_list_head; while ( p_current_list_tail->get_next()!= nullptr ) { p_current_list_tail = p_current_list_tail->get_next(); // Now p_current_list_tail' stores the address // of the last node of the linked list assert( p_current_list_tail!= nullptr ); assert( p_current_list_tail->get_next() == nullptr ); p_current_list_tail->p_next_node = new Node{datum, nullptr; Wait: Isn t p_next_node private? How do we modify it? The Node class can declare the Linked_list class to be a friend class Node { // Member functions to access private member variables Node( int value, Node *p_next = nullptr ); int get_value() const; Node *get_next() const; // Let's make them private now... int node_value; ; Node *p_next_node; friend Remember: Only friends can access your private members 11 of an empty linked list Can we speed this up? 12 With friendship, we can access the member variables of the Node class else { Node *p_current_list_tail{p_list_head; while ( p_current_list_tail->get_next()!= nullptr ) { p_current_list_tail = p_current_list_tail->get_next(); // Now p_current_list_tail' stores the address // of the last node of the linked list assert( p_current_list_tail!= nullptr ); assert( p_current_list_tail->get_next() == nullptr ); p_current_list_tail->p_next_node = new Node{datum, nullptr; The loop in pushing at the back is the bottleneck: We must step through the entire linked list to find the last node To speed this up, we must keep track of the current tail of the linked list That is, like p_list_head, we need a p_list_tail member variable class Linked_list { // Public constructor/destructor/member function declarations ; Node *p_list_head; // Pointer to head node Node *p_list_tail; // Pointer to tail node std::size_t list_size; 3

Can we speed this up? 13 Constructor and destructor 14 Again, like with list_size, we may have to update p_list_tail whenever we either: Add a new node Erase an existing node The constructor must initialize the pointer s value: Linked_list::Linked_list(): p_list_head{nullptr, p_list_tail{nullptr, list_size{0 { // Nothing else for the constructor to do Inserting a node 15 Removing a node 16 When pushing a new node at the front of the linked list, we only have to update the member variable if the list is empty: void Linked_list::push_front( int datum ) { p_list_head = new Node{datum, p_list_head; if ( size() == 0 ) { p_list_tail = p_list_head; Question: Why can we no longer call empty()? When popping, we only have to update tail pointer if we re now empty: bool Linked_list::pop_front() { return false; else { Node *p_previous_list_head{p_list_head; p_list_head = p_list_head->get_next(); delete p_previous_list_head; --list_size; p_list_tail = nullptr; return true; 4

17 of an empty linked list Accessing the value at the back 18 We can now simplify our implementation of push_back( ): else { p_list_tail->p_next_node = new Node{datum, nullptr; p_list_tail = p_list_tail->get_next(); We can now access the back quickly, too: int Linked_list::back() const { std::cerr << "Error: list is empty" << std::endl; throw nullptr; else { return p_list_tail->get_value(); Pushing an entire list at the back 19 Concatenating two linked lists 20 We can now also concatenate another linked list onto this one: The implementation is very fast: void Linked_list::push_back( Linked_list &list ) { if (!list.empty() ) { p_list_tail->p_next_node = list.p_list_head; p_list_tail = list.p_list_tail; list_size += list.list_size; Questions: Why do we have to empty the second list? What do we do if the second list is already empty? list.p_list_head = nullptr; list.p_list_tail = nullptr; list.list_size = 0; 5

Concatenating two linked lists 21 Our linked list class 22 Question: What would happen if we did not empty the second list? void Linked_list::push_back( Linked_list &list ) { if (!list.empty() ) { p_list_tail->p_next_node = list.p_list_head; p_list_tail = list.p_list_tail; list_size += list.list_size; Our public member functions have not changed their behavior: class Linked_list { Linked_list(); // Constructor ~Linked_list(); // Destructor bool empty() const; std::size_t size() const; int front() const; int back() const; void print() const; std::size_t find( int datum ) const; void push_front( int datum ); void push_back( int datum ); void push_back( Linked_list &list ); bool pop_front(); void clear(); ; Node *p_list_head; // Pointer to head node Node *p_list_tail; // Pointer to tail node std::size_t list_size; Benefit of encapsulation 23 Summary 24 Adding this member variable and modifying our member functions in no way affected the way users interact with this class The only differences are: It uses a little more memory (one more pointer) Some functions are trivially slower (a few extra instructions) The back() and push_back( ) member function are significantly faster now Following this lesson, you now Know how to add a new node at the back of the linked list Understand how this can be sped up by adding a new member variable storing the address of the last node Understand the concept of friend in C++ Know when and how to update this member variable 6

References 25 Colophon 26 [1] No references? These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see https://www.rbg.ca/ for more information. Disclaimer 27 These slides are provided for the ECE 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended. 7