Download presentation
Presentation is loading. Please wait.
2
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms Lecture 3: Basics of Linked List C++ pointer revision Basics of a linked list -- By Rossella Lau
3
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 A reason for using a Linked List Array It allows storing of multiple occurrences of items consecutively The number of items should be specified ahead Linked list As an array may occupy too many spaces or may be too small to store all items, a linked list allows storing of the exact number of items Storage for an item is allocated dynamically on demand Example Program List.h
4
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 C++ pointer Revision: slide 6-21, Lecture 8 in DCO10105 Another point of view: Ford’s Slides: 5: 2-14
5
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 An example of pointer and array Ford’s revision exercise: 5.2: What are the final values in the array arr? int arr[] = {45, 24, 16, 3, 2, 8}; int *parr = arr; *parr = 3; parr +=4; *parr = 5; parr = parr - 3; *parr = 15; parr[4] *= 2; Final values in the array arr = { ____, ____, ____, ____, ____, ____}
6
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 Dynamic storage requests for items When an item needs to be stored, new is used, and a pointer is needed to point to the new item When a second item is required to be stored, new is used again. An additional pointer is needed. It is unwise to use an array to store such pointers – as we cannot determine the size of the array ahead item1 ptr item2next Therefore, the pointer which points to the second element is better stored with the first element.
7
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 Nodes of a Linked list When more items are requested, one can see elements are linked as a list. To formalize all items in the linked list, a null pointer (value 0) is added to the last item. 0 nextitem1 ptr item2next itemn … itemnext Now, each node (item + next) has the same format
8
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 Typical structures of a Linked List template class Node { T item; Node *next; friend class List ; }; template class List { Node *head, *tail; }; head tail nextitem10itemN ……
9
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 C++ Notes – friends Friend classes can access the private members of the class which is declaring Node and List are hand by hand classes but should be implemented as two independent classes In order to allow for class List to direct use the private members in Node, List is declared as a friend class inside class Node Similarly, a class may declare its friend function(s) However, friends should be avoided as much as possible because they are contradicted with encapsulation!
10
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 C++ notes on header and implementation source Traditional C uses a header file (.h) to define data members and function prototypes and implementation codes (.cpp) will be written in another file The “.h” should be included if a program uses the class, and lines in “.h” file should be compiled again and again The “.cpp” is only required to be compiled once and allow for “link” up during link time after the code compilation. It saves time to compile for the “.cpp” file C++ uses only one header file (.h) to define a template classe since a template’s type is not determined until the compilation time. It cannot save time by separating the header and implementation It sacrifices compilation time for flexibility
11
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 Typical operations of a linked list node Creation of a node: nodePtr = new Node(item); Member identification of a node: nodePtr item Care should be taken to avoid identification from a null pointer or a pointer without properly being assigned a value Nodes “linked” up: node1Ptr next = node2Ptr; List pointer assignment: head = firstNodePtr;
12
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 Typical operations of a linked list push_back() -- append an item at the end push_front() -- insert an item at the front find() -- a linear search insert() -- insert an item before or after a node and usually performed after a find() remove() -- remove an item
13
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 Examples of push_back() and push_front() push_back(10), next10 headtail next20 Initial : headtail (null) Initial : headtail (null) push_front(10), next10 tailhead next20 push_back(20) push_front(20),
14
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 An example of insert_before() next10 headtail next20 Initial : Insert_before(20, 15) next10 head tail next20 next15 prev curr
15
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 Examples of remove() next 7 9 head next 4 6 delete 9: head next 7 4 6 delete 6: next 7 head next 4 6 prevcurr next 9 curr next 6
16
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 Pointer traversal on a linked list A typical traversal of an array: for (size_t i =0; i != SIZE; i++) {...... array[i]......} As head points to the first element, all the items can be traversed through the link, i.e., Node *next. Treating head similar to a[0], iteration through next similar to i++, null pointer similar to the end of an array, a typical traversal on linked list: for (Node * it=head; it; it=it->next) {...... it->item......} Example in List.h: search()
17
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 Pointer “look ahead” traversal The above loop is simple but would end up with the iterating pointer pointing to null We may also use a look ahead traversal method to examine contents of a node from its previous node on the linked list for (Node * it=head; it->next; it=it->next) {...... it->next->item...... } It is used more often and examples are List.h: print(), find()
18
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 Summary A pointer stores an address and can apply de- referencing and object member identification Pointer is a powerful but also a dangerous feature Linked list is a typical dynamic memory structure in which it uses pointers to link up its nodes Nodes on a linked list are created on demand and are more space efficient than an array in which unwanted space is usually presented
19
Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 Reference Ford: 5.1-5.3 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 -- END --
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.