Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.

Similar presentations


Presentation on theme: "1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation."— Presentation transcript:

1 1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation

2 2 Circular, Doubly-Linked Lists STL list implemented using circular, doubly- linked list (& header node) int A[ ] = { 4, 9, 3, 2 }; list myList (A, A + 4); No data field in header

3 3 Building Lists Three constructors 0.0 (a) list reals (8); 8:30 (b) list times (6, time24 (8, 30)); array (c) list types (strArr, strArr + 3); listvector

4 4 CLASS list Constructors list (); Create an empty list. This is the default constructor. list (size_t n, const T& value = T()); Create a list with n elements, each having a specified value. If the value argument is omitted, the elements are filled with the default value for type T. Type T must have a default constructor, and the default value of type T is specified by the notation T(). list (InIter first, InIter last); Initialize the list, using the range [first, last).

5 Iterators To insert to or remove from middle of vector/list Keeps track of current position in a vector/list Can also be used to traverse a vector/list 5

6 Traversing a Vector Consider: for (int i = 0; i != v.size(); ++i) cout << v[i] << endl; 6 for (vector ::iterator itr = v.begin(); itr != v.end(); ++i) cout << itr.??? << endl;

7 7 CLASS list::iterator Operations * :Accesses the value of the item currently pointed to by the iterator.*iter; ++ :Moves the iterator to the next item in the list. iter++; ++ iter; // both pre and post -- :Moves the iterator to the previous item in the list. iter--; --iter;// both pre and post == :Takes two iterators as operands and returns true when they both reference the same item in the list. iter1 == iter2 != :Returns true when the two iterators do not reference the same item in the list. iter1 != iter2

8 Traversing a Vector Consider: for (int i = 0; i != v.size(); ++i) cout << v[i] << endl; 8 for (vector ::iterator itr = v.begin(); itr != v.end(); ++i) cout << *itr << endl;

9 Traversing a Vector 9 vector ::iterator itr = v.begin(); while(itr != v.end()) cout << *itr++ << endl; for (vector ::iterator itr = v.begin(); itr != v.end(); ++i) cout << *itr << endl; But why bother?

10 Operations Requiring Iterators iterator insert(iterator pos, const Object & x) iterator erase(iterator pos) iterator erase(iterator start, iterator end) 10

11 11 List Insert list li; // then populate list ::iterator newElt = li.insert (iter, 4);

12 12 List Erase list li; // then populate list ::iterator i = li.erase (iter); cout << *i;

13 13 Ordered lists // Walk list to find insertion point list ::iterator curr; for (curr = lst.begin (); curr != lst.end () && item > *curr; ++cur) ; lst.insert (curr, item); What if inserting item = 83?

14 14 Node Composition struct Node { Node (const T& v = T (), Node* n = NULL, Node* p = NULL) : data (v), next (n), prev (p) { } T data; Node* next; Node* prev; };

15 15 List Class class List { // Insert Node struct def. Node* d; // ptr to dummy header size_t sz; // size public: List () : d (new Node ()), sz (0) { d->next = d->prev = d; } ~List () { while (! empty ()) pop_back (); delete d; } void push_front (const T& v); void push_back (const T& v); void pop_front (); void pop_back (); // Arbitrary location insert and erase };

16 16 Pushing a Value prev next header newNode Afterinsert:List with one element

17 17 Pushing Values void push_front (const T& v) { Node* p = d; Node* s = d->next; Node* n; n = new Node (v, s, p); p->next = n; s->prev = n; ++sz; } void push_back (const T& v) { Node* p = d->prev; Node* s = d; Node* n; n = new Node (v, s, p); p->next = n; s->prev = n; ++sz; }

18 18 Inserting a Node newNode = new Node (item, curr, prevNode); // 2 & 1 prevNode->next = newNode; // 3 curr->prev = newNode; // 4 ++sz; Lobj.insert (curr, item);

19 19 Erasing a Node curr->prev->next = curr->next; // 1 curr->next->prev = curr->prev; // 2 delete curr; --sz; lObj.erase (curr);


Download ppt "1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation."

Similar presentations


Ads by Google