ADT List Pointer Implementation 3-28-2013
ADT list implemented with nodes and pointers Singly-linked list iterators & const_iterators Reading: Maciel, Chapter 13 HW#4 due: Wednesday, 4/03 (new due date)
Exampl mples s done e in cl class
struc uct Node e { T T dat ata; a; Node* e* next; t; // Co Constructor tructor Node( e(const const T& & data_item _item, Node* e* next_ptr t_ptr = N NULL) LL) : d data(da data_i ta_item tem), next( t(ne next xt_ptr _ptr) ) {} }; };
A str uct is the same as a class… truc except that the default visibility for a struct uct is ic . publ blic Generally struc truct s are used to define classes that only contain public data fields. Constructors may be provided. Other member functions (operators) are usually not defined for stru ct s. ruct
Node* de* bo bob = n b = new ew Nod Node("Bo e("Bob") b"); bob bob->nex >next = h = harr rry->next; >next; // // st step 1 p 1 harry rry->next = ext = bo bob; b; // st // step ep 2
Node* de* ptr ptr = t = tom om->ne >next; xt; tom tom->nex next = t t = tom om->ne >next xt->next ext; delet lete e ptr tr;
Think ink throu rough gh your r solution: lution: does es your r code work k for eve very ry case? se? Test your solution on ... the empty list a list containing only one node (which is both first and last in the list) a list containing two or more nodes a node in the middle of the list the last node in the list the first node in the last
default constructor create an empty list push_back() add an item to the end of the list pop_back() remove the last item from the list back() return the last item in the list insert insert an item at a given position in the list erase delete an item at a given position from the list traverse: need list iterator
represent a list with three data members: head of the list tail of the list current size of the list represent contents with nodes
template<classT> // // list.h, , pointer nter implem ementati ntation on class list { private: /* d decl clare are Node struct ct here */ Node* head; // hea ead of the e list Node* tail; // tail of the list int num_items; // size e of the list ...
template<classT> // // list.h, , pointer nter implem ementati ntation on class list { private: /* d decl clare are Node struct ct here */ #i #incl clude ude <node de.h .h> Node* head; // head of the list Node* tail; // tail of the list int num_items; // size e of the list ...
// // node.h e.h #ifndef NODE_H_ #define NODE_H_ /* A A no node e is the buildi lding ng block ck for a s singly ly-linke linked d list. . */ struct Node { T data; Node* next; Node(const T& data_item, Node* next_ptr = NULL) : data(data_item), next(next_ptr) {} }; #endif
template<class T> // // list.h .h, pointer ter implemen lementa tatio tion class list { public: /* What t else will list need? ? A p publi lic c iterator tor */ private: /* d decl clare are Node struct ct here */ #i #incl clude ude <node de.h .h> Node* head; // head of the list Node* tail; // tail of the list int num_items; // size e of the list ...
An Iterator is an object that can “iterate” or “traverse” a collection of items, starting at the first item (if there is a first item) and progressing through each item on the list until it gets to the end of the list. Each standard library container (e.g. the list) provides both an iterator and a const_iterator The operations on them are the same, except When a const_iterator is dereferenced ( operator* ), the value of the item referenced cannot be changed.
We use an iterator like a pointer // access ess each ch element ement of a_lis list and d process ocess it for (list<T>::iterator iter = a_list.begin(); iter != a_list.end(); ++iter) { T next_element = *iter; // do something with next_element (*iter) } But an iterator is not the same as a pointer ++iter advances to the next item on the list
Limitations of a singly-linked list include: can insert only after a referenced node removing node requires pointer to previous node can traverse list only in the forward direction We can remove these limitations: Add a pointer in each node to the previous node: This is called a doub ubly-linked inked list st Another modification: add a dummy first node, and link the last node to the first node This is a circul rcular r doubly ubly-linked linked list ist
Implementation of Linked Lists Maciel: Chapter 13
Recommend
More recommend