List Structures
What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element has a unique predecessor (except first) and a unique successor (except last) Examples: grocery, to-do, address
Lists Lists may be keys required to be unique, other lists may not. Note: Stacks and Queues are lists How would you implement a list?
Define key. A field in a record whose value is used to determine the logical (or physical) order of the records in a list –Examples: social security number, name, zip code
Design Alternatives Consider alternative designs for the actual implementation of the list For example, –array –singly linked list –doubly linked list –circular linked list How would the choice be made? Might it change in a later version?
Array-Based List item 1item 2item 3item 4item 5
Singly Linked List item 1item 2item 3item 4item 5 null
Linked Stacks A simple linked list easily implements a stack. Only one end of the stack is accessed (Top) Only one pointer is required Push / Pop are performed at Top 9 17 22 26 34 Top
Head and Tail Pointers item 1item 2item 3item 4item 5 null
Linked Queues A linked can implement a queue Both ends of the stack must be accessed (Front/Back) Enqueued elements are added at the Back Dequeued elements are removed from the Front 9 17 22 26 34 Front Back
Circular Linked Lists –Last node contains a pointer to the first node in the list –A single pointer has access to both ends of the list 9 17 22 26 34 Last
Doubly-Linked Lists –Each node has two pointers one to its successor one to its predecessor 9 17 22 26 34 First Last Prev Data Next
9 17 22 26 34 Top 9 17 22 26 34 First Last
9 17 22 26 34 Top 9 17 22 26 34 First Last
9 17 22 26 34 Top 9 17 22 26 34 First Last
..\ds\stl\stl docs\list.html
Lists and Iterators Each container class is equipped with an associated iterator class. Iterators maintain pointer to “current” element
Lists Datatype Interface should allow –creation of empty list –tests for empty –re-initialization to empty –access to front and back –insertion and removal –operators for moving iterator to list beginning and end
List Iterator Datatype Interface provides –Operators for moving iterator to beginning of list begin( ) end( ) –Operators for moving through the list –Operator for returning the current item on the list (*) –Generic algorithms (Find, Remove, etc.)
Sample List Declarations list list_one; list list_three; // list of Widgets list list; //list of pointers to // Widgets list list_four (list_one); list list_five; list_five = list_three;
Adding Elements to a List list_one.push_front(12); list_three.push_back(Widget(6)); // insert widget 8 at end of list list_three.insert(list_three.end(), Widget(8)); // find location of first 5 in list list ::iterator location = find (list_one.begin(), list_one.end(), 5); // and insert an 11 immediately before it location = list_one.insert(location,11);
Erasing Elements from a List list_nine.erase(location); //erase all values between the first 5 and next 7 list ::iterator start = find(list_nine.begin(), list_nine.end(), 5); list ::iterator stop = find(location, list_nine.end(), 7); list_nine.erase(start,stop);
Number of Elements cout << “Number of elements:” << list_nine.size(); if (list_nine.empty() ) cout << “list is empty”; int num = 0; count (list_five.begin(),list_five.end(), 17, num); if (num > 0) cout << “contains a 17”;
Generic Algorithms // place elements into sequence list_ten.sort(); // sorting using the widget compare function list_twelve.sort(widgetCompare); // elements are now reversed list_ten.reverse(); list_one.swap(list_four); // generic algorithm
Insert Iterators Assignment to an iterator is normally an overwriting operation, replacing the contents of the target location. (list_one holds 1, 2, 3 and list_two holds 7, 8, 9, 10) copy (list_one.begin(), list_one.end(), list_two.begin()); will erase first 3 values of list_two so it now holds 1, 2, 3, 10
Insert Iterators (continued) For lists (and sets) often instead want to perform insertion. Can be done by creating a list insert iterator. copy (list_one.begin(), list_one.end(), back_inserter(list_two) ); list_one now holds 7,8, 9, 10, 1, 2, 3
Forms of Insert Iterators back_inserter(list) –inserts at back of list (uses push_back to add values) front_inserter ( list ) –inserts at front of list (uses push_front to add values inserter (container, iterator) –copies elements into the location specified by the iterator
Program Assignment Write a program which generates a concordance for a text file ("r:\users\ds\story.txt") Read a word from the input file, if not in the list insert. Print the list in ascending sequence –Add count of times it appears –Paragraph numbers in which it appears –Create struct to hold data overload ==, <