Lec12 - Lecture 12: More Linked Lists PIC 10B Todd Wittman...

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

View Full Document Right Arrow Icon
1 Lecture 12: More Linked Lists PIC 10B Todd Wittman Recall : The 3 Classes s Our linked list will be built on 3 inter-dependent classes. s Node -- Stores one block of data. Private : T data, Node<T>* prev, Node<T>* next s LinkedList -- A list of nodes. Private : Node<T>* first, Node<T>* last s Iterator -- Marks our current position in the list. Private : Node<T>* position Functions : forward(); backward(); T get(); bool isNull(); data next prev 10 20 30 NULL NULL first last position
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 The Iterator Class s The LinkedList class needs to know the Iterator position for its insert and erase functions. template <typename T> class Iterator { public: Iterator(); T get() const; //Returns data value we are pointing to. void forward(); //Moves position forward one node. void backward(); //Moves position backward one node. bool isNull(); //Returns true if iterator points to NULL. template <typename T> friend class LinkedList; private: Node<T>* position; }; position The Iterator Class template <typename T> Iterator<T>::Iterator() { position = NULL; } template <typename T> T Iterator<T>::get() const { return position->data; } template <typename T> bool Iterator<T>::isNull { return (position==NULL); } template <typename T> void Iterator<T>::forward() { position = position->next; } template <typename T> void Iterator<T>::backward() { position = position->prev; }
Background image of page 2
3 Sec 16.2 : The LinkedList Class s We’re using dynamically allocated memory, so we need to define the Big 4. template <typename T> class LinkedList { public: LinkedList(); ~LinkedList(); ***We’ll add more functions: insert, erase, operators*** private: Node<T>* first; //Pointer to first node in list. Node<T>* last; //Pointer to last node in list. }; 10 20 30 NULL NULL first last LinkedList Constructor s The default constructor just sets up an empty list. template <typename T> LinkedList<T>::LinkedList( ) { first = NULL; last = NULL; } s The template allows us to store lists of ints, strings, etc. LinkedList<string> words;
Background image of page 3

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

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

This note was uploaded on 11/19/2011 for the course PIC 10B taught by Professor Wittman during the Winter '08 term at UCLA.

Page1 / 10

Lec12 - Lecture 12: More Linked Lists PIC 10B Todd Wittman...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online