Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists Useful when the number of elements is not known in advance or varies widely during execution Allows efficient insertion and removal, sequential.

Similar presentations


Presentation on theme: "Linked Lists Useful when the number of elements is not known in advance or varies widely during execution Allows efficient insertion and removal, sequential."— Presentation transcript:

1 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 HeadABC

2 Useful Linked List Functions Constructors and assignment: –list L; // default constructor –list 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

3 Useful Linked List Functions (cont) Iterators: –list ::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

4 Linked List Functions - Examples Constructors: –list L1;// create a new (empty) list // of integers –list L2;// create a new (empty) list // of pointers to widgets –list L3;// create a new (empty) list // of widgets –list L4 (L1);// copy constructor

5 Linked List Functions – Examples (cont) Assignment: –list L3;// create a new (empty) list // of widgets –list L5; // create a new (empty) list // of widgets –…// add some items to L3 –L5 = L3;// assignment

6 Linked List Functions – Examples (cont) Adding elements: –L.push_front(7); L123L7132

7 Linked List Functions – Examples (cont) Adding elements: –L.push_back(7); L123L1273

8 Linked List Functions – Examples (cont) Adding elements: –L.insert(L.end(),8); L123L1283

9 Linked List Functions – Examples (cont) Adding elements: –list ::iterator loc = find(L.begin(),L.end(),2); –loc = L.insert(loc,8); L123L1832

10 Linked List Functions – Examples (cont) Deleting elements: –L.pop_front(); L123L32

11 Linked List Functions – Examples (cont) Deleting elements: –L.pop_back(); L123L21

12 Linked List Functions – Examples (cont) Deleting elements: –L.remove(17); L83L1783

13 Linked List Functions – Examples (cont) Deleting elements: –list ::iterator i = find(L.begin(),L.end(),3); –L.erase(i); L124L1243

14 Linked List Functions – Examples (cont) Deleting elements: –list ::iterator start = find(L.begin(),L.end(),2); –list ::iterator stop = find(L.begin(),L.end(),5); –L.erase(start,stop); L12534L125

15 Linked List Functions – Examples (cont) Number of elements: –cout << There are << L.size() << elements in the list 5 L12534

16 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 L112534 L2

17 Linked List Functions – Examples (cont) Number of elements: –int num=0; –count(L1.begin(),L.end(),7,num); –cout << L1 contains << num << 7s L1 contains 2 7s L1371117

18 Linked List Functions – Examples (cont) Miscellaneous: –L1.sort(); L142315 12534

19 Linked List Functions – Examples (cont) Miscellaneous: –L1.reverse(); L112534 54132

20 Insert Iterators Assignment to an iterator is normally an overwriting operation (replaces the contents of the target): copy(L2.begin(),L2.end(),L1.begin()); L183L2125L1125

21 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)); L183L2125L183512

22 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

23 Example Program – Inventory System A business, World Wide Widget Works, manufactures widgets class 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 };


Download ppt "Linked Lists Useful when the number of elements is not known in advance or varies widely during execution Allows efficient insertion and removal, sequential."

Similar presentations


Ads by Google