Linked Lists Useful when the number of elements is not known in advance or varies widely during...

Post on 30-Mar-2015

215 views 0 download

Tags:

Transcript of Linked Lists Useful when the number of elements is not known in advance or varies widely during...

Linked Lists

• Useful when the number of elements is not known in advance or varies widely during execution

• Allows efficient insertion and removal, sequential access

Head A B C

Useful Linked List Functions

• Constructors and assignment:– list <T> L; // default constructor– list <T> L (L2); // copy constructor– L = L2; // assignment operator

• Element access:– L.front(); // first item in the list– L.back(); // last item in the list

• Size:– L.empty(); // true if list is empty– L.size(); // returns the number of items

Useful Linked List Functions (cont)

• Iterators:– list <T>::iterator i; // declare a new iterator– L.begin(); // starting iterator– L.end(); // ending iterator

• Insertion and removal:– L.push_front(value); // add value to front of the list– L.push_back(value); // add value to end of the list– L.insert(iterator,value); // add value at specified location– L.pop_front(); // remove item from front of list– L.pop_back(); // remove item from end of list– L.erase(iterator); // remove referenced item– L.remove(value); // remove all occurrences of value

Linked List Functions - Examples

• Constructors:– list <int> L1; // create a new (empty)

list// of integers

– list <Widget *> L2;// create a new (empty) list// of pointers to widgets

– list <Widget> L3; // create a new (empty) list// of widgets

– list <int> L4 (L1); // copy constructor

Linked List Functions – Examples (cont)

• Assignment:– list <Widget> L3; // create a new (empty) list

// of widgets– list <Widget> L5; // create a new (empty) list

// of widgets– … // add some items to L3– L5 = L3; // assignment

Linked List Functions – Examples (cont)

• Adding elements:– L.push_front(7);

L 1 2 3

L 7 1 32

Linked List Functions – Examples (cont)

• Adding elements:– L.push_back(7);

L 1 2 3

L 1 2 73

Linked List Functions – Examples (cont)

• Adding elements:– L.insert(L.end(),8);

L 1 2 3

L 1 2 83

Linked List Functions – Examples (cont)

• Adding elements:– list <int>::iterator loc = find(L.begin(),L.end(),2);

– loc = L.insert(loc,8);

L 1 2 3

L 1 8 32

Linked List Functions – Examples (cont)

• Deleting elements:– L.pop_front();

L 1 2 3

L 32

Linked List Functions – Examples (cont)

• Deleting elements:– L.pop_back();

L 1 2 3

L 21

Linked List Functions – Examples (cont)

• Deleting elements:– L.remove(17);

L 8 3

L 17 8 317

Linked List Functions – Examples (cont)

• Deleting elements:– list <int>::iterator i = find(L.begin(),L.end(),3);

– L.erase(i);

L 1 2 4

L 1 2 43

Linked List Functions – Examples (cont)

• Deleting elements:– list <int>::iterator start = find(L.begin(),L.end(),2);

– list <int>::iterator stop = find(L.begin(),L.end(),5);

– L.erase(start,stop);

L 1 2 53 4

L 1 2 5

Linked List Functions – Examples (cont)

• Number of elements:– cout << “There are” << L.size() << “elements in the list”

5

L 1 2 53 4

Linked List Functions – Examples (cont)

• Number of elements:– if (L1.empty()) cout << “L1 is empty”

– if (!L2.empty()) cout << “L2 is not empty”

L1 is empty

L2 is not empty

L1 1 2 53 4

L2

Linked List Functions – Examples (cont)

• Number of elements:– int num=0;

– count(L1.begin(),L.end(),7,num);

– cout << “L1 contains” << num << “7’s”

L1 contains 2 7’s

L1 3 7 111 7

Linked List Functions – Examples (cont)

• Miscellaneous:– L1.sort();

L1 4 2 31 5

L1 1 2 53 4

Linked List Functions – Examples (cont)

• Miscellaneous:– L1.reverse();

L1 1 2 53 4

L1 5 4 13 2

Insert Iterators

• Assignment to an iterator is normally an overwriting operation (replaces the contents of the target):

• copy(L2.begin(),L2.end(),L1.begin());

L1 8 3

L2 1 2 5

L1 1 2 5

Insert Iterators (cont)

• For lists (and sets) often instead want to perform insertion. Can use a list insertion iterator:

• copy(L2.begin(),L2.end(),back_inserter(L1));

L1 8 3

L2 1 2 5

L1 8 3 51 2

Insert Iterators (cont)

• In addition to back_inserter (which adds one list to the end of another) there is also:– front_inserter – adds one list to the front of another– Inserter – inserts one list in another at the position

pointed to by an iterator

• An insert iterator is a form of adaptor– An adaptor changes the interface of an object but does

little or no work itself– The insert iterator changes the list insert interface into

the iterator interface

Example Program – Inventory System

• A business, World Wide Widget Works, manufactures widgetsclass Widget {

public:

Widget():id_number(0){} // constructor

Widget(int a):id(a) {} // constructor

// operations

int id() {return id_number};

void operator = (Widget & rhs) {id.number = rhs.id_number;}

bool operator == (Widget & rhs) {id.number == rhs.id_number;}

bool operator < (Widget & rhs) {id.number < rhs.id_number;}

protected:

int id_number; // widget identification number

};