The Big 5 and Lists.

Slides:



Advertisements
Similar presentations
Chapter 17 Linked Lists.
Advertisements

PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Linked Lists CENG 213 Data Structures.

Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Introduction to Programming Lecture 39. Copy Constructor.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
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.
CSE 332: C++ copy control II Copy Control (Part II) Review: copy control consists of 5 distinct operations –A copy constructor initializes an object by.
Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.
1 Classes with Pointer Data Members (II) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.
Classes with dynamic objects List as a (dynamic) class.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Unrestricted © Siemens AG 2015 All rights reserved.Smarter decisions, better products. Move semantics && rvalue references, part 2 Bert Rodiers, Software.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Chapter 6 LISTS AND STRINGS 1. List Definition 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Doubly Linked 3. Linked.
The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Linked Lists and Generics Written by J.J. Shepherd.
1 CSE 2341 Object Oriented Programming with C++ Note Set #18.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Learning Objectives Pointers as dada members
Chapter 7 CS 3370 chapter 7 CS 3370 – C++ Classes.
CSC172 Data Structures Linked Lists (C version)
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
LinkedList Class.
The Bag and Sequence Classes with Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Pointers and Linked Lists
Chapter 16-2 Linked Structures
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Chapter 4 Linked Lists.
understanding memory usage by a c++ program
Chapter 18: Linked Lists.
CS 302 Data Structures Linked Lists.
Foundational Data Structures
Pointers and Linked Lists
Linked Lists.
Indirection.
Chapter 4 Linked Lists.
Templates and Iterators
Resource Allocation and Ownership
Passing Arguments and The Big 5
프로그래밍2 및 실습 Sort Code 전명중.
CMSC 341 List 2.
Passing Arguments and The Big 5
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Essential Class Operations
Linked Lists Templates and Iterators
Linked List Improvements
Linked Lists.
Destructors, Copy Constructors & Copy Assignment Operators
Rule of Five.
Destructors, Copy Constructors & Copy Assignment Operators
CS 144 Advanced C++ Programming April 30 Class Meeting
Presentation transcript:

The Big 5 and Lists

Classes – No Internal "new" Member variables Methods Constructor Rule of Zero

How hard was week 9 Sudoku code review assignment? Easy Moderate Challenging Unreasonable

How long did week 9 Sudoku assignment take? Less than 3 hours 3 to 6 hours 6 to 9 hours 9 to 12 hours More than 12 hours

Case Study: StringHolder class StringHolder { std::string *string_; public: StringHolder() : string_(nullptr) {}; StringHolder(const char *initial_string); const char* c_str() const noexcept; void ChangeString(const char *input_string); };

Classes – Containers Use "new" Rule of Zero won't work Deep vs Shallow Copy

Rule of Three Destructor Copy Constructor Copy Assignment Operator

Rule of Three - Destructor ~StringHolder()

Rule of Three - Destructor ~StringHolder(){ delete string_; }

Rule of Three - Copy Constructor StringHolder(const StringHolder &source)

Rule of Three - Copy Constructor StringHolder(const StringHolder &source) { if(source.string_) { string_ = new std::string(*source.string_); } else { string_ = nullptr; }

Rule of Three – Copy Assignment Operator StringHolder& operator=(const StringHolder &source)

Rule of Three – Copy Assignment Operator StringHolder& operator=(const StringHolder &source){ if(this == &source) { return; } delete string_; string_ = nullptr; if(source.string_) { string_ = new std::string(*source.string_); return *this;

Rule of Three vs Rule of Five Rule of five (move semantics new with C++11) Move Constructor Move Assignment Operator rvalue reference && Can bind to a temporary (rvalue)

Rule of Five - Move Constructor StringHolder(StringHolder&& source)

Rule of Five - Move Constructor StringHolder(StringHolder&& source) { string_ = source.string_; source.string_ = nullptr; }

Rule of Five – Move Assignment Operator StringHolder& operator=(StringHolder&& source)

Rule of Five – Move Assignment Operator StringHolder& operator=(StringHolder&& source) { if(this == &source) { return; } delete string_; string_ = source.string_; source.string_ = nullptr; return *this;

Linked Lists

Linked List Data Structures struct ListNode { Data data_; ListNode next_; ListNode(Data d) : data_(d), next_(nullptr) {}; } class LinkedList { ListNode head_; … public:

Insert at start void InsertHead(Data new_data) { ListNode *new_node = new ListNode(new_data); new_node->next_ = head_; head_ = new_node; }

Remove Head void RemoveHead() { if(!head_) { return; } ListNode *tmp = head_; head_ = head_->next_; delete tmp; }

Remove Tail void RemoveTail() { if(!head_) { return; } if(head_->next_ == nullptr) { delete head_; head_ = nullptr; return;} ListNode *remove_next = head_; while(remove_next->next_ && remove_next->next_->next_) { remove_next = remove_next->next_; } delete remove_next->next_; remove_next = nullptr;

VisuAlgo https://visualgo.net/en/list