CS301-Lec04 handout - CS301 Data Structures Lecture No. 04...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 04 ___________________________________________________________________ Data Structures Lecture No. 04 Reading Material Data Structures and algorithm analysis in C++ Chapter. 3 3.2.3, 3.2.4, 3.2.5 Summary Methods of Linked List Example of list usage Analysis of Link List Doubly-linked List Circularly-linked lists Josephus Problem Methods of Linked List In the previous lecture, we discussed the methods of linked list. These methods form the interface of the link list. For further elucidation of these techniques, we will talk about the start method that has the following code. // position currentNode and lastCurrentNode at first element void start() { lastCurrentNode = headNode; currentNode = headNode; }; There are two statements in this method. We assign the value of headNode to both lastCurrentNode and currentNode . These two pointers point at different nodes of the list. Here we have pointed both of these pointers at the start of the list. On calling some other method like next, these pointers will move forward. As we can move in the singly-linked list in one direction, these pointers cannot go behind headNode . We will now see how a node can be removed from the link list. We use the method remove for this purpose. void remove() { (step 1) lastCurrentNode->setNext(currentNode->getNext()); (step 2) delete currentNode; Page 1 of 14
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS301 – Data Structures Lecture No. 04 ___________________________________________________________________ (step 3) currentNode = lastCurrentNode->getNext(); (step 4) size--; } }; Suppose that the currentNode is pointing at the location that contains the value 6. A request for the removal of the node is made. Resultantly, the node pointed by currentNode should be removed. For this purpose, at first, the next pointer of the node with value 2 (the node pointed by the lastCurrentNode pointer), that is before the node with value 6, bypasses the node with value 6. It is, now pointing to the node with value 8. The code of the first step is as: lastCurrentNode->setNext(currentNode->getNext()); What does the statement currentNode->getNext() do? The currentNode is pointing to the node with value 6 while the next of this node is pointing to the node with value 8. That is the next pointer of node with value 6 contains the address of the node with value 8. The statement lastCurrentNode->setNext(currentNode->getNext()) will set the next pointer of the node pointed by the lastCurrentNode to the node with value 8. So the next pointer of the node with value 2 is pointing to the node with value 8. You see that the next pointer of the node having data element 2 contains the address of the node having data element 8. The node with value 6 has been disconnected from the chain while the node with value 2 is connected to the node with the value 8. The code of the next step is:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

CS301-Lec04 handout - CS301 Data Structures Lecture No. 04...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online