Download presentation
Presentation is loading. Please wait.
Published byLindsey Richards Modified over 8 years ago
2
List Structures
3
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
4
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?
5
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
6
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?
7
Array-Based List item 1item 2item 3item 4item 5
8
Singly Linked List item 1item 2item 3item 4item 5 null
9
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
10
Head and Tail Pointers item 1item 2item 3item 4item 5 null
11
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
12
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
13
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
14
9 17 22 26 34 Top 9 17 22 26 34 First Last
15
9 17 22 26 34 Top 9 17 22 26 34 First Last
16
9 17 22 26 34 Top 9 17 22 26 34 First Last
17
..\ds\stl\stl docs\list.html
18
Lists and Iterators Each container class is equipped with an associated iterator class. Iterators maintain pointer to “current” element
19
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
20
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.)
21
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;
22
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);
23
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);
24
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”;
25
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
26
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
27
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
28
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
30
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 ==, <
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.