1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and...

19

Click here to load reader

description

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

Transcript of 1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and...

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

1

Circular, Doubly-Linked ListsCircular, Doubly-Linked Lists

Node CompositionNode Composition

List ClassList Class

Pushing and Popping ValuesPushing and Popping Values

Insert and Erase at Arbitrary Insert and Erase at Arbitrary LocationsLocations

List ImplementationList Implementation

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

2

Circular, Doubly-Linked ListsCircular, Doubly-Linked Lists STL list implemented using circular, doubly-

linked list (& header node)

head er23 4 9

int A[ ] = { 4, 9, 3, 2 };list<int> myList (A, A + 4);

No data field in header

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

3

Building ListsBuilding Lists

Three constructors

0.00.0 0.00.0 0.00.0 0.00.0

(a) list<double> reals (8);

8:308:30 8:308:30 8:30 8:30

(b) list <time24> times (6, time24 (8, 30));

array

(c) list<string> types (strArr, strArr + 3);

listvector

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

4

CLASS list Constructors <list>

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

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

IteratorsIterators 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

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

Traversing a VectorTraversing a VectorConsider:

for (int i = 0; i != v.size(); ++i) cout << v[i] << endl;

6

for (vector<int>::iterator itr = v.begin();itr != v.end(); ++i)

cout << itr.??? << endl;

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

7

CLASS list::iterator Operations <list>

*:Accesses the value of the item currently pointed to by the iterator.

*iter;*iter;

++: Moves the iterator to the next item in the list. iter++; ++ iter; iter++; ++ iter; // both pre and post// both pre and post

--: Moves the iterator to the previous item in the list.iter--; --iter;iter--; --iter; // both pre and post// both pre and post

==: Takes two iterators as operands and returns true when they both reference the same item in the list.

iter1 == iter2iter1 == iter2!=: Returns true when the two iterators do not reference the

same item in the list.iter1 != iter2iter1 != iter2

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

Traversing a VectorTraversing a VectorConsider:

for (int i = 0; i != v.size(); ++i) cout << v[i] << endl;

8

for (vector<int>::iterator itr = v.begin();itr != v.end(); ++i)

cout << *itr << endl;

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

Traversing a VectorTraversing a Vector

9

vector<int>::iterator itr = v.begin();while(itr != v.end())

cout << *itr++ << endl;

for (vector<int>::iterator itr = v.begin();itr != v.end(); ++i)

cout << *itr << endl;

But why bother?

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

Operations Requiring IteratorsOperations Requiring Iterators iterator insert(iterator pos, const Object & x)

iterator erase(iterator pos)

iterator erase(iterator start, iterator end)

10

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

11

List InsertList Insert

fro nt

Lis t o b je c t (a fte r)

fro nt re a r

Lis t o b je c t (b e fo re )

ne w E ltre a r

ite r

2 55937 2 9374

ite r4

list <int> li; // then populatelist <int>::iterator newElt = li.insert (iter, 4);

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

12

List EraseList Erase

fro nt

Lis t o b je c t (a fte r)

fro nt re a r

Lis t o b je c t (b e fo re )

re a rite r

2 5937 2 593

ite r? ?

list <int> li; // then populatelist <int>::iterator i = li.erase (iter);cout << *i;

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

13

Ordered listsOrdered lists8 27 46 0

fro nt re a r6 5

8 27 46 0fro ntre a r

5 0 8 27 46 5

fro nt re a rc u rr

6 06 5

5 0

B e fo re Ins e rt A fte r Ins e rt

// Walk list to find insertion point

list<int>::iterator curr;

for (curr = lst.begin (); curr != lst.end ()

&& item > *curr; ++cur) ;

lst.insert (curr, item);

What if inserting item = 83?

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

14

Node CompositionNode Compositionstruct Node { Node (const T& v = T (), Node* n = NULL,

Node* p = NULL) : data (v), next (n), prev (p) { } T data; Node* next; Node* prev;};

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

15

List ClassList Classclass List { // Insert Node struct def. Node* d; // ptr to dummy header size_t sz; // sizepublic: 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};

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

16

Pushing a ValuePushing a Value

p r e v n e x t

head er

B efo re In s ert : E m p t y lis t

prev next

header

newNode

After insert : List with one element

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

17

Pushing ValuesPushing Valuesvoid 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;}

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

18

Inserting a NodeInserting a Node

ne xt pre v

pre vN ode = c urr -> pre vp r ev item n ex t

143 2 c ur r

n ew N o d e

newNode = new Node (item, curr, prevNode); // 2 & 1prevNode->next = newNode; // 3curr->prev = newNode; // 4++sz;

Lobj.insert (curr, item);

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

19

Erasing a NodeErasing a Node

pre v ne xt

s uc c N o de = c ur r->ne xt

1

2

//////

//

pre vN o de = c ur r->pre v c ur r

curr->prev->next = curr->next; // 1curr->next->prev = curr->prev; // 2delete curr;--sz;

lObj.erase (curr);