Download presentation
Presentation is loading. Please wait.
1
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects
2
2 Overview Linked List Class Definition Add Node Delete Node Destructor Namespace
3
3 Linked List A linked list is a collection of data. Each element of the linked list has Some data A link to the next element The link is used to chain the data, usually they are pointers. Example: A linked list of integers: 20457585 Data Link
4
4 Linked List A linked-list is a useful structure to hold a collection of data. A linked-list is dynamic. It can grow and shrink to any size. It can be implemented using dynamic class (dynamic objects). Dynamic objects are objects created and deleted by ‘ new ’ and ‘ delete ’. The main task of a linked list class is to maintain the chain (pointer) between the items it holds.
5
5 Linked List Original linked list of integers: Insertion, add(60): Deletion, delete(45): 20457585 20457585 60 Old pointer 20457585 Rearrange pointers
6
6 Linked List – class definition This is the definition of class ListNode (holding data). Pay attention to the techniques we have learned before. class ListNode { public: // no default constructor, user must call the // constructor explicitly ListNode(int i_data, ListNode* i_next=NULL): data(i_data), next(i_next) {} friend class List; // no destructor, let List class // handles memory deallocation (delete) private: int data; ListNode* next; };
7
7 Linked List – class definition This is the definition of class List (organizing data). class List { public: List() : head(NULL), tail(NULL), num_node(0) {} ~List(); void print(); bool addNode(int new_data); bool deleteNode(int del_data); int getSize() { return num_node; } private: ListNode* head; ListNode* tail; int num_node; };
8
8 Linked List – add node To insert a new node: Create a new node with ListNode* temp = new ListNode(new_data); Update the tail pointer, move its next pointer to the new node. Move the tail pointer to the new end. In our example, new nodes are added to the end of list. Full code are showed in main.cpp, linked.h and linked.cpp You can try to add nodes to the head or according to the number sequence. The procedure is similar.
9
9 Linked List – delete node To delete a node: Locate the node to be deleted temp points to the node. prev points to its predecessor Disconnect node from list using: prev->next = temp->next; Delete unnecessary node, return memory to system: delete temp; Be careful when deleting in data in special location, e.g. head. Depends on your implementation, sometime tail is another special case too.
10
10 Linked List - destructor ListNode class don’t have a destructor, because it doesn’t have any dynamic data member. But List class has dynamic members, therefore we have to write a destructor for it. List::~List() { ListNode* temp = head; while (head != NULL) { head = head->next; delete temp; temp = head; } Pay attention to the terminating condition when you use this kind of loops. They will easily get wrong.
11
11 Comparison between Array and Linked List Static Array: Size are fixed at compilation time. Elements can be random accessed. Binary search is efficient when elements can be random accessed. Elements are continuously stored. Dynamic Array: Size are not fixed at compilation time. You may resize the array, but copying is needed. A slow process. Elements can be random accessed. Delete an element also needs copying. Elements are continuously stored.
12
12 Comparison between Array and Linked List Linked List: Size are not fixed at compilation time. You may resize the linked list any time, no copying is needed. Elements can only be sequentially accessed. Slow to use binary search. Easy to delete an element. Elements are not continuously stored. Slow when caching is considered.
13
13 Namespace Suppose you want to use two libraries, but some names collide, what can you do? // file: classA.h// file: classB.h class Service {…}; class ClassB {…}; class ClassA {…}; class Service {…}; int del_all(); Even if you don't use “Service” and “del_all”, you run into trouble: the compiler will complain about multiple definitions of “Service” the linker may complain about multiple definitions of “del_all()”
14
14 Namespace Use namespace can solve multiple names problem. // file: classA.h namespace service_class_A { class Service {…}; class ClassA {…}; int del_all(); } // file: classB.h namespace service_class_B { class Service {…}; class ClassB {…}; int del_all(); }
15
15 Namespace You can refer to names in a namespace with ::, it is called scope resoultion operator. service_class_A::del_all(); service_class_B::Service serB; If the namespace is too long, you can define your own namespace alias, namespace cA = service_class_A; namespace cB = service_class_B; cA::del_all(); cB::Service serB;
16
16 Namespace You can also import the whole namespace by a “using” declaration. You can import a single name, using service_class_A::del_all; Everytime you use del_all(), it will refer to the del_all() of “classA.h”
17
17 Namespace Or you can import the whole namespace, using namespace service_class_B; Everytime you use del_all() or Service class, they will refer to the one in “classB.h”. One famous example is “ using namespace std; ” Of course, after you imported the whole namespace, you can specify the whole name if you do want to use other functions, e.g. using namespace service_class_B; service_class_A::del_all();
18
18 Namespace You can also bring all the names of a namespace into your program at once, but make sure it won't cause any ambiguities. namespace cA = service_class_A; namespace cB = service_class_B; using namespace cA; using namespace cB; Service ser_K;// error: ambiguous classB gun_dum;// ok, no problem. If the above ambiguity is removed, the program above can be compiled.
19
19 Namespace The following will cause error too, using service_class_A::Service; using service_class_B::Service; Do you know why?
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.