Download presentation
Presentation is loading. Please wait.
2
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6 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
3
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6 vs (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 Accessingcan 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
4
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
5
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”
6
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., ptr item ptr next itemnext ptr
7
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6 ptr next 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=ptr next changes ptr pointing to node1 itemnext ptr item next node2 item next node1
8
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()
9
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6 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
10
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);
11
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;
12
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6 Reference of ptr->next Node * & target = ptr next; target = node2; ptr = target itemnext ptr item next node2 item next node1 target
13
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;
14
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6 The declaration of Node * & is a reference of a pointer which can name a part of a node; e.g., Node *& 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 ( item, target); Node *& find() item next targetV
15
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, chain effects may accidentally happen Because de-allocating a node would cause the destructor of another node (next) to be executed!
16
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
17
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 ::iterator is provided (forward/backward only) list ::const_iterator is for traversal on constant objects size() Syntax and other methods: Ford: 6: 6-13 Operator overload operations: 6:14 Header file is needed
18
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6 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 front List object (after) front rear List object (before) rear iter 2 5937 2 593 ??
19
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
20
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6 Application considerations re-visit For the container “order” Will it use an ordered list better?
21
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
22
Rossella Lau Lecture 4, DCO20105, Semester A,2005-6 Reference Ford: 6.2-3, 9.1-3,5 STL online references http://www.sgi.com/tech/stl http://www.sgi.com/tech/stl http://www.cppreference.com/ http://www.cppreference.com/ Example programs: List.h, TestList.cpp, d_listl.h, and prg6_3.cpp -- END --
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.