# Week 6 - Week 6 1 Topics Nodes of a list A List of nodes...

This preview shows pages 1–14. Sign up to view the full content.

1 Week 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Topics Nodes of a list. A List of nodes. List iterators. Miscellaneous 2
List Nodes Illustration (1) \$ cat nodes.cpp #include <iostream> #include <string> using namespace std; template <typename T> class Node { T data; Node<T> *next; public: void show() { cout << data; if(next) { cout << ’ ’; next -> show(); } } } ; 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
List Nodes Illustration (2) int main(int ac, char *av[]) { Node<string> *n = 0; int i; for(i=0; av[i]; i++) { n = new Node<string>( string(av[i]), n ); } n->show(); cout << endl; return 0; } 4 \$ g++ nodes.cpp \$ a.out 1 22 333 44444 55555 cs1280 cs1280 55555 44444 333 22 1 a.out
The Problem of the Previous List Node Implementation The above implementation supports only insertion at the head The list user must maintain a pointer to point to the first node of the list 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Separation of a List and Its Nodes It is better to model a list with two objects: the list container and the nodes. Only the list container will know where is the first node Desired operations such as add new nodes or delete nodes from a list will be member functions of the list. 6
List Illustration: list.cpp (1) #include <iostream> #include <string> using namespace std; template <typename T> class List; // forward declaration template <typename T> class Node { T data; Node<T> *next; friend class List<T>; public: } ; 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
List Illustration: list.cpp (2) template <typename T> class List { Node<T> *first; public: List() : first(0) {} ~List() { Node<T> *q; for(Node<T> *p=first; p;) { q = p; p = p -> next; delete q; } } 8
List Illustration: list.cpp (3) first = new Node<T>( D, first ); } if(!first) return false; D = first -> data; Node<T> * q = first; first = first -> next; delete q; return true; } 9

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
List Illustration: list.cpp (4) if(!first) pushH(D); Node<T> * q = first; while(q -> next) q = q -> next; q->next = new Node<T>(D, 0); } 10
List Illustration: list.cpp (5) if(!first) return false; Node<T> *p = first, *q = p -> next; while(q && q -> next) { p = q; q = q -> next; } if(!q) return popH(D); p->next = 0; D = q -> data; delete q; return true; } 11

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
List Illustration: list.cpp (6) void show() { Node<T> *p = first; if(p) cout << p -> data; if(p) p = p -> next; while(p) { cout << p -> data << endl ; p = p -> next; } } }; 12
List Illustration: list.cpp (7) int main() { List<string> abba; abba.pushH( string("Bjorn") ); abba.pushT( string("Benny") );

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 41

Week 6 - Week 6 1 Topics Nodes of a list A List of nodes...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online