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

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

View Full Document Right Arrow Icon
1 Week 6
Background image of page 1

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

View Full DocumentRight Arrow Icon
Topics Nodes of a list. A List of nodes. List iterators. Miscellaneous 2
Background image of page 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 11

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

View Full DocumentRight Arrow Icon
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
Background image of page 12
List Illustration: list.cpp (7) int main() { List<string> abba; abba.pushH( string("Bjorn") ); abba.pushT( string("Benny") );
Background image of page 13

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

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online