Vous êtes sur la page 1sur 29

Object-Oriented Programming with Link List

This lecture prepared by the instructors at the University of Manitoba in Canada and has been modified by Dr. Ahmad Reza Hadaegh
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 1

Linked Lists and Classes


class node; typedef Node* NodePtr; class node { public: int number; NodePtr next; }; As before

but in addition, now have another class:


A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 2

Linked Lists and Classes


class LL { private: NodePtr top; void destroy(NodePtr&); public: LL(); ~LL(); void buildLL(int); void printLL(); }; - A linked list is now defined as a class itself
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 3

Linked Lists and Classes

void main () { LL list1, list2, list3; }

list1, list2, and list3 are three instances of the LL class. list1, list2, and list3 are also called objects.

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page

Linked Lists and Classes


- A class is made up of data and methods

class LL { datum private: NodePtr top; void destroy(NodePtr&); This class definition public: has 5 method prototypes method LL(); and one datum. ~LL(); void buildLL(int); void printLL(); };
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 5

Linked Lists and Classes


- Data and methods of a class definition can be public or private

- Public: - Accessible by the world (scoping rules still apply) - Private: - Accessible only by member functions

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page

Linked Lists and Classes


- Placing data and methods within the private portion of a class definition protects them from outside harm - Sometimes called data hiding

- The only way hidden data and methods can be accessed is indirectly via public methods
- Methods and data are bound together - Encapsulation

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page

Linked Lists and Classes


class LL { private: NodePtr top; Can only be void destroy(NodePtr&); accessed by public: LL(); ~LL(); void buildLL(int); A linked list (LL) is now void printLL(); an encapsulated package }; of data and a limited number of methods that work upon that data.
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 8

Linked Lists and Classes


- In the procedural linked list example - Top pointers were set to NULL before use - Nodes within linked list were deleted before linked list left scope - Remember that the top pointer has been defined as automatic and will cease to exist when the linked list leaves scope - The allocated memory will live on until deleted

- Programmers responsibility to ensure initialization and destruction takes place properly


A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 9

Linked Lists and Classes


- Objects can have constructors - Method that is automatically invoked when an object is created - Allows for initialization to take place - Objects can have destructors - Method that is automatically invoked when an object leaves scope - Allows for clean up to take place - Constructors and destructors are not normally called explicitly

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page 10

Linked Lists and Classes


class LL { private: NodePtr top; void destroy(NodePtr&); public: LL(); ~LL(); void buildLL(int); void printLL(); };
A.R. Hadaegh Dr. Ahmad R. Hadaegh

constructor destructor

National University

Page 11

Linked Lists and Classes


Scope resolution operator -- lets the compiler know that what youre defining is a member function of the class LL

LL::LL() {
// Linked list constructor

top = NULL; }

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page 12

Linked Lists and Classes

LL::~LL() {
// Linked list destructor -- will call a private // member function destroy that will perform // a deep deletion of the linked list object

destroy (top) ; } This destructor will automatically be invoked when an instance of LL leaves scope
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 13

Linked Lists and Classes


void LL::destroy(NodePtr& head) {
// Deep delete a linked list pointed at by head

NodePtr curr, temp;


curr = head; while (curr != NULL) { temp = curr; curr = curr->next; delete temp; } head = NULL; }
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 14

Very much like a regular procedural function -- does not operate directly on data components of the object

Linked Lists and Classes


void LL::buildLL(int size) {
// "build will create an ordered linked list consisting // of the first "size" even integers

NodePtr newNode; int i;

destroy(top); //Destroy self before building linked list. for (i=size;i>0;i--) { newNode = new (Node); newNode->next = top; newNode->number = i*2; top = newNode; } }
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 15

Linked Lists and Classes


void LL::printLL() {
// "print" will output the entire linked list to // the standard output device -// one "number" per line.

NodePtr curr; curr = top; while (curr != NULL) { cout << curr->number << endl; curr = curr->next; } }
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 16

Linked Lists and Classes


Member functions are invoked as follows: void main () { LL list1, list2, list3; list1.buildLL(10); list2.buildLL(20); list3.buildLL(50); list1.buildLL(40); list1.printLL(); list2.printLL(); list3.printLL(); }
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 17

Linked Lists and Classes


Member functions are called as if they were a part of a structure -- which they are

void main () { LL list1, list2, list3; list1.buildLL(10); list2.buildLL(20); list3.buildLL(50);


list1.buildLL(40);

list1.printLL(); list2.printLL(); list3.printLL(); }


A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 18

Linked Lists and Classes


Constructor called 3 times here void main () { LL list1, list2, list3;

private member function destroy called 4 times indirectly here

list1.buildLL(10); list2.buildLL(20); list3.buildLL(50) list1.buildLL(40); list1.printLL(); list2.printLL(); list3.printLL(); } Destructor called 3 times here
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 19

Shallow vs. Deep Copy


Assume the following:

void main () { LL list1, list2; list1.buildLL(4); list2 = list1;

Constructor called -- top pointer set to NULL Build a linked list of size 4 Shallow copy of list1 made to list2

Destructor called for both list1 and list2


A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 20

Shallow vs. Deep Copy


void main () { LL list1; list1.buildLL(4); LL list2 = list1; } A shallow copy in this circumstance simply means that the data members of list1 are copied to the data members of list2 -- the top pointer of list1 and list2 will now point to the same place
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 21

Shallow vs. Deep Copy


void main () { LL list1; list1.buildLL(4);
LL list2 = list1; }
list1.top 2 list2.top
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 22

Creating an alias is useful, but not likely the result you were looking for here -- really want a copy of the entire linked list

Shallow vs. Deep Copy


void main () { LL list1, list2; list1.buildLL(4); list2 = list1; }
list1.top list2.top 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh

Creating an alias is useful, but not likely the result you were looking for here -- really want a copy of the entire linked list

2 4
National University

4
6

6 8

Page 23

Shallow vs. Deep Copy


- A deep copy implies that the actual structure is copied -- not the data members - It is possible to write a member function that does an explicit deep copy of one linked list to another, but there are times when a deep copy would best be done implicitly - Initialization (as in the previous example) - Passing a linked list to a function as a value parameter - Would then be able to protect a linked list much as you can protect an integer - Be cognisant of the overhead involved
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 24

Shallow vs. Deep Copy


- Returning a linked list from a function - What problem would you currently have if you tried to return a linked list from a function?

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page 25

Copy Constructor
- Implicit deep copying can be performed by creating a copy constructor - Copy constructor is a function that will automatically be called when a deep copy is likely required - As in the previous three cases

So...

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page 26

Copy Constructor
class LL { private: NodePtr top; void copy(NodePtr, NodePtr&); // Deep copy of 1st to 2nd void destroy(NodePtr&);

public: LL(); ~LL(); LL(const LL&); // Copy constructor void buildLL(int); void printLL(); };
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 27

void LL::copy(NodePtr atop, NodePtr& btop) { // Performs a deep copy from linked list pointed to // by atop to linked list pointed to be btop. NodePtr acurr, bcurr; destroy (btop); // Free any storage used by target linked list if (atop != NULL) { btop = new (node); btop->number = atop->number; acurr = atop; bcurr = btop; while (acurr->next != NULL) { bcurr->next = new (node); acurr = acurr->next; bcurr = bcurr->next; bcurr->number = acurr->number; } bcurr->next = NULL; }
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 28

Copy Constructor

LL::LL(const LL& source) {


// Copy constructor -- does a deep copy of linked list.

copy (source.top, top);


}

A.R. Hadaegh Dr. Ahmad R. Hadaegh

National University

Page 29

Vous aimerez peut-être aussi