210 likes | 359 Views
DCO 20105 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. <list> vs <vector> (or array). Notes on memory allocation.
E N D
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
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
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”
item next ptr 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
item next node2 node1 item item next next ptr 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
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()
Reference re-visit • To 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
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 arefP.getCode(); • It can also give a “name” to a dynamic area: e.g., Node & refNode = * (new Node);
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;
item next node2 node1 item item next next ptr Reference of ptr->next • Node * & target = ptrnext; • target = node2; • ptr = target target
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;
targetV item next next Node<T> *& find() • 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);
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!
STL’s list • It is a doubly linked listFord’s slide 6:3 • Operations on a doubly linked list: Ford’s slide: 9:12-16
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
List object (before) List object (after) 7 3 9 5 3 9 5 2 2 rear rear front front ?? iter iter Sample usage of STL’s list • Ford’s slide: 6:15 insert() • Ford’s slide 6:16 erase() • Ford’s review exercises: 1,2,5,6,7
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
Application considerations re-visit • For the container “order” • Will it use an ordered list better?
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
Reference • Ford: 6.2-3, 9.1-3,5 • STL online references • http://www.sgi.com/tech/stl • http://www.cppreference.com/ • Example programs: List.h, TestList.cpp, d_listl.h, and prg6_3.cpp -- END --