Lists
-
Upload
ora-copeland -
Category
Documents
-
view
19 -
download
1
description
Transcript of Lists
![Page 1: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/1.jpg)
Lists
Chapter 8
![Page 2: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/2.jpg)
2
Linked Lists
• As an ADT, a list is– finite sequence (possibly empty) of elements
Operations commonly include:• Construction Allocate & initialize object• Empty Check if it is empty• Traverse Go through list, process elements in
order stored• Insert Add an item to list at any point• Delete Remove an item from the list at any
point
![Page 3: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/3.jpg)
3
Array/Vector Implementation of List
• Data Members: – Store the list items in consecutive array or
vector locations
a1, a2, a3 , . . . an
a[0] a[1] a[2] ... a[n-1], a[n] ... a[CAPACITY-1]
For an array, add a mySize member to store the length (n) of the list.
![Page 4: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/4.jpg)
4
Array/Vector Implementation of List
Operations
Construction: Set mySize to 0; if run-time array, allocate memory for it
For vector: let its constructor do the work
Empty: mySize == 0For vector: Use its empty() operation
![Page 5: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/5.jpg)
5
Array/Vector Implementation of List
Traverse:
or
for (int i = 0; i < size; i++)
{ Process(a[i]); }
i = 0;while (i < size){ Process(a[i]); i++; }
![Page 6: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/6.jpg)
6
Array/Vector Implementation of List
• Insert:
• Delete
Insert 6 after 5 in 3, 5, 8, 9, 10, 12, 13, 15
3, 5, 6, 8, 9, 10, 12, 13, 15
Have to shift array elements to make room.
Delete 5 from 3, 5, 6. 8, 9, 10, 12, 13, 15
3, 6, 8, 9, 10, 12, 13, 15
Have to shift array elements to close the gap.
![Page 7: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/7.jpg)
7
Array/Vector Implementation of List
• This implementation of lists is inefficient for dynamic lists – Those that change frequently – Those with many insertions and deletions
So …
We look for an alternative implementation.
![Page 8: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/8.jpg)
8
Linked List
For the array/vector-based implementation:1. First element is at location 02. Successor of item at location i is at location
i + 13. End is at location size – 1
Fix:1. Remove requirement that list elements be
stored in consecutive location.2. But then need a "link" that connects each
element to its successorLinked Lists !!
![Page 9: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/9.jpg)
9
Linked Lists
• Definition <=> A linked list of self-referential class objects– called nodes– connected by pointer links (thus, a "linked" list)
• Subsequent nodes accessed via link-pointer member stored in each member
• Link pointer in last node set to null (zero)– marks the end of the list
• Nodes created dynamically, as needed
![Page 10: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/10.jpg)
10
Self-Referential Classes
• A self-referential class contains a pointer member that points to a class object of the same class type
class Part_node { public: Part_node ( ); … private: char part_num[8], descrip[20]; int qty; float price; Part_node *next_part; } ;
class Part_node { public: Part_node ( ); … private: char part_num[8], descrip[20]; int qty; float price; Part_node *next_part; } ;
![Page 11: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/11.jpg)
11
Self-Referential Classes
• This pointer to an object of the type being declared enables class objects to be linked together in various ways– This is how we get linked lists
0
Pointervariable
LinkPointerMember
NullPointer
![Page 12: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/12.jpg)
12
Dynamic Memory Allocation
• If the data structures are to be dynamic, then dynamic memory allocation is required– operators new and delete are essential
part_node *newPtr = new part_node; part_node *newPtr = new part_node;
Creates thenew pointer Allocates the space for
a new part node
0
![Page 13: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/13.jpg)
13
Dynamic Memory Allocation
• The delete operator deallocates memory allocated with the new
• Note: newPtr is not itself deleted -- rather the space newPtr points to
0
delete newPtr; delete newPtr;
![Page 14: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/14.jpg)
14
Linked Lists Operations
• Construction: first = null_value;
• Empty: first == null_value?
• Traverse
ptr = first; while (ptr != null_value) { Process data part of node pointed to by ptr ptr = next part of node pointed to by ptr; }
![Page 15: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/15.jpg)
15
9 17 22 26 34first
ptr
9 17 22 26 34first
ptr
...9 17 22 26 34first
ptr
9 17 22 26 34first
ptr
ptr = first;while (ptr != null_value){ Process data part of node pointed to by ptr;
ptr = next part of node pointed to by ptr;
}
ptr = first;while (ptr != null_value){ Process data part of node pointed to by ptr;
ptr = next part of node pointed to by ptr;
}
Traverse
![Page 16: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/16.jpg)
16
Operations: Insertion
• Insertion – To insert 20 after 17– Need address of item before point of insertion– predptr points to the node containing 17– Get a new node pointed to by newptr and store 20 in it– Set the next pointer of this new node equal to the next
pointer in its predecessor, thus making it point to its successor.
– Reset the next pointer of its predecessor to point to this new node
9 17 22 26 34first
20newptr
predptr
And voila! The node is inserted (linked) into the list
And voila! The node is inserted (linked) into the list
![Page 17: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/17.jpg)
17
Operations: Insertion
• Note: insertion also works at end of list– pointer member of new node set to null
• Insertion at the beginning of the list– predptr must be set to first– pointer member of newptr set to that value– first set to value of newptr
Note: In all cases, no shifting of list elements is required !
![Page 18: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/18.jpg)
18
Operations: Deletion
• Delete node containing 22 from list.– Suppose ptr points to the node to be deleted– predptr points to its predecessor (the 20)
• Do a bypass operation: – Set the next pointer in the predecessor to
point to the successor of the node to be deleted– Deallocate the node being deleted.
5 17 22 29 34first 209
predptr ptr
To free space
![Page 19: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/19.jpg)
19
Linked Lists - Advantages
• Access any item as long as external link to first item maintained
• Insert new item without shifting
• Delete existing item without shifting
• Can expand/contract as necessary
![Page 20: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/20.jpg)
20
Linked Lists - Disadvantages
• Overhead of links: – used only internally, pure overhead
• If dynamic, must provide – destructor– copy constructor
• No longer have direct access to each element of the list– Many sorting algorithms need direct access– Binary search needs direct access
• O(1) access becomes O(n) access – must go through first element, and then second, and
then third, etc.
![Page 21: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/21.jpg)
21
Linked Lists - Disadvantages• List-processing algorithms that require fast access to each
element cannot be done as efficiently with linked lists.• Consider adding an element at the end of the list
Array Linked Lista[size++] = value;
or for a vector:
v.push_back(value);
Get a new node;
set data part = value
next part = null_value
If list is empty
Set first to point to new node.
Else
Traverse list to find last node
Set next part of last node to point to new node.
This is the inefficient part
![Page 22: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/22.jpg)
22
Using C++ Pointers and Classes
• To Implement Nodesclass Node{ public:
DataType data; Node * next;};
• Note: The definition of a Node is recursive – (or self-referential)
• It uses the name Node in its definition• The next member is defined as a pointer to a
Node
![Page 23: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/23.jpg)
23
Working with Nodes
• Declaring pointers Node * ptr; ortypedef Node * NodePointer;
NodePointer ptr;
• Allocate and deallocate ptr = new Node; delete ptr;
• Access the data and next part of node(*ptr).data and (*ptr).nextor ptr->data and ptr->next
![Page 24: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/24.jpg)
24
Working with Nodes
• Note data members are public
• This class declaration will be placed inside another class declaration for LinkedList
• The data members data and next of struct Node will be public inside the class– will accessible to the member and friend
functions– will be private outside the class
class Node{ public: DataType data; Node * next; };
class Node{ public: DataType data; Node * next; };
![Page 25: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/25.jpg)
25
Class Template LinkedList
template <typename DataType>;class LinkedList{ private: class Node { public: DataType data; Node * next; }; typedef Node * NodePointer; . . .};
• data is public inside class Node
• class Node is private inside LinkedList
![Page 26: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/26.jpg)
26
Data Members for LinkedList
• A linked list will be characterized by:– A pointer to the first node in the list.– Each node contains a pointer to the next node in
the list– The last node contains a null pointer
• As a variation first may – be a structure– also contain a count of the elements in the list
9 17 22 26 34first
![Page 27: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/27.jpg)
27
Function Members for LinkedList
• Constructor– Make first a null pointer and – set mySize to 0
• Destructor– Nodes are dynamically allocated by new– Default destructor will not specify the delete– All the nodes from that point on would be
"marooned memory"– A destructor must be explicitly implemented to
do the delete
Lfirst
mySize 0
![Page 28: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/28.jpg)
28
Function Members for LinkedList
• Copy constructor– By default, when a copy is made of a
LinkedList object, it only gets the head pointer– Copy constructor will make a new linked list of
nodes to which copyOfL will point
Lfirst
mySize 5
9 17 22 26 34
copyOfLfirst
mySize 5
9 17 22 26 34
![Page 29: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/29.jpg)
29
Variations
• An empty head node– Every node has a predecessor– Does away with special case insertions
• An empty trailer node– Every node has a successor
• Doubly linked list
last
prev L
first
mySize 5
9 17 22 26 34
next
![Page 30: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/30.jpg)
30
The STL list<T> Class Template
• A sequential container – Optimized for insertion and erasure at
arbitrary points in the sequence.– Implemented as a circular doubly-linked list
with head node.
L
first
mySize 5
last 17 22 26 34 9
prev
next
data
![Page 31: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/31.jpg)
31
The STL list<T> Class Template
Node structure
struct list_node
{ pointer next, prev;T data; }
![Page 32: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/32.jpg)
32
The STL list<T> Class Template
• But it's allo/deallo-cation scheme is complex– Does not simply using new and delete
operations.
• Using the heap manager is inefficient for large numbers of allo/deallo-cations– Thus it does it's own memory management.
![Page 33: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/33.jpg)
33
The STL list<T> Memory Management
When a node is allocated
1. If there is a node on the free list, allocate it.• This is maintained as a linked stack
2. If the free list is empty:a) Call the heap manager to allocate a block of
memory (a "buffer", typically 4K)
b) Carve it up into pieces of size required for a node of a list<T>.
![Page 34: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/34.jpg)
34
The STL list<T> Memory Management
• When a node is deallocated– Push it onto the free list.
• When all lists of this type T have been destroyed– Return it to the heap
![Page 35: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/35.jpg)
35
Comparing List<t> With Other Containers
• Note : list<T> does not support direct access – does not have the subscript operator [ ].
Property Array vector<T> deque<T> list<T>
Direct/random access ([]) (exclnt)(good)X
Sequential access
Insert/delete at front (poor)
Insert/delete in middle
Insert/delete at end
Overhead lowest low low/mediumhigh
![Page 36: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/36.jpg)
36
list<t> Iterators
• list<T>'s iterator is "weaker" than that for vector<T>. vector<T>: random access iterators list<T>: bidirectional iterators
• Operations in common ++ Move iterator to next element
(like ptr = ptr-> next) -- Move iterator to preceding element
(like ptr = ptr-> prev) * dereferencing operator
(like ptr-> data)
![Page 37: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/37.jpg)
37
list<t> Iterators
• Operators in common = assignment
(for same type iterators) it1 = it2 makes it1 positioned at
same element as it2 == and !=
(for same type iterators) checks whether iterators are positioned
at the same element
![Page 38: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/38.jpg)
38
Using list<t> Iterators
Example: Construct a list containing first 4 even integers; then output the list.
list<int> l;
for (int i = 1; i <= 4; i++) l.push_back(2*i);
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
cout << *it << " ";
cout << endl;
![Page 39: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/39.jpg)
39
Limitations of list<t> Iterators
• Directional iterators don't have: +, -, +=, -=, []
• list<t> iterators cannot do "jumping"– No iterator ± n– No direct access
• Result, cannot implement some sort() algorithms– Solution: list<T> has it's own sort()
operation
![Page 40: Lists](https://reader031.fdocuments.us/reader031/viewer/2022032805/5681315c550346895d97d31d/html5/thumbnails/40.jpg)
40
Basic list<t> Operations
• See page 451,2– Constructors– Destructors– Empty, Size– Push, insert, pop, remove– Front, back– Iterator functions:
• begin, end,
– Sort– Merge, splice– Comparisons