comp103 - linked lists (part b)1 chapter 17 linked list as objects

27
COMP103 - Linked Lists (P art B) 1 Chapter 17 Linked List as Objects

Post on 20-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

COMP103 - Linked Lists (Part B) 1

Chapter 17Linked List as

Objects

COMP103 - Linked Lists (Part B) 2

Linked List Design

We completed the basic operations in a linked list (Part A): Traverse the list Search for an item Insert an item Delete an item

The last topic in the course is to implement linked list as objects.

COMP103 - Linked Lists (Part B) 3

Where are we? So far, we have not treated linked list as

an object. We have not applied the principle of

Abstract Data Type. It is declared as struct type, therefore,

the main program CAN access directly the fields in a NODE, such as data and link.

This creates problems when we change the implementation of the NODE don’t want to release the internal structure of

NODE for security reasons

COMP103 - Linked Lists (Part B) 4

Linked List DesignNode declared as a class: 2 private members:

Data (relevant information) link (pointer to the next Node)

Private constructor Friend class List (to create and access a node)

class Node{ private:

int data; Node* link; friend class List; Node (int); // constructor

}; // class Node

// Node ConstructorNode :: Node (int dataIn){

data = dataIn;link = NULL;

}

COMP103 - Linked Lists (Part B) 5

Friend class List as object

addToList

deleteFromList

2 public functions Also keeps the 3 required pointers, as private variables

COMP103 - Linked Lists (Part B) 6

Class Definition of Listclass List{ private:

Node* pHead;Node* pPre; // predecessorNode* pCur; // current position in list

bool addNode(int item);void deleteNode ();bool searchList (int argument);

public:List (); // constructor~List (); // destructorint addToList (int dataIn);bool deleteFromList (int key);

};

COMP103 - Linked Lists (Part B) 7

addToList (public function)// Return 0 successful add// Return 1 item already in the list// Return 2 dynamic memory is fullint List::addToList(int item){

int success;success = 0; // Default is successfulif (searchList(item) == true) success = 1; // Found duplicate dataelse if (addNode(item) == false) success = 2;

// Memory overflowreturn success;

}

Calls 2 private functions:

• searchList• addNode

COMP103 - Linked Lists (Part B) 8

searchList (private function)bool List::searchList(int target) {

bool found;pPre = NULL;pCur = pHead;// Search until target <= list datawhile (pCur && target > pCur->data) { pPre = pCur; pCur = pCur->link; }// Determine if target foundif (pCur && target == pCur->data) found = true; else found = false;return found;

}

COMP103 - Linked Lists (Part B) 9

addNode (private function)

bool List::addNode(int item){

Node* pNew;pNew = new Node(item);if (!pNew)return false; // memory overflowif (pPre == NULL){

// Add before first node or to empty list pNew->link = pHead; pHead = pNew;} else {

// Add in the middle or at the end pNew->link = pPre->link; pPre->link = pNew;} return true;

}

COMP103 - Linked Lists (Part B) 10

deleteFromList (public function)

bool List::deleteFromList(int key) { if (searchList(key)) { deleteNode(); return true; // found } else return false; // not found}

Calls 2 private functions: • searchList• deleteNode

COMP103 - Linked Lists (Part B) 11

deleteNode (private function)

void List::deleteNode(){ if (pPre == NULL) pHead = pCur->link; // first node is deleted else pPre->link = pCur->link; // node at other location is deleted

delete (pCur); return;}

COMP103 - Linked Lists (Part B) 12

List constructor (default)

List::List(){ pHead = NULL; pPre = NULL; pCur = NULL;}

When a new list is constructed, we must set pHead = NULL, because the list is initially empty.

pPre and pCur must also be NULL, since there is no node to point to.

COMP103 - Linked Lists (Part B) 13

List destructor

List::~List(){ Node *pDelete; Node *pTemp; pDelete = pHead; // while there are more nodes while (pDelete!=NULL) {

pTemp = pDelete->link;delete pDelete; // delete the current nodepDelete = pTemp; // go to the next node

}}

When a list is destroyed, we must delete all the nodes.

To do this, we traverse the list and we delete the nodes one by one.

COMP103 - Linked Lists (Part B) 14

Recall the declarations of Node and List

class List{ private: Node* pHead; Node* pPre; // predecessor Node* pCur; // current position in list

bool addNode(int item); void deleteNode (); bool searchList (int argument);

public: List (); // constructor ~List (); // destructor int addToList (int dataIn); bool deleteFromList (int key);};

class Node{ private: int data; Node* link; friend class List; Node (int); }; // class Node

COMP103 - Linked Lists (Part B) 15

Problem!! The data structure is in the node class, Inside the List we do not know and cannot

access the data in the node. Outside the node, applications cannot

access the pHead, pPre, pCur, etc.

How can we traverse the item in the List from a program?

COMP103 - Linked Lists (Part B) 16

SolutionAnother ADT (class) ListIterator enables one to

manipulate a List object.

We can “use” ListIterator to perform the following functions:• Set the manipulation to start at the first item of the list:

function reset• Advance to the next node in the list:

function operator++• Determine whether the current node is valid or not:

function operator!• Access the content of the current node:

function operator*

COMP103 - Linked Lists (Part B) 17

Iterator, List & Node

COMP103 - Linked Lists (Part B) 18

ListIterator Class Definition

class ListIterator{ private:

Node *pWalker; public:// set pWalker to the first node

void reset(List &pList);// returns data in the current node

int operator*();// check whether pWalker points to a valid node

bool operator!();// advance to next node

Node* operator++(int);};

COMP103 - Linked Lists (Part B) 19

Access to private data ListIterator must access the private

members of Node and List Therefore, ListIterator must be a friend

of both Node and List

class Node{ … // add this declaration friend class ListIterator; …};

class List{ … // add this declaration friend class ListIterator; …};

COMP103 - Linked Lists (Part B) 20

reset and operator!

// sets current pointer to headervoid ListIterator :: reset (List& list){ pWalker = list.pHead;}

// checks whether pointer is validbool ListIterator :: operator! (){ return pWalker != NULL;}

main() { List list; ListIterator listWalker; listWalker.reset(list)

… // add items to list

while (!listWalker) { …

}}

main() { List list; ListIterator listWalker; listWalker.reset(list)

… // add items to list

while (!listWalker) { …

}}

This statement will become false when we reach the end of the list

COMP103 - Linked Lists (Part B) 21

operator++()

// advance to the next node

Node* ListIterator :: operator++ (int)

{

if (pWalker)

pWalker = pWalker->link;

return pWalker;

}

main() { List list; ListIterator listWalker; listWalker.reset(list)

… // add items to list

while (!listWalker) { …listWalker++;

}}

main() { List list; ListIterator listWalker; listWalker.reset(list)

… // add items to list

while (!listWalker) { …listWalker++;

}}

The syntax operator++(int) is used to denote the postfix operator: listWalker++

If you use operator++(void) instead, you declare a prefix operator: ++listWalker

Will return to this later!!

COMP103 - Linked Lists (Part B) 22

operator*()

// returns data in the current nodeint ListIterator :: operator* (){ if (!pWalker) { cout << "Invalid list reference\n"; exit (105); } else return pWalker->data;}

main() { List list; ListIterator listWalker; listWalker.reset(list)

… // add items to list

while (!listWalker) { …cout << *listWalker;listWalker++;

}}

main() { List list; ListIterator listWalker; listWalker.reset(list)

… // add items to list

while (!listWalker) { …cout << *listWalker;listWalker++;

}}

COMP103 - Linked Lists (Part B) 23

Example: calculate average

const int LIST_SIZE = 100;

// Prototype Declarationsvoid buildList(List &list);void printAvrgList(List &list);void printList(List &list);

void main(){ List list;

buildList(list); printList(list); printAvrgList(list);}

COMP103 - Linked Lists (Part B) 24

buildList(List &list)void buildList (List &list){ int datum, result; int i;

for (i = 0; i < LIST_SIZE; i++) { cout << “input integer: “ << endl; cin >> datum; // input integer result = list.addToList (datum); if (result == 1) cout << "Duplicate data: " << datum << endl; else if (result == 2) cout << "\a\aERR 200: Memory Overflow\n"; } // end for}

COMP103 - Linked Lists (Part B) 25

printList(List &list)

void printList(List& list){ ListIterator listWalker; listWalker.reset(list); cout << “ The list is “<< endl; while (!listWalker) { cout << “data: “ << *listWalker << endl; listWalker++; } }

COMP103 - Linked Lists (Part B) 26

printAvrgList(List &list)void printAvrgList (List& list) {

int count = 0, sum = 0;float avrg;ListIterator listWalker;listWalker.reset(list); // set to header

while (!listWalker) { sum += *listWalker; // sum = sum + data count++; // increment count listWalker++; // next node

} if (count) avrg = (float) sum / count;else avrg = 0;

cout << "\nTotal value: " << sum << "\n for " << count << " items.“ << "\nAverage is: " << avrg << endl;

}

COMP103 - Linked Lists (Part B) 27

The End