View
212
Download
0
Embed Size (px)
Citation preview
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
DCO20105 Data structures and algorithms
Lecture 4: C++ and list
Usage of Vector and List C++ reference STL’s list and its usage
-- By Rossella Lau
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
<list> vs <vector> (or array)
Vector (or array) List
Adding/removing elements
fast when appending while there is still room or deleting the last element
fast when adding/deleting an element in any position if the position is set
Accessing can be sorted and accessed by efficient method such as binary search
no random access, should be linear search
Memory allocation/de-allocation
Actions are only required when the array needs to be re-sized
Actions are required for each add/delete operation on a list
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
Notes on memory allocation
In theory, list is efficient for applications in which storage is variant and insert/remove operations are required
However, memory allocation or de-allocation is a very expensive operation
It involves lots of steps to look for storage, computing positions of occupied or free memory
In the following discussion, we assume that we have an efficient memory allocation method
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
Application considerations
In bookShop Sales: only append, no update, no search List Catalog: one load, no update, search frequently
Sorted vector with binary search Order: volume is variant, search is frequently
• List: append easily, but can only apply linear search• Vector: ordered or random? may apply binary search
but needs shift operations for “insert”
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
C++ pointer considerations on list
Indirect identification of a part
Each part of a node must be identified through a node, and usually a pointer: e.g.,
ptritem ptrnext
itemnext
ptr
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
ptrnext
Consider it in an assignment operation When it is on the left hand side, it is going to be assigned a
value to point to another node• E.g., ptr->next = node2;
changes ptr->next pointing to node2
When it is on the right hand side, it represents the next nodes on the linked list; e.g., ptr=ptrnext changes ptr pointing to node1
itemnext
ptr
itemnext
node2itemnext
node1
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
C++ reference
Using C++ reference can make an alias for ptr next
It allows for a simpler coding for insert_before() and insert_after() with find()
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
Reference re-visitTo simplify pointer operation in C, C++ supports
one more form to represent an object: reference
To define a reference; e.g., int & refA = a; or Product & refP = p; where a and p are ordinary variables in a program
A reference must reference to another object and can not reference to null value
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
Use of reference
A reference internally is an address, same as a pointer, but it can be used as the way of a real object/datum; i.e., it does not require dereferencing; e.g., refA = b; // assign b to a refP.getCode();
It can also give a “name” to a dynamic area: e.g., Node & refNode = * (new Node);
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
Example of reference
What is the output? int a, b; int & refA = a; Node& aNode = * (new Node); Cin >> b; //input 15 a = 25; refA = b; aNode.item = refA; cout << aNode.item;
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
Reference of ptr->next
Node * & target = ptrnext;
target = node2;
ptr = target
itemnext
ptr
itemnext
node2itemnext
node1target
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
Interesting reference
Since a function can return a reference, a function call can be on the left hand side of an assignment:
int & min(int &lhs, &rhs) { return a < b? a : b; } …… int a, b; cin >> a >> b; min(a, b) += 1;
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
The declaration of Node<T> * & is a reference of a pointer which can name a part of a node; e.g., Node<T> *& target = nodePtr->next;
Since the part can be directly referred, find(), in List.h, allows for the found part to have an alias name
The name itself can be assigned a value, i.e., pointing to another node; i.e., link to another node or linked list
The name itself also represents a value which points to the next node
With find(), insert() can be as easy as:target = new Node<T>( item, target);
Node<T> *& find()itemnext next
targetV
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
C++ notes on destructor
When designing destructor for friend classes, it should be careful to determine which destructor is responsible for memory de-allocation.
If the de-allocation places the destructor in Node<T>, chain effects may accidentally happen
Because de-allocating a node would cause the destructor of another node (next) to be executed!
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
STL’s list
It is a doubly linked list
Ford’s slide 6:3
Operations on a doubly linked list: Ford’s slide: 9:12-16
fro nt b ac k
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
Methods of STL’s list
Method names of different containers in the STL are identical
• push_front(), push_back()• front(), back()• pop_front(), pop_front()• list<T>::iterator is provided (forward/backward only)• list<T>::const_iterator is for traversal on constant objects• size()• Syntax and other methods: Ford: 6: 6-13• Operator overload operations: 6:14
Header file <list> is needed
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
Sample usage of STL’s listFord’s slide: 6:15 insert()
Ford’s slide 6:16 erase()
Ford’s review exercises: 1,2,5,6,7
fro nt
Lis t o b je c t (a fte r)
fro nt re a r
Lis t o b je c t (b e fo re )
ne w E ltre a r
ite r
2 55937 2 9374
ite r4
front
List object (after)
front rear
List object (before)
reariter
2 5937 2 593
iter??
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
Ordered list
Ford: 6:17
The list has an order
Each insert operation should maintain the order
Linear search can be faster to stop for items not found on the list
Sample programs: d_listl.h and prg6_3.cpp
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
Application considerations re-visit
For the container “order” Will it use an ordered list better?
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6
Summary
Efficiency of operations on linked list and array depends
Reference is a more convenient way to use address than pointer
STL’s list is a doubly linked list and the operations names (method) are the same as what they are in the vector