ADT List Pointer Implementation 3-28-2013 ADT list implemented - - PowerPoint PPT Presentation

adt list
SMART_READER_LITE
LIVE PREVIEW

ADT List Pointer Implementation 3-28-2013 ADT list implemented - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

ADT List Pointer Implementation 3-28-2013

slide-2
SLIDE 2

 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)

slide-3
SLIDE 3

Exampl mples s done e in cl class

slide-4
SLIDE 4

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) ) {} }; };

slide-5
SLIDE 5

 A str truc uct is the same as a class…

 except that the default visibility for a struct

uct is publ blic ic.

 Generally struc tructs are used to define classes that

  • nly contain public data fields.

 Constructors may be provided.  Other member functions (operators) are usually not

defined for stru ruct cts.

slide-6
SLIDE 6

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

slide-7
SLIDE 7

Node* de* ptr ptr = t = tom

  • m->ne

>next; xt; tom tom->nex next = t t = tom

  • m->ne

>next xt->next ext; delet lete e ptr tr;

slide-8
SLIDE 8

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

slide-9
SLIDE 9

 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

slide-10
SLIDE 10

 represent a list with three data members:

 head of the list  tail of the list  current size of the list

 represent contents with nodes

slide-11
SLIDE 11

template<classT> // // list.h, , pointer nter implem ementati ntation

  • n

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 ...

slide-12
SLIDE 12

template<classT> // // list.h, , pointer nter implem ementati ntation

  • n

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 ...

slide-13
SLIDE 13

// // 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

slide-14
SLIDE 14

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 ...

slide-15
SLIDE 15

 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.

slide-16
SLIDE 16

 We use an iterator like a pointer // access ess each ch element ement of a_lis list and d process

  • cess 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

slide-17
SLIDE 17

 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

slide-18
SLIDE 18

 Implementation of Linked Lists

 Maciel: Chapter 13