Docstoc

Linked Lists

Document Sample
Linked Lists Powered By Docstoc
					              Linked Lists

      Head       A       B      C


• Useful when the number of elements is not
  known in advance or varies widely during
  execution
• Allows efficient insertion and removal,
  sequential access
   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)

        L          1       2       3


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


    L          7       1       2       3
Linked List Functions – Examples (cont)

        L         1       2       3


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


    L         1       2       3       7
Linked List Functions – Examples (cont)

        L           1          2       3


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


    L          1           2       3       8
Linked List Functions – Examples (cont)

          L            1           2         3


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

     L            1          8          2          3
Linked List Functions – Examples (cont)

          L               1       2       3


• Deleting elements:
   – L.pop_front();


                      L       2       3
Linked List Functions – Examples (cont)

          L              1       2       3


• Deleting elements:
   – L.pop_back();


                     L       1       2
Linked List Functions – Examples (cont)

      L          17    8   17   3


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


                 L     8    3
Linked List Functions – Examples (cont)

      L             1          2          3          4


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


           L             1          2          4
Linked List Functions – Examples (cont)

L              1         2          3         4          5


• 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          5
Linked List Functions – Examples (cont)

L             1          2          3          4          5


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



    5
Linked List Functions – Examples (cont)

L1              1           2          3            4   5

 L2

• 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
Linked List Functions – Examples (cont)

L1              3         7          1         7   11


• Number of elements:
     – int num=0;
     – count(L1.begin(),L.end(),7,num);
     – cout << “L1 contains” << num << “7’s”

     L1 contains 2 7’s
Linked List Functions – Examples (cont)

L1            4     2   1   5     3


• Miscellaneous:
     – L1.sort();

L1            1     2   3   4     5
Linked List Functions – Examples (cont)

L1         1        2   3   4     5


• Miscellaneous:
  – L1.reverse();

L1         5        4   3   2     1
               Insert Iterators
• Assignment to an iterator is normally an
  overwriting operation (replaces the contents of the
  target):
               L1         8        3


              L2          1        2        5

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

               L1          1        2       5
          Insert Iterators (cont)
• For lists (and sets) often instead want to perform
  insertion. Can use a list insertion iterator:
                  L1       8        3

               L2          1        2        5

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

  L1          8        3        1        2       5
           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 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
};

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:6
posted:12/11/2011
language:
pages:23