new linked-based implementation · 2019. 9. 20. · fish(); //default constructor fish(std::string...

85
Linked-Based Implementation Tiziana Ligorio [email protected] 1

Upload: others

Post on 12-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Linked-Based Implementation

Tiziana Ligorio [email protected]

!1

Page 2: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Today’s Plan

Announcements

A quick review of pointers and dynamic memory allocation

Linked-Based Implementation

!2

Page 3: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Announcements

Project 3: 2 weeks is NOT an invitation to procrastinate… on the contrary - Give yourself time to resolve unforeseen issues - Give yourself time to seek help should you need it

Practice Challenges https://huntercuny2x.github.io/challenges

!3

Page 4: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Recap

On Tuesday we implemented: - add()

- toVector() // traverse - getPointerTo() - remove()

!4

Page 5: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Constructors Clarifications

- Multiple constructors, only one is invoked

!5

Page 6: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

#include “Animal.hpp" int main() {

Animal nameless; //calls default constructor

Animal tiger(“tiger”); //calls parameterized const. w/ default args

Animal shark(“shark”, false, true); //calls parameterized constructor //with all arguments

//more code here . . . }; //end main

class Animal { public: Animal(); //default constructor Animal(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

// more code here };// end Animal

main()

!6

Page 7: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Constructors Clarifications

- Multiple constructors, only one is invoked

- Initialize ALL data members in parameterized constructor, not only those with arguments

- Explicitly call Base class constructor only if needs argument values or if there is no default to be called

!7

Page 8: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Constructors Clarifications

- Multiple constructors, only one is invoked

- Initialize ALL data members in parameterized constructor, not only those with arguments

- Explicitly call Base class constructor only if needs argument values or if there is no default to be called

!8

Page 9: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

#include “Fish.hpp" //default constructorFish::Fish(): venomous_(0){} //parameterized constructorFish::Fish(std::string name, bool domestic, bool predator):

Animal(name, domestic, predator), venomous_(0){}

//more code here . . .

class Fish { public: Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

// more code here };// end Fish

Fish.cpp

!9

Base class (Animal) constructor always called

first. It will initialize derived data members.

Base class parameterized constructor needs access to argument values and must be

called explicitly.

Page 10: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Pointers an Dynamic Memory Allocation (Review)

!10

Page 11: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Pointer Variables

A typed variable whose value is the address of another variable of same type

!11

Page 12: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

int x = 5; int y = 8; int *p, *q = nullptr; //declares two int pointers

. . .

!12

Type Name Address Data... ... ... ...int x 0x12345670 5int y 0x12345674 8

int pointer p 0x12345678 nullptrint pointer q 0x1234567C nullptr

... ... ... ...

Program Stack

Make sure you do this if not assigning a value!

Page 13: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

int x = 5; int y = 8; int *p, *q = nullptr; //declares two int pointers

. . . p = &x; // sets p to the address of x q = &y; // sets q address of y

!13

Type Name Address Data... ... ... ...int x 0x12345670 5int y 0x12345674 8

int pointer p 0x12345678 0x12345670int pointer q 0x1234567C 0x12345674

... ... ... ...

Program Stack

Make sure you do this if not assigning a value!

Page 14: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

int x = 5; int y = 8; int *p, *q = nullptr; //declares two int pointers

. . . p = &x; // sets p to the address of x q = &y; // sets q address of y

!14

We won’t do much of this

Type Name Address Data... ... ... ...int x 0x12345670 5int y 0x12345674 8

int pointer p 0x12345678 0x12345670int pointer q 0x1234567C 0x12345674

... ... ... ...

Program Stack

Make sure you do this if not assigning a value!

Page 15: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Recall Dynamic Variables

What if I cannot statically allocate data? (e.g. will be reading from input at runtime)

!15

Page 16: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Recall Dynamic Variables

What if I cannot statically allocate data? (e.g. will be reading from input at runtime)

Allocate dynamically with new

!16

Page 17: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Dynamic VariablesCreated at runtime in the memory heap using operator new

Nameless typed variables accessed through pointers

// create a nameless variable of type dataType on the //application heap and stores its address in p dataType *p = new dataType;

!17

Type Name Address Data... ... ... ...

dataType ptr p 0x12345678 0x100436f20

... ... ... ...

Program StackType Address Data

... ... ...

dataType 0x100436f20

... ... ...

Heap

Page 18: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Accessing members

dataType some_object; dataType *p = new dataType; // initialize and do stuff with instantiated objects

. . .

string my_string = some_object.getName(); string another_string = p->getName();

!18

To access member functions in place of . operator

Page 19: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Deallocating Memory

delete p; p = nullptr;

!19

Must do this!!!

Deletes the object pointed to by p

Page 20: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Avoid Memory Leaks (1)Occurs when object is created in free store but program no longer has access to it

dataType *my_ptr = new dataType; dataType *your_ptr = new dataType; // do stuff with my_ptr and your_ptr

your_ptr = my_ptr;

!20

my_ptr

my_ptr

your_ptr

your_ptr

Object

Object

Object

InaccessibleMemory Leak

Page 21: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Avoid Memory Leaks (2)Occurs when object is created in free store but program no longer has access to it

void leakyFunction(){ dataType *my_ptr = new dataType; dataType *your_ptr = new dataType; // do stuff with my_ptr and your_ptr }

!21

my_ptr

your_ptr

Object

Object

InaccessibleMemory Leak

Inaccessible

Left scope of local pointer variables

Memory Leak

Programmer’s responsibility to

release free store

Page 22: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Avoid Memory Leaks (2)Occurs when object is created in free store but program no longer has access to it

void leakyFunctionFixed(){ dataType *my_ptr = new dataType; dataType *your_ptr = new dataType; // do stuff with my_ptr and your_ptr delete my_ptr; my_ptr = nullptr; delete your_ptr; your_ptr = nullptr; }

!22

my_ptr

your_ptr

Object

ObjectLeft scope of local pointer variables

but deleted dynamic objects first

Page 23: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Avoid Dangling Pointers

Pointer variable that no longer references a valid object

delete my_ptr;

delete my_ptr; my_ptr = nullptr;

!23

Must do this!!!

my_ptr Object

my_ptr

my_ptr

Dangling Pointer

Fix

Page 24: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Avoid Dangling Pointers

Pointer variable that no longer references a valid object delete my_ptr; my_ptr = nullptr;

!24

my_ptrObject

my_ptr

Dangling Pointeryour_ptr

your_ptr

Page 25: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Avoid Dangling Pointers

Pointer variable that no longer references a valid objectdelete my_ptr; my_ptr = nullptr;

delete your_ptr;// ERROR!!!! No object to delete

!25

my_ptrObject

my_ptr

Dangling Pointeryour_ptr

your_ptr

Page 26: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Avoid Dangling Pointers

Pointer variable that no longer references a valid object delete my_ptr; my_ptr = nullptr;

delete my_ptr; my_ptr = nullptr; your_ptr = nullptr;

!26

Must set all pointers to nullptr!!!

my_ptrObject

my_ptr

my_ptr

Dangling Pointeryour_ptr

your_ptr

your_ptr

Fix

Page 27: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

What is wrong with the following code?void someFunction() {

int* p = new int[5]; int* q = new int[10];

p[2] = 9; q[2] = p[2]+5; p[0] = 8; q[7] = 15;

std::cout<< p[2] << " " << q[2] << std::endl; q = p; std::cout<< p[0] << " " << q[7] << std::endl;

}!27

Page 28: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

What is wrong with the following code?void someFunction() {

int* p = new int[5]; int* q = new int[10];

p[2] = 9; q[2] = p[2]+5; p[0] = 8; q[7] = 15;

std::cout<< p[2] << " " << q[2] << std::endl; q = p; std::cout<< p[0] << " " << q[7] << std::endl;

}!28

MEMORY LEAK: int[10] lost on heap

SEGMENTATION FAULT int[5] index out of range

MEMORY LEAK: Did not delete int[5]before exiting function

Page 29: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Let’s try a different implementation for Bag

!29

Page 30: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Link-Based Implementation

!30

Page 31: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Data Organization

Place data within a Node object

Link nodes into a chain

!31

Page 32: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Node

!32

Page 33: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Chain

!33

Page 34: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Entering the Chain

!34

Page 35: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

The Empty Chain

!35

Page 36: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

The Class Node#ifndef NODE_H_ #define NODE_H_ template<class T> class Node { public:

Node(); Node(const T& an_item); Node(const T& an_item, Node<T>* next_node_ptr); void setItem(const T& an_item); void setNext(Node<T>* next_node_ptr); T getItem() const; Node<T>* getNext() const;

private:

T item_; // A data item Node<T>* next_; // Pointer to next node

}; // end Node

#include "Node.cpp" #endif // NODE_H_

!36

Page 37: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Node Implementation#include “Node.hpp”

template<class T> Node<T>::Node() : next_(nullptr) {

} // end default constructor

template<class T> Node<T>::Node(const T& an_item) : item_(an_item), next_(nullptr) {

} // end constructor

template<class T> Node<T>::Node(const T& an_item, Node<T>* next_node_ptr) : item_(an_item), next_(next_node_ptr){

} // end constructor

!37

The Constructors

Page 38: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Node Implementation#include “Node.hpp”

template<class T> void Node<T>::setItem(const T& an_item) { item_ = an_item; } // end setItem

template<class T> void Node<T>::setNext(Node<T>* next_node_ptr) {

next_ = next_node_ptr;

} // end setNext

!38

The “setData” members

Page 39: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Node Implementation#include “Node.hpp”

template<class T> T Node<T>::getItem() const {

return item_;

} // end getItem

template<class T> Node<T>* Node<T>::getNext() const {

return next_;

} // end getNext

!39

The “getData” members

Page 40: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

A Linked Bag ADT

!40

Page 41: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

The Class LinkedBag#ifndef LINKED_BAG_H_ #define LINKED_BAG_H_

#include "BagInterface.hpp" #include “Node.hpp"

template<class T> class LinkedBag { public: LinkedBag(); LinkedBag(const LinkedBag<T>& a_bag); // Copy constructor ~LinkedBag(); // Destructor int getCurrentSize() const; bool isEmpty() const; bool add(const T& new_entry); bool remove(const T& an_entry); void clear(); bool contains(const T& an_entry) const; int getFrequencyOf(const T& an_entry) const; std::vector<T> toVector() const;

private:

???

}; // end LinkedBag

#include “LinkedBag.cpp" #endif //LINKED_BAG_H_

!41

Page 42: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

The Class LinkedBag#ifndef LINKED_BAG_H_ #define LINKED_BAG_H_

#include “Node.hpp"

template<class T> class LinkedBag { public: LinkedBag(); LinkedBag(const LinkedBag<T>& a_bag); // Copy constructor ~LinkedBag(); // Destructor int getCurrentSize() const; bool isEmpty() const; bool add(const T& new_entry); bool remove(const T& an_entry); void clear(); bool contains(const T& an_entry) const; int getFrequencyOf(const T& an_entry) const; std::vector<T> toVector() const;

private: Node<T>* head_ptr_; // Pointer to first node int item_count_; // Current count of bag items

// Returns either a pointer to the node containing a given entry // or the null pointer if the entry is not in the bag.Node<T>* getPointerTo(const T& target) const;

}; // end LinkedBag

#include “LinkedBag.cpp" #endif //LINKED_BAG_H_

!42

More than one public method will need to know ifthere is a pointer to a target so we separate it out into a

private helper function (similar to ArrayBag but here we get pointers rather than indices)

Page 43: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

LinkedBag Implementation#include “LinkedBag.hpp”

template<class T> LinkedBag<T>::LinkedBag() : head_ptr_(nullptr), item_count_(0) {

} // end default constructor

!43

The default constructor

Private data member initialization

Page 44: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

add(const T& new_entry)

Where should we add?

!44

Page 45: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Lecture Activity

Write pseudocode for a sequence of steps to add to the front of the chain

!45

Page 46: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Instantiate a new node and let a temp pointer point to it

. . . head_ptr_

temp_ptr

Page 47: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Instantiate a new node and let a temp pointer point to it

Let the next pointer of the new node point to the same node head_ptr_ points to

. . .

. . . head_ptr_

temp_ptr

temp_ptr

head_ptr_

Page 48: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Instantiate a new node and let a temp pointer point to it

Let the next pointer of the new node point to the same node head_ptr_ points to

Let head_ptr_ point to the new node

!48

. . .

. . .

. . . head_ptr_

temp_ptr

temp_ptr

head_ptr_

head_ptr_

temp_ptr

Page 49: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Instantiate a new node and let a temp pointer point to it

Let the next pointer of the new node point to the same node head_ptr_ points to

Let head_ptr_ point to the new node

!49

. . .

. . .

. . . head_ptr_

temp_ptr

temp_ptr

head_ptr_

head_ptr_

temp_ptr

Page 50: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Pseudocode (English-like)

• Instantiate a new node and let temp_ptr point to it

• Set temp_ptr->next to point to the same node head_ptr_ points to

• Set head_ptr to point to the same node temp_ptr points to

• Set temp_ptr to nullptr

!50

Page 51: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Pseudocode (Code-like)

temp_ptr = new node temp_ptr->next = head_ptr_ head_ptr = temp_ptr temp_ptr = nullptr

!51

Page 52: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

#include “LinkedBag.hpp”

template<class T> bool LinkedBag<T>::add(const T& new_entry) { // Add to beginning of chain: new node references rest of chain; // (head_ptr_ is null if chain is empty) Node<T>* new_node_ptr = new Node<T>; new_node_ptr->setItem(new_entry); new_node_ptr->setNext(head_ptr_); // New node points to chain

head_ptr_ = new_node_ptr;// New node is now first node item_count_++;

return true; } // end add

Dynamic memory allocation

Adding nodes to the heap!

LinkedBag Implementation

!52

The add method Add at beginning of chain is easy

because we have head_ptr_

Page 53: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Efficiency

Create a new node and assign two pointers

What about adding to end of chain?

What about adding to front of array?

!53

Page 54: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Lecture Activity

Write Pseudocode to traverse the chain from first node to last

!54

Page 55: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Traversing the chain

Let a current pointer point to the first node in the chain while(the current pointer is not the null pointer) {

“visit” the current node set the current pointer to the next pointer of the

current node }

!55

Page 56: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

LinkedBag Implementation#include “LinkedBag.hpp”

template<class T> std::vector<T> LinkedBag<T>::toVector() const { std::vector<T> bag_contents; Node<T>* cur_ptr = head_ptr_; while ((cur_ptr != nullptr)) {

bag_contents.push_back(cur_ptr->getItem()); cur_ptr = cur_ptr->getNext(); } // end while

return bag_contents; } // end toVector

!56

The toVector method

Traversing: Visit each node Copy it

Page 57: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

LinkedBag Implementation

Similarly getFrequencyOf will: traverse the chain and count frequency of (count each) an_entry

!57

Page 58: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

LinkedBag Implementation#include “LinkedBag.hpp”

template<class T> Node<T>* LinkedBag<T>::getPointerTo(const T& an_entry) const { bool found = false; Node<T>* cur_ptr = head_ptr_;

while (!found && (cur_ptr != nullptr)) { if (an_entry == cur_ptr->getItem()) found = true; else cur_ptr = cur_ptr->getNext(); } // end while

return cur_ptr; } // end getPointerTo

!58

The getPointerTo method

Traversing: visit each node if found what looking for return

Page 59: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Purposely vague Some fixed amount of work

Efficiency

No fixed number of steps

Depends on location of an_entry - 1 “check” if it is found at first node (best case) - n “checks” if it is found at last node (worst case) - approximately n/2 on average if random?

!59

Page 60: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

What should we do to remove?

!60

Page 61: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

LinkedBag Implementation#include “LinkedBag.hpp”

template<class T> bool LinkedBag<T>::remove(const T& an_entry) { Node<T>* entry_ptr = getPointerTo(an_entry); bool can_remove = (entry_ptr != nullptr); if (can_remove) { // Copy data from first node to located node entry_ptr->setItem(head_ptr_->getItem()); // Delete first node Node<T>* node_to_delete_ptr = head_ptr_; head_ptr_ = head_ptr_->getNext(); // Return node to the system node_to_delete_ptr->setNext(nullptr); delete node_to_delete_ptr; node_to_delete_ptr = nullptr; item_count_--; } // end if

return can_remove; } // end remove

!61

The remove method

Deleting first node is easy

Copy data from first node to node to delete Delete first node

Must do this!!! Avoid memory leaks!!!

Find

Page 62: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

How do we clear the bag?

!62

Page 63: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

LinkedBag Implementation#include “LinkedBag.hpp”

template<class T> void LinkedBag<T>::clear() { Node<T>* node_to_delete_ptr = head_ptr_; while (head_ptr_ != nullptr) { head_ptr_ = head_ptr_->getNext();

// Return node to the system node_to_delete_ptr->setNext(nullptr); delete node_to_delete_ptr;

node_to_delete_ptr = head_ptr_; } // end while // head_ptr_ is nullptr; node_to_delete_ptr is nullptr

item_count_ = 0; } // end clear

!63

The clear method

Once again we are traversing: Visit each node Delete it

Must do this!!! Avoid memory Leak!!!

Page 64: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Dynamic Memory Considerations

Each new node added to the chain is allocated dynamically and stored on the heap

Programmer must ensure this memory is deallocated when object is destroyed!

Avoid memory leaks!!!!

!64

Page 65: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

What happens when object goes out of scope?

!65

head_ptr_

item_count_

Stack Heap

LinkedBag

Page 66: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

What happens when object goes out of scope?

!66

Stack HeapSad and adrift in Heap space

Memory leak!!!

Page 67: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

LinkedBag Implementation#include “LinkedBag.hpp”

template<class T> LinkedBag<T>::~LinkedBag() {

clear();

} // end destructor

!67

The destructor

Ensure heap space is returned to the system

Must do this!!! Avoid memory leaks!!!

Page 68: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Copy Constructor•1. Initialize one object from another of the same type

• MyClass one;

MyClass two = one; More explicitly MyClass one;

MyClass two(one); // Identical to above.

•2. Copy an object to pass by value as an argument to a function void MyFunction(MyClass arg) {

/* ... */ }

•3. Copy an object to be returned by a function MyClass MyFunction() {

MyClass mc; return mc;

}

!68

Creates a new object as a copy of another one

Compiler will provide one but may not appropriate

for complex objects

Page 69: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Deep vs Shallow Copy

!69

Page 70: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Deep vs Shallow Copy

!70

Page 71: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Overloaded operator=

•MyClass one; •//Stuff here MyClass two = one;

•IS DIFFERENT FROM

•MyClass one, two; •//Stuff here two = one;

!71

Instantiation: copy constructor is called

Assignment, NOT instantiation: no constructor

is called, must overload operator= to avoid

shallow copy

Page 72: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

LinkedBag Implementation#include “LinkedBag.hpp” template<class T> LinkedBag<T>::LinkedBag(const LinkedBag<T>& a_bag) { item_count_ = a_bag.item_count_; Node<T>* orig_chain_ptr = a_bag.head_ptr_; // Points to nodes in original chain if (orig_chain_ptr == nullptr) head_ptr_ = nullptr; // Original bag is empty else { // Copy first node head_ptr_ = new Node<T>(); head_ptr_->setItem(orig_chain_ptr->getItem());

// Copy remaining nodes Node<T>* new_chain_ptr = head_ptr_; // Points to last node in new chain orig_chain_ptr = orig_chain_ptr->getNext(); // Advance original-chain pointer while (orig_chain_ptr != nullptr) { // Get next item from original chain T next_item = orig_chain_ptr->getItem(); // Create a new node containing the next item Node<T>* new_node_ptr = new Node<T>(next_item); // Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr);

// Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); // Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); } // end while new_chain_ptr->setNext(nullptr); // Flag end of chain } // end if } // end copy constructor

!72

The copy constructorA constructor whose parameter is an

object of the same class

Called when object is initialized with a copy of another object, e.g. LinkedBag<string> my_bag = your_bag;

Copy first node

Copy item from current node

Create new node with item

Connect new node to new chain

Advance pointer traversing new chain

Advance pointer traversing original chain

Two traversing pointers One to new chain, one

to original chain

while

Signal last node

Page 73: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

!73

orig_chain_ptr

new_chain_ptr

// Copy first node head_ptr_ = new Node<T>(); head_ptr_->setItem(orig_chain_ptr->getItem());

// Copy remaining nodes Node<T>* new_chain_ptr = head_ptr_; // Points to last node in new chain orig_chain_ptr = orig_chain_ptr->getNext();

Deep vs Shallow Copy

Page 74: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

!74

orig_chain_ptr

new_chain_ptr

// Copy first node head_ptr_ = new Node<T>(); head_ptr_->setItem(orig_chain_ptr->getItem());

// Copy remaining nodes Node<T>* new_chain_ptr = head_ptr_; // Points to last node in new chain orig_chain_ptr = orig_chain_ptr->getNext();

Deep vs Shallow Copy

Page 75: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

!75

orig_chain_ptr

new_chain_ptr

Deep vs Shallow Copy

while (orig_chain_ptr != nullptr) { // Get next item from original chain T next_item = orig_chain_ptr->getItem(); // Create a new node containing the next item Node<T>* new_node_ptr = new Node<T>(next_item); // Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr);

// Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); // Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); }

Page 76: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

!76

orig_chain_ptr

new_chain_ptr

Deep vs Shallow Copy

while (orig_chain_ptr != nullptr) { // Get next item from original chain T next_item = orig_chain_ptr->getItem(); // Create a new node containing the next item Node<T>* new_node_ptr = new Node<T>(next_item); // Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr);

// Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); // Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); }

Page 77: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

!77

orig_chain_ptr

new_chain_ptr

Deep vs Shallow Copy

while (orig_chain_ptr != nullptr) { // Get next item from original chain T next_item = orig_chain_ptr->getItem(); // Create a new node containing the next item Node<T>* new_node_ptr = new Node<T>(next_item); // Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr);

// Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); // Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); }

Page 78: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

!78

orig_chain_ptr

new_chain_ptr

Deep vs Shallow Copy

while (orig_chain_ptr != nullptr) { // Get next item from original chain T next_item = orig_chain_ptr->getItem(); // Create a new node containing the next item Node<T>* new_node_ptr = new Node<T>(next_item); // Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr);

// Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); // Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); }

Page 79: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

!79

orig_chain_ptr

new_chain_ptr

Deep vs Shallow Copy

while (orig_chain_ptr != nullptr) { // Get next item from original chain T next_item = orig_chain_ptr->getItem(); // Create a new node containing the next item Node<T>* new_node_ptr = new Node<T>(next_item); // Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr);

// Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); // Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); }

Page 80: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

!80

orig_chain_ptr

new_chain_ptr

Deep vs Shallow Copy

while (orig_chain_ptr != nullptr) { // Get next item from original chain T next_item = orig_chain_ptr->getItem(); // Create a new node containing the next item Node<T>* new_node_ptr = new Node<T>(next_item); // Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr);

// Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); // Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); }

Page 81: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

!81

orig_chain_ptr

new_chain_ptr

Deep vs Shallow Copy

while (orig_chain_ptr != nullptr) { // Get next item from original chain T next_item = orig_chain_ptr->getItem(); // Create a new node containing the next item Node<T>* new_node_ptr = new Node<T>(next_item); // Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr);

// Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); // Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); }

Page 82: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

!82

orig_chain_ptr

new_chain_ptr

Deep vs Shallow Copy

while (orig_chain_ptr != nullptr) { // Get next item from original chain T next_item = orig_chain_ptr->getItem(); // Create a new node containing the next item Node<T>* new_node_ptr = new Node<T>(next_item); // Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr);

// Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); // Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); }

Page 83: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

Efficiency Considerations

Every time you pass or return an object by value: - Call copy constructor - Call destructor

For linked chain: - Traverse entire chain to copy ( n “steps”) - Traverse entire chain to destroy ( n “steps”)

Preferred: myFunction(const MyClass& object);

!83

Page 84: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

The Class LinkedBag#ifndef LINKED_BAG_H_ #define LINKED_BAG_H_

#include "BagInterface.hpp" #include “Node.hpp"

template<class T> class LinkedBag { public: LinkedBag(); LinkedBag(const LinkedBag<T>& a_bag); // Copy constructor ~LinkedBag(); // Destructor int getCurrentSize() const; bool isEmpty() const; bool add(const T& new_entry); bool remove(const T& an_entry); void clear(); bool contains(const T& an_entry) const; int getFrequencyOf(const T& an_entry) const; std::vector<T> toVector() const;

private: Node<T>* head_ptr_; // Pointer to first node int item_count_; // Current count of bag items

// Returns either a pointer to the node containing a given entry // or the null pointer if the entry is not in the bag.Node<T>* getPointerTo(const T& target) const;

}; // end LinkedBag

#include “LinkedBag.cpp" #endif //LINKED_BAG_H_

!84

Efficient

ExpensiveTHINK

WORST CASE

Page 85: New Linked-Based Implementation · 2019. 9. 20. · Fish(); //default constructor Fish(std::string name, bool domestic = false, bool predator = false);//parameterized constructor

The Class LinkedBag#ifndef LINKED_BAG_H_ #define LINKED_BAG_H_

#include "BagInterface.hpp" #include “Node.hpp"

template<class T> class LinkedBag { public: LinkedBag(); LinkedBag(const LinkedBag<T>& a_bag); // Copy constructor ~LinkedBag(); // Destructor int getCurrentSize() const; bool isEmpty() const; bool add(const T& new_entry); bool remove(const T& an_entry); void clear(); bool contains(const T& an_entry) const; int getFrequencyOf(const T& an_entry) const; std::vector<T> toVector() const;

private: Node<T>* head_ptr_; // Pointer to first node int item_count_; // Current count of bag items

// Returns either a pointer to the node containing a given entry // or the null pointer if the entry is not in the bag.Node<T>* getPointerTo(const T& target) const;

}; // end LinkedBag

#include “LinkedBag.cpp" #endif //LINKED_BAG_H_

!85

Efficient

ExpensiveTHINK

WORST CASE