1 CMPT 117 Linked Lists (singly linked)
2 Problems with arrays When an element is deleted from or inserted into an array, the rest of the array has to be adjusted If the number of elements is more than what has been allocated, a new array has to be used
3 Linked list Advantages Easy to insert and delete data Easy to insert and delete data Dynamically allocate data Dynamically allocate data Disadvantages Random access is slow Random access is slow Need some space overhead to store pointers Need some space overhead to store pointers
4 Pointer based linked list itemnext Can be a pointer to a class Pointer to the next node Each node has two parts:
5 Simple examples using linked list 10
6 Simple examples using linked list 10 30
7 Simple examples using linked list
8 Insertion
9 Deletion Delete this node
10 C++ definition struct Node { PStudent item; Node* next; }; typedef Node* PNode; struct Node { int item; Node* next; }; typedef Node* PNode; The definition is different for different types of items
11 Allocate space for a node PNode p; p = new Node; p
12 Basic operations To reference the member Item p->item p->item To access the next node (if present) p->next p->next To determine the end of list if (p->next == NULL) if (p->next == NULL) To access the head of list Needs to define a head pointer, i.e. PNode head Needs to define a head pointer, i.e. PNode head head
13 Class LinkedList A LinkedList class should be defined to encapsulate all operations of linked list class LinkedList { protected: protected: PNode head; PNode head; public: public: void InsertFirst(...); void InsertFirst(...); void DeleteFirst(...); void DeleteFirst(...); void Insert(...); void Insert(...);......}
14 To delete the first node Is this code correct? if (head!=NULL) head = head->next; This code works in Java, but not in C++, because Java has garbage collection Is this correct? if (head!=NULL) { delete head->item; delete head->item; delete head; delete head; head = head->next; } This code may work but is dangerous. How should we change it?
15 To insert as the first node A new node needs to be allocated The new node should point to the first node of the linked list head should point to the newly created node Please write the code for InsertFirst(PStudent pst)?
16 To display the contents of a linked list A high level Pseudocode: 1. Let current pointer point to the first node in the linked list. 2. While (current pointer is not NULL) { Display the data portion of the current node Set current pointer to the Next pointer of the current node }
17 Solution Use Cur to keep track of the current node and initialise it to Head To display the data cout item item <<endl; To advance to the next node Cur = Cur->next;
18 Solution (cont’d) The above can be combined: for (PNode Cur = head; Cur!= NULL; Cur=Cur->next) cout item<<endl;
19 Deleting a node (not the first one) head CurPre Return this to the system
20 Deleting a node (cont’d) Locate the node Adjust the next pointer of the previous node, to point to the next node pointed to by next of the current node Need to have a pointer to point to the previous node! Need to have a pointer to point to the previous node! Return the deleted node to the system delete or simply collect them all! delete or simply collect them all!
21 To locate a node while( Cur!=NULL && Cur->item!=value) { Pre = Cur; Cur= Cur->next; }
22 To adjust the pointer if (Cur!= NULL) { if (Pre == NULL) { head = Cur->next; //The first node is deleted } else { // not the first node Pre->next = Cur->next; } return Cur; }
23 Insert a node and keep the order of data ascending 2040 PreCur2040 PreCur30 NewPtr Locate the insertion location Adjust the pointers NewPtr->Next=Cur; Pre->Next=NewPtr;
24 Head PreCur NewPtr For the first node: NewPtr->Next = Cur; Head = NewPtr; Insert a node (cont’d)
25 To locate a node Pre = NULL; Cur = Head; while(NewValue>Cur->item) { Pre = Cur; Cur = Cur->next; } Will this work? What if NewValue>the value of the last element? Then Cur = Cur->next equals to NULL. So, Cur->item does not exist.
26 To locate the position (revised) Pre= NULL; Cur = Head; while(Cur!= NULL && NewValue>Cur->item) { Pre= Cur; Cur = Cur->next; }