• Save
Upcoming SlideShare
×

Like this presentation? Why not share!

# 06 ds and algorithm session_08

## on Apr 24, 2012

• 784 views

### Views

Total Views
784
Views on SlideShare
723
Embed Views
61

Likes
2
0
0

### 2 Embeds61

 http://niitcourseslides.blogspot.in 58 http://niitcourseslides.blogspot.com 3

### Categories

Uploaded via as Microsoft PowerPoint

### Report content

• Comment goes here.
Are you sure you want to
Your message goes here
• In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
• Ask the students to write the algorithm first and then show them the solution for the same.
• Ask the students to write the algorithm first and then show them the solution for the same.
• Tell the students that a circular linked list can be a singly circular or a doubly circular linked list.
• Ask the students to write the algorithm first and then come up with the solution.
• Tell the students that inserting a node in the middle of a circular linked list is same as that of a singly-linked list. This is because the only difference between the two lists is with the arrangement in the address field of the last node. Therefore, the process of insertion of a node at the end of the two lists is different.
• Ask the students to write an algorithm to insert a node between two nodes in a circular linked list.
• Tell the students that in a circular linked list, the process of deleting a node other than the first and the last node is same as that of a singly-linked list. However, the algorithm to locate the node to be deleted is different in a circular linked list. Ask the student to write the algorithm to locate the node to be deleted in a circular linked list.
• Ask the student to write an algorithm to delete a node other than the first and the last node in a circular linked list.
• Linked lists also offers the following applications: To store the data typed in an open Word document. To store the coordinates of the various pixels that constitute a snake in the Snake game. To implement a new data type that can store an integer of any length. To store the details of various objects drawn on a powerpoint slide.
• Linked lists also offers the following applications: To store the data typed in an open Word document. To store the coordinates of the various pixels that constitute a snake in the Snake game. To implement a new data type that can store an integer of any length. To store the details of various objects drawn on a powerpoint slide.
• Linked lists also offers the following applications: To store the data typed in an open Word document. To store the coordinates of the various pixels that constitute a snake in the Snake game. To implement a new data type that can store an integer of any length. To store the details of various objects drawn on a powerpoint slide.

## 06 ds and algorithm session_08Presentation Transcript

• Data Structures and AlgorithmsObjectives In this chapter, you will learn to: Implement a doubly-linked list Implement a circular linked list Apply linked lists to solve programming problems Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List Now consider a case of 100000 numbers display a linked Consider a sorted list in which you need tostored inthese numbers in a descending order. list. How want to display these numbers in ascending order, If youwill you solve this problem? what will node do? Each you is linked to the next node in sequence. Traverse thethat starting from the first list in the forward This means list you can traverse the node. direction only. Such a linked list is called a singly-linked list. To display the numbers in the descending order, you need to reverse the linked list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. Algorithm to reverse a singly-linked list. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. 12. Make the next field of ptr1 point to NULL. 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL a. Set ptr1 = ptr2 b. Set ptr2 = ptr3 c. Make ptr3 point to the next node in its sequence. d. Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) • Declare three variables / pointers ptr1, ptr2, and ptr 3. • If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. 12. Make the next field of ptr1 point to NULL. 14. Make the next field of ptr2 point to ptr1. ptr1 ptr2 ptr3 16. Repeat until ptr3 becomes NULL a. Set ptr1 = ptr2 b. Set ptr2 = ptr3 c. Make ptr3 point to the next node in its sequence. d. Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) • Declare three variables / pointers ptr1, ptr2, and ptr 3. • If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. 12. Make the next field of ptr1 point to NULL. 14. Make the next field of ptr2 point to ptr1. ptr1 ptr2 ptr3 16. Repeat until ptr3 becomes NULL a. Set ptr1 = ptr2 b. Set ptr2 = ptr3 c. Make ptr3 point to the next node in its sequence. d. Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START • Mark the first node in the list as ptr1. 10 15 19 21 32 • Mark the successor of ptr1 as ptr2 • Mark the successor of ptr2 as ptr3. ptr1 • Make the next field of ptr1 point to NULL. • Make the next field of ptr2 point to ptr1. ptr1 ptr2 ptr3 • Repeat until ptr3 becomes NULL a. Set ptr1 = ptr2 b. Set ptr2 = ptr3 c. Make ptr3 point to the next node in its sequence. d. Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START • Mark the first node in the list as ptr1. 10 15 19 21 32 • Mark the successor of ptr1 as ptr2 • Mark the successor of ptr2 as ptr3. ptr1 ptr2 • Make the next field of ptr1 point to NULL. • Make the next field of ptr2 point to ptr1. ptr2 ptr3 • Repeat until ptr3 becomes NULL a. Set ptr1 = ptr2 b. Set ptr2 = ptr3 c. Make ptr3 point to the next node in its sequence. d. Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START • Mark the first node in the list as ptr1. 10 15 19 21 32 • Mark the successor of ptr1 as ptr2 • Mark the successor of ptr2 as ptr3. ptr1 ptr2 ptr3 • Make the next field of ptr1 point to NULL. • Make the next field of ptr2 point to ptr1. ptr3 • Repeat until ptr3 becomes NULL a. Set ptr1 = ptr2 b. Set ptr2 = ptr3 c. Make ptr3 point to the next node in its sequence. d. Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START • Mark the first node in the list as ptr1. 10 15 19 21 32 • Mark the successor of ptr1 as ptr2 • Mark the successor of ptr2 as ptr3. ptr1 ptr2 ptr3 • Make the next field of ptr1 point to NULL. • Make the next field of ptr2 point to ptr1. • Repeat until ptr3 becomes NULL a. Set ptr1 = ptr2 b. Set ptr2 = ptr3 c. Make ptr3 point to the next node in its sequence. d. Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START • Mark the first node in the list as ptr1. 10 15 19 21 32 • Mark the successor of ptr1 as ptr2 • Mark the successor of ptr2 as ptr3. ptr1 ptr2 ptr3 • Make the next field of ptr1 point to NULL. • Make the next field of ptr2 point to ptr1. • Repeat until ptr3 becomes NULL a. Set ptr1 = ptr2 b. Set ptr2 = ptr3 c. Make ptr3 point to the next node in its sequence. d. Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START • Mark the first node in the list as ptr1. 10 15 19 21 32 • Mark the successor of ptr1 as ptr2 • Mark the successor of ptr2 as ptr3. ptr1 ptr2 ptr3 • Make the next field of ptr1 point to NULL. • Make the next field of ptr2 point to ptr1. • Repeat until ptr3 becomes NULL a. Set ptr1 = ptr2 b. Set ptr2 = ptr3 c. Make ptr3 point to the next node in its sequence. d. Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr2 ptr3 12. Make the next field of ptr1 point to NULL. ptr1 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr2 ptr3 12. Make the next field of ptr1 point to NULL. ptr1 ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr3 ptr3 12. Make the next field of ptr1 point to NULL. ptr1 ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr3 12. Make the next field of ptr1 point to NULL. ptr1 ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. • Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr3 12. Make the next field of ptr1 point to NULL. ptr1 ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr3 12. Make the next field of ptr1 point to NULL. ptr2 ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr3 ptr3 12. Make the next field of ptr1 point to NULL. ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr3 12. Make the next field of ptr1 point to NULL. ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. • Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr1 ptr3 12. Make the next field of ptr1 point to NULL. ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr3 12. Make the next field of ptr1 point to NULL. ptr2 ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr3 12. Make the next field of ptr1 point to NULL. ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL ptr3 = NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 12. Make the next field of ptr1 point to NULL. ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL ptr3 = NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. • Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) • Declare three variables / pointers ptr1, ptr2, and ptr 3. • If there is only one node in the list: START a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 12. Make the next field of ptr1 point to NULL. ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL ptr3 = NULL a. Set ptr1 = ptr2 b. Set ptr2 = ptr3 c. Make ptr3 point to the next node in its sequence. d. Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) What is the problem with the previous algorithm? How can you solve this problem? This need to adjust the links if eachthe three the list holds the You problem can be solved for all node in variables reference of itsvisit the next node. addition to its next node in whenever you preceding node in sequence. Disadvantage of this approach: Consider the following list: and time consuming for large lists. This approach is inefficient START 10 15 19 21 23 25 32 Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Doubly-Linked List (Contd.) You can introduce an additional field in each node of a singly-linked list, which would hold the address of its previous node. Such a type of list is known as a doubly-linked list. START 10 15 19 21 23 25 32 Structure of a doubly-linked list Ver. 1.0 Session 8
• Data Structures and AlgorithmsRepresenting a Doubly-Linked List A Linked list is represented in a program by defining two classes: – Node class: In a doubly-linked list, each node needs to store: – The information – The address of the next node in sequence – The address of the previous node // Code in C# class Node { public int info; public Node next; public Node prev; } Ver. 1.0 Session 8
• Data Structures and AlgorithmsRepresenting a Doubly-Linked List (Contd.) // Code in C++ class Node { public: int info; Node * next; Node * prev; }; Ver. 1.0 Session 8
• Data Structures and AlgorithmsRepresenting a Doubly-Linked List(Contd.) – DoubleLinkedList class: This class consists of a set of operations implemented on a linked list. In addition, it declares a variable/pointer, START, which will always point to the first node in the list. // Code in C# class DoubleLinkedList { Node START; DoubleLinkedList(){} public void addNode(int element){} public bool search(int element, ref Node previous, ref Node current){} public bool delNode(int element){} public void traverse() {} public void revtraverse(){} } Ver. 1.0 Session 8
• Data Structures and AlgorithmsRepresenting a Doubly-Linked List(Contd.) // Code in C++ class DoubleLinkedList { Node *START; public: DoubleLinkedList(); void addNode(int element); bool search(int element, Node *previous, Node *current); bool delNode(int element); void traverse(); void revtraverse(); }; Ver. 1.0 Session 8
• Data Structures and AlgorithmsJust a minute How does the representation of a node in a doubly-linked list differ from that of a singly-linked list? Answer: Unlike singly-linked list, in which each node stores the address of only the next node in the list, each node in a doubly-linked list holds the address of its previous node also. Ver. 1.0 Session 8
• Data Structures and AlgorithmsTraversing a Doubly-Linked List 1. Mark the first node in the list as currentNode. Algorithm to traverse a doubly a doubly linked list in the Write an algorithm to traverse 3. Repeat steps 3 and 4 until forward direction. linked list in the forward direction. currentNode becomes NULL. 5. Display the information contained in the node marked as currentNode. 7. Make currentNode point to the next node in sequence. Ver. 1.0 Session 8
• Data Structures and AlgorithmsTraversing a Doubly-Linked List (Contd.) 2. Mark the last node in the list as Algorithm to traverse a doubly a doubly linked list in the Write an algorithm to traverse currentNode. backward direction. linked list in the backward 4. Repeat steps 3 and 4 until currentNode becomes NULL. direction. 6. Display the information contained in the node marked as currentNode. 8. Make currentNode point to the node preceding it. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting Nodes in a Doubly-Linked List A node can be inserted at any of the following positions in a doubly-linked list: Beginning of the list Between two nodes in the list End of the list Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List Write an algorithm to insert a node in the beginning of a doubly-linked list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List (Contd.) 1. Allocate memory for the new node. Algorithm to insert a node in the 3. Assign value to the data field of beginning of a doubly-linked list. the new node. 5. Make the next field of the new node point to the first node in the list. 7. Make the prev field of START point to the new node. 9. Make the prev field of the new node point to NULL. START 11. Make START, point to the new node. 10 15 17 20 Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. • Make the next field of the new node point to the first node in the list. • Make the prev field of START point to the new node. newnode • Make the prev field of the new node point to NULL. START • Make START, point to the new node. 10 10 15 17 20 Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. • Make the next field of the new node point to the first node in the list. • Make the prev field of START point to the new node. newnode • Make the prev field of the new node point to NULL. START • Make START, point to the new 7 node. 10 10 15 17 20 Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. newnode.next = START • Assign value to the data field of the new node. • Make the next field of the new node point to the first node in the list. • Make the prev field of START point to the new node. newnode • Make the prev field of the new node point to NULL. START • Make START, point to the new 7 node. 10 10 15 17 20 Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. newnode.next = START • Assign value to the data field of START.prev = newnode the new node. • Make the next field of the new node point to the first node in the list. • Make the prev field of START point to the new node. newnode • Make the prev field of the new node point to NULL. START • Make START, point to the new 7 node. 10 10 15 17 20 Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. newnode.next = START • Assign value to the data field of START.prev = newnode the new node. newnode.prev = NULL • Make the next field of the new node point to the first node in the list. • Make the prev field of START point to the new node. newnode • Make the prev field of the new node point to NULL. START • Make START, point to the new 7 node. 10 10 15 17 20 Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. newnode.next = START • Assign value to the data field of START.prev = newnode the new node. newnode.prev = NULL • Make the next field of the new node point to the first node in START = newnode the list. • Make the prev field of START point to the new node. newnode • Make the prev field of the new node point to NULL. START • Make START, point to the new 7 node. 10 10 15 17 20 Insertion complete Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List Write an algorithm to insert a node between two nodes in a doubly-linked list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 16 Algorithm to insert a node between locate previous and current, execute the following steps: two nodes in a doubly-linked list. a. Make current point to the first node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 15 17 20 e. Make current point to the next node in sequence. 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node point to current. 5. Make the prev field of the new node point to previous. 6. Make the prev field of current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 16 locate previous and current, execute the following steps: a. Make current point to the first node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 e. Make current point to the next node in sequence. 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node point to current. 5. Make the prev field of the new node point to previous. 6. Make the prev field of current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • Make current point to the next node in sequence. 2. Allocate memory for the new node. 3. Assign value to the data field of the new current node. 4. Make the next field of the new node point to current. 5. Make the prev field of the new node point to previous. 6. Make the prev field of current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • Make current point to the next node in sequence. 2. Allocate memory for the new node. 3. Assign value to the data field of the new current node. 4. Make the next field of the new node previous = NULL point to current. 5. Make the prev field of the new node point to previous. 6. Make the prev field of current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • Make current point to the next node in sequence. 2. Allocate memory for the new node. 3. Assign value to the data field of the new current node. 4. Make the next field of the new node previous = NULL point to current. 5. Make the prev field of the new node point to previous. 6. Make the prev field of current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • Make current point to the next node in sequence. 2. Allocate memory for the new node. 3. Assign value to the data field of the new previous current node. 4. Make the next field of the new node previous = NULL point to current. 5. Make the prev field of the new node point to previous. 6. Make the prev field of current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • Make current point to the next node in sequence. 2. Allocate memory for the new node. 3. Assign value to the data field of the new previous current current node. 4. Make the next field of the new node point to current. 5. Make the prev field of the new node point to previous. 6. Make the prev field of current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • Make current point to the next node in sequence. 2. Allocate memory for the new node. 3. Assign value to the data field of the new previous current node. 4. Make the next field of the new node point to current. 5. Make the prev field of the new node point to previous. 6. Make the prev field of current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • Make current point to the next node in sequence. 2. Allocate memory for the new node. 3. Assign value to the data field of the new previous previous current node. 4. Make the next field of the new node point to current. 5. Make the prev field of the new node point to previous. 6. Make the prev field of current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • Make current point to the next node in sequence. 2. Allocate memory for the new node. 3. Assign value to the data field of the new previous currentcurrent node. 4. Make the next field of the new node point to current. 5. Make the prev field of the new node point to previous. 6. Make the prev field of current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • Make current point to the next node in sequence. 2. Allocate memory for the new node. 3. Assign value to the data field of the new previous current node. 4. Make the next field of the new node point to current. 5. Make the prev field of the new node point to previous. 6. Make the prev field of current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 16 newnode locate previous and current, execute the following steps: a. Make current point to the first node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 e. Make current point to the next node in sequence. • Allocate memory for the new node. • Assign value to the data field of the new previous current node. • Make the next field of the new node point to current. • Make the prev field of the new node point to previous. • Make the prev field of current point to the new node. • Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 16 newnode locate previous and current, execute the following steps: a. Make current point to the first 16 node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 e. Make current point to the next node in sequence. • Allocate memory for the new node. • Assign value to the data field of the new previous current node. • Make the next field of the new node point to current. • Make the prev field of the new node point to previous. • Make the prev field of current point to the new node. • Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To newnode locate previous and current, execute the following steps: a. Make current point to the first 16 node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 e. Make current point to the next node in sequence. • Allocate memory for the new node. • Assign value to the data field of the new previous current node. • Make the next field of the new node point to current. newnode.next = current • Make the prev field of the new node point to previous. • Make the prev field of current point to the new node. • Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To newnode locate previous and current, execute the following steps: a. Make current point to the first 16 node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 e. Make current point to the next node in sequence. • Allocate memory for the new node. • Assign value to the data field of the new previous current node. • Make the next field of the new node point to current. newnode.next = current • Make the prev field of the new node point to previous. newnode.prev = previous • Make the prev field of current point to the new node. • Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To newnode locate previous and current, execute the following steps: a. Make current point to the first 16 node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 e. Make current point to the next node in sequence. • Allocate memory for the new node. • Assign value to the data field of the new previous current node. • Make the next field of the new node point to current. newnode.next = current • Make the prev field of the new node point to previous. newnode.prev = previous • Make the prev field of current point to the new node. current.prev = newnode • Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the Insertion complete new node is to be inserted. Mark them as previous and current respectively. To newnode locate previous and current, execute the following steps: a. Make current point to the first 16 node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 e. Make current point to the next node in sequence. • Allocate memory for the new node. • Assign value to the data field of the new previous current node. • Make the next field of the new node point to current. newnode.next = current • Make the prev field of the new node point to previous. newnode.prev = previous • Make the prev field of current point to the new node. current.prev = newnode • Make the next field of previous point to previous.next = newnode the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To What is the problem with this locate previous and current, execute the following steps: algorithm? a. Make current point to the first node. If current is NULL, then the new b. Make previous point to NULL. node should be inserted at the c. Repeat steps d and e until current.info > newnode.info or end of the list. current = NULL. However, in this case, step 6 will d. Make previous point to current. e. Make current point to the next give an error. node in sequence. 2. Allocate memory for the new node. This is because, NULL cannot 3. Assign value to the data field of the new have a prev field. node. 4. Make the next field of the new node Therefore, you need to modify point to current. 5. Make the prev field of the new node this algorithm so that you can point to previous. also insert a node at the end of 6. Make the prev field of current point to the new node. the list. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 to the modified algorithm Refer locate previous and current, execute the following steps: a. Make current point to the first node. b. Make previous point to NULL. c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. e. Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: a. Make current point to the first node. b. Make previous point to NULL. c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. e. Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of current current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of current current point to the new node. 7. Make the next field of previous point to previous = NULL the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps e and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of current current point to the new node. 7. Make the next field of previous point to previous = NULL the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of currentprevious current point to the new node. 7. Make the next field of previous point to previous = NULL the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of currentprevious current current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of currentprevious current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of currentprevious previous current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of current previous current current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of current previous current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of current previous previous current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of current previous current current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of current previous current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of current previous previous current point to the new node. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To Insert 23 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. • Make current point to the next node in sequence. START 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of current previous current point to the new node. 7. Make the next field of previous point to the new node. current = NULL Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To locate previous and current, execute the following steps: a. Make current point to the first node. b. Make previous point to NULL. c. Repeat steps d and e until current.info > newnode.info or newnode current = NULL. d. Make previous point to current. e. Make current point to the next node in sequence. START • Allocate memory for the new node. • Assign value to the data field of the new node. • Make the next field of the new node 10 10 15 17 20 • point to current. Make the prev field of the new node point to previous. • If current is not NULL: a. Make the prev field of current previous point to the new node. 7. Make the next field of previous point to the new node. current = NULL Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To locate previous and current, execute the following steps: a. Make current point to the first node. b. Make previous point to NULL. c. Repeat steps d and e until current.info > newnode.info or newnode current = NULL. d. Make previous point to current. e. Make current point to the next node in sequence. START 23 • Allocate memory for the new node. • Assign value to the data field of the new node. • Make the next field of the new node 10 10 15 17 20 • point to current. Make the prev field of the new node point to previous. • If current is not NULL: a. Make the prev field of current previous point to the new node. 7. Make the next field of previous point to the new node. current = NULL Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To newnode.next = current locate previous and current, execute the following steps: a. Make current point to the first node. b. Make previous point to NULL. c. Repeat steps d and e until current.info > newnode.info or newnode current = NULL. d. Make previous point to current. e. Make current point to the next node in sequence. START 23 • Allocate memory for the new node. • Assign value to the data field of the new node. • Make the next field of the new node 10 10 15 17 20 • point to current. Make the prev field of the new node point to previous. • If current is not NULL: a. Make the prev field of current previous point to the new node. 7. Make the next field of previous point to the new node. current = NULL Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To newnode.next = current locate previous and current, execute the following steps: newnode.prev = previous a. Make current point to the first node. b. Make previous point to NULL. c. Repeat steps d and e until current.info > newnode.info or newnode current = NULL. d. Make previous point to current. e. Make current point to the next node in sequence. START 23 • Allocate memory for the new node. • Assign value to the data field of the new node. • Make the next field of the new node 10 10 15 17 20 • point to current. Make the prev field of the new node point to previous. • If current is not NULL: a. Make the prev field of current previous point to the new node. 7. Make the next field of previous point to the new node. current = NULL Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To newnode.next = current locate previous and current, execute the following steps: newnode.prev = previous a. Make current point to the first node. b. Make previous point to NULL. c. Repeat steps d and e until current.info > newnode.info or newnode current = NULL. d. Make previous point to current. e. Make current point to the next node in sequence. START 23 • Allocate memory for the new node. • Assign value to the data field of the new node. • Make the next field of the new node 10 10 15 17 20 • point to current. Make the prev field of the new node point to previous. • If current is not NULL: a. Make the prev field of current previous point to the new node. 7. Make the next field of previous point to the new node. current = NULL Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the new node is to be inserted. Mark them as previous and current respectively. To newnode.next = current locate previous and current, execute the following steps: newnode.prev = previous a. Make current point to the first node. previous.next = newnode b. Make previous point to NULL. c. Repeat steps d and e until current.info > newnode.info or newnode current = NULL. d. Make previous point to current. e. Make current point to the next node in sequence. START 23 2. Allocate memory for the new node. 3. Assign value to the data field of the new node. 4. Make the next field of the new node 10 10 15 17 20 point to current. 5. Make the prev field of the new node point to previous. 6. If current is not NULL: a. Make the prev field of current previous point to the new node. Insertion complete 7. Make the next field of previous point to the new node. current = NULL Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the end of the List 1. Allocate memory for the new node. 3. Assign value to the data field of the new Algorithm to insert a node at the at the end of a Write an algorithm to insert a node node. end of a doubly-linkedcontains a variable, LAST,field of the node marked doubly-linked list that list 5. Make the next to keep track of the last node of the list. as LAST point to the new node. 7. Make the prev field of new node point to node marked LAST. 9. Make the next field of the new node point to NULL. 11. Mark the new node as LAST. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting Nodes from a Doubly-Linked List You can delete a node from one of the following places in a doubly-linked list: Beginning of the list Between two nodes in the list End of the list Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the Beginning of the List Write an algorithm to delete a node from the beginning of a doubly-linked list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the Beginning of the List (Contd.) • Mark the first node in the list as current. Algorithm to delete a node from the • beginning of a doubly-linked list. Make START point to the next node in sequence. • If START is not NULL: /* If the node to be deleted is not the only node in the list */ START a. Assign NULL to the prev field of the node marked as START. 10 15 17 20 8. Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the Beginning of the List (Contd.) • Mark the first node in the list as current. • Make START point to the next node in sequence. • If START is not NULL: /* If the node to be deleted is not the only node in the list */ START a. Assign NULL to the prev field of the node marked as START. 10 10 15 17 20 8. Release the memory of the node marked as current. current Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the Beginning of the List (Contd.) • Mark the first node in the list as current. • Make START point to the next node in sequence. • If START is not NULL: /* If the node to be deleted is not the only node in the list */ START START a. Assign NULL to the prev field of the node marked as START. 10 10 15 17 20 8. Release the memory of the node marked as current. current START = START.next Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the Beginning of the List (Contd.) • Mark the first node in the list as current. • Make START point to the next node in sequence. • If START is not NULL: /* If the node to be deleted is not the only node in the list */ START a. Assign NULL to the prev field of the node marked as START. 10 10 15 17 20 8. Release the memory of the node marked as current. current START = START.next START. prev = NULL Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the Beginning of the List (Contd.) • Mark the first node in the list as Delete operation complete current. • Make START point to the next node in sequence. • If START is not NULL: /* If the node to be deleted is not the only node in the list */ START a. Assign NULL to the prev field of the node marked as START. 10 15 17 20 • Release the memory of the node marked as current. current Memory released START = START.next START. prev = NULL Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List Write an algorithm to delete a node after any other node in a doubly-linked list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. To locate previous and current, execute Algorithm Delete 17 to delete a node between the following steps: two nodes in a doubly-linked list. • Make previous point to NULL. // Set previous = NULL START • Make current point to the first node in the linked list. // Set current = START 10 15 17 20 • Repeat steps d and e until either the node is found or current becomes NULL. • Make previous point to current. • Make current point to the next node in sequence. 4. Make the next field of previous point to the successor of current. 6. Make the prev field of the successor of current point to previous. 8. Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. To locate previous and current, execute Delete 17 the following steps: • Make previous point to NULL. // Set previous = NULL START • Make current point to the first node in the linked list. // Set current = START 10 10 15 17 20 • Repeat steps d and e until either the node is found or current becomes NULL. • Make previous point to current. • Make current point to the next node in sequence. 4. Make the next field of previous point to the successor of current. 6. Make the prev field of the successor of current point to previous. 8. Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. To locate previous and current, execute the following steps: • Make previous point to NULL. // Set previous = NULL START • Make current point to the first node in the linked list. // Set current = START 10 15 17 20 • Repeat steps d and e until either the node is found or current becomes NULL. • Make previous point to current. previous = NULL • Make current point to the next node in sequence. 4. Make the next field of previous point to the successor of current. 6. Make the prev field of the successor of current point to previous. 8. Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. To locate previous and current, execute the following steps: • Make previous point to NULL. // Set previous = NULL START • Make current point to the first node in the linked list. // Set current = START 10 10 15 17 20 • Repeat steps d and e until either the node is found or current becomes NULL. current • Make previous point to current. previous = NULL • Make current point to the next node in sequence. 4. Make the next field of previous point to the successor of current. 6. Make the prev field of the successor of current point to previous. 8. Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. To locate previous and current, execute the following steps: • Make previous point to NULL. // Set previous = NULL START • Make current point to the first node in the linked list. // Set current = START 10 10 15 17 20 • Repeat steps d and e until either the node is found or current becomes NULL. current • Make previous point to current. previous = NULL • Make current point to the next node in sequence. 4. Make the next field of previous point to the successor of current. 6. Make the prev field of the successor of current point to previous. 8. Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. To locate previous and current, execute the following steps: • Make previous point to NULL. // Set previous = NULL START • Make current point to the first node in the linked list. // Set current = START 10 10 15 17 20 • Repeat steps d and e until either the node is found or current becomes NULL. current • Make previous point to current. previous previous = NULL • Make current point to the next node in sequence. 4. Make the next field of previous point to the successor of current. 6. Make the prev field of the successor of current point to previous. 8. Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. To locate previous and current, execute the following steps: • Make previous point to NULL. // Set previous = NULL START • Make current point to the first node in the linked list. // Set current = START 10 10 15 17 20 • Repeat steps d and e until either the node is found or current becomes NULL. current current • Make previous point to current. previous • Make current point to the next node in sequence. • Make the next field of previous point to the successor of current. • Make the prev field of the successor of current point to previous. • Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. To locate previous and current, execute the following steps: • Make previous point to NULL. // Set previous = NULL START • Make current point to the first node in the linked list. // Set current = START 10 10 15 17 20 • Repeat steps d and e until either the node is found or current becomes NULL. current • Make previous point to current. previous previous • Make current point to the next node in sequence. 4. Make the next field of previous point to the successor of current. 6. Make the prev field of the successor of current point to previous. 8. Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. To locate previous and current, execute the following steps: • Make previous point to NULL. // Set previous = NULL START • Make current point to the first node in the linked list. // Set current = START 10 10 15 17 20 • Repeat steps d and e until either the node is found or current becomes NULL. previous current current • Make previous point to current. previous • Make current point to the next node in sequence. • Make the next field of previous point to the successor of current. • Make the prev field of the successor of current point to previous. • Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. To locate previous and current, execute the following steps: • Make previous point to NULL. // Set previous = NULL START • Make current point to the first node in the linked list. // Set current = START 10 10 15 17 20 • Repeat steps d and e until either the node is found or current becomes NULL. previous current • Make previous point to current. • Make current point to the next previous. next = current. next node in sequence. • Make the next field of previous point to the successor of current. • Make the prev field of the successor of current point to previous. • Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. To locate previous and current, execute the following steps: • Make previous point to NULL. // Set previous = NULL START • Make current point to the first node in the linked list. // Set current = START 10 10 15 17 20 • Repeat steps d and e until either the node is found or current becomes NULL. previous current • Make previous point to current. • Make current point to the next previous. next = current. next node in sequence. current.next.prev = previous • Make the next field of previous point to the successor of current. • Make the prev field of the successor of current point to previous. • Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. To locate previous and current, execute the following steps: Deletion complete • Make previous point to NULL. // Set previous = NULL START • Make current point to the first node in the linked list. // Set current = START 10 10 15 17 20 • Repeat steps d and e until either the node is found or current becomes NULL. previous current • Make previous point to current. • Make current point to the next previous. next = current. next node in sequence. current.next.prev = previous • Make the next field of previous point to the successor of current. • Make the prev field of the successor of current point to previous. • Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. To locate previous and current, execute What is the problem with this the following steps: algorithm? • Make previous point to If the last node is to be deleted from the NULL. // Set previous = NULL list, then step 3 will give an error. This is because, the successor of • Make current point to the first node in the linked list. // Set current is NULL. Therefore, it cannot current = START have a prev field. • Repeat steps d and e until In that case, you need to modify the either the node is found or algorithm so that the last node can be current becomes NULL. deleted from the list by using the same • Make previous point to current. algorithm. • Make current point to the next node in sequence. 4. Make the next field of previous point to the successor of current. 6. Make the prev field of the successor of current point to previous. 8. Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. Refer 20 Deleteto the modified algorithm 3. Make the next field of previous point to the successor of current. 5. If the successor of current exists: START a. Make the prev field of the successor of current point to previous. 10 15 17 20 8. Release the memory of the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) • Mark the node to be deleted as current and its predecessor as previous. Delete 20 • Make the next field of previous point to the successor of current. • If the successor of current exists: START a. Make the prev field of the successor of current point to previous. 10 10 15 17 20 8. Release the memory of the node marked as current. previous current Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) • Mark the node to be deleted as current and its predecessor as previous. • Make the next field of previous point to the successor of current. • If the successor of current exists: START a. Make the prev field of the successor of current point to previous. 10 10 15 17 20 8. Release the memory of the node marked as current. previous current Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) • Mark the node to be deleted as current and its predecessor as previous. • Make the next field of previous point to the successor of current. • If the successor of current exists: START a. Make the prev field of the successor of current point to previous. 10 10 15 17 20 8. Release the memory of the node marked as current. previous current Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List (Contd.) 1. Mark the node to be deleted as current and its predecessor as previous. 3. Make the next field of previous point to the successor of current. 5. If the successor of current exists: START a. Make the prev field of the successor of current point to previous. 10 10 15 17 20 • Release the memory of the node marked as current. previous current Delete operation complete Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Circular Linked List • Youus supposesingly-linked list. Let can use a you are developing an action game in which a player is given a set of n weapons. However, the weapons need to be displayed in a sequence • repeatedly in a round robin manner. after a specific time Each weapon appears on the screen period. Therefore, once all the weapons have been displayed, the • pointer must start again with the first weapon in the list. The player is required to select the weapon within 10 This requires thethe weapon becomes unusable.time the seconds or else pointer to be reinitialized each • end ofthe nlist is reached. Once the th weapon is displayed, the weapon that came In this displayed it would be the process continues. the node first is situation, again and good if after traversing corresponding to the last weapon, the pointer could • automatically move to the first weapon in the list. of Which data structure will you use to store the list This can be this case? weapons in implemented by using a circular linked list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Circular Linked List (Contd.) You can implement a circular linked list by linking the last node back to the first node in the list. START 10 15 9 11 23 5 12 Ver. 1.0 Session 8
• Data Structures and AlgorithmsImplementing a Circular Linked List (Contd.) In a circular linked list, the last node holds the address of the first node. LAST 10 15 9 11 23 5 12 In a circular linked list, you need to maintain a variable/pointer, LAST, which always point to the last node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsRepresenting a Circular Linked List To represent a circular linked list, you need to declare two classes, Node and List: Node class: The declaration of the Node class of a circular linked list is the same as that of a singly-linked list. List class: This class consists of a set of operations implemented on a linked list. These operations are insertion, deletion, search, and traversal. It also contains the declaration of a variable/pointer, LAST, that always points to the last node in the list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsRepresenting a Circular Linked List (Contd.) // Code in C# class List { private Node LAST; List() { LAST = NULL; } public void addNode(int element) {} public bool search(int element, ref Node previous, ref Node current){} public bool delNode(int element) {} public void traverse() {} } Ver. 1.0 Session 8
• Data Structures and AlgorithmsRepresenting a Circular Linked List (Contd.) // Code in C++ class List { Node * LAST; public: List() { LAST = NULL; } void addNode(int element); bool search(int element, Node *previous, Node *current); bool delNode(int element); void traverse(); }; Ver. 1.0 Session 8
• Data Structures and AlgorithmsTraversing a Circular Linked List 1. Make currentNode point to the successor of the node marked as LAST, such that Algorithm to traverse traverse alinked linked list. Write an algorithm to a circular circular currentNode points to the list. first node in the list. 3. Repeat step 3 and 4 until currentNode = LAST. 5. Display the information contained in the node marked as currentNode. 7. Make currentNode point to the next node in its sequence. 9. Display the information contained in the node marked as LAST. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node in a Circular Linked List In a circular linked list, you can insert a node at any of the following positions: Beginning of the list End of the list Between two node in the list Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List Write an algorithm to insert a node in the beginning of a circular linked list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List (Contd.) 1. Allocate memory for the new node. Algorithm to insert a node in the 3. Assign value to the data field beginning of a circular linked list. of the new node. 5. Make the next field of the new node point to the successor of LAST. LAST 7. Make the next field of LAST point to the new node. 10 10 15 17 20 Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. newnode • Make the next field of the new node point to the successor of LAST. LAST • Make the next field of LAST point to the new node. 10 10 15 17 20 Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. newnode • Make the next field of the new node point to the successor of LAST. 7 LAST • Make the next field of LAST point to the new node. 10 10 15 17 20 Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. newnode • Make the next field of the new node point to the successor of LAST. 7 LAST • Make the next field of LAST point to the new node. 10 10 15 17 20 newnode. next = LAST. next Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. newnode Insertion complete • Make the next field of the new node point to the successor of LAST. 7 LAST • Make the next field of LAST point to the new node. 10 10 15 17 20 newnode. next = LAST. next LAST. next =newnode Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List The algorithm to insert a node between two nodes in a circular linked list is same as that of a singly-linked list. However, the algorithm to search for the nodes between which the new node is to be inserted is a bit different in a circular linked list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) 1. Make current point to the first node. 3. Make previous point to NULL. Algorithm to locate the nodes Insert 16 between which the new node is 5. Repeat steps 4 and 5 until current.info > newnode.info or previous = LAST. to be inserted. 7. Make previous point to current. 9. Make current point to the next node in sequence. LAST 10 15 17 20 Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) • Make current point to the first node. • Make previous point to NULL. • Repeat steps 4 and 5 until current.info > newnode.info or previous = LAST. • Make previous point to current. • Make current point to the next node in sequence. LAST 10 10 15 17 20 current Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) • Make current point to the first node. • Make previous point to NULL. • Repeat steps 4 and 5 until current.info > newnode.info or previous = LAST. • Make previous point to current. • Make current point to the next node in sequence. LAST 10 10 15 17 20 current previous = NULL Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) • Make current point to the first node. • Make previous point to NULL. • Repeat steps 4 and 5 until current.info > newnode.info or previous = LAST. • Make previous point to current. • Make current point to the next node in sequence. LAST 10 10 15 17 20 current previous = NULL Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) • Make current point to the first node. • Make previous point to NULL. • Repeat steps 4 and 5 until current.info > newnode.info or previous = LAST. • Make previous point to current. • Make current point to the next node in sequence. LAST 10 10 15 17 20 previous current previous = NULL Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) • Make current point to the first node. • Make previous point to NULL. • Repeat steps 4 and 5 until current.info > newnode.info or previous = LAST. • Make previous point to current. • Make current point to the next node in sequence. LAST 10 10 15 17 20 previous current current Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) • Make current point to the first node. • Make previous point to NULL. • Repeat steps 4 and 5 until current.info > newnode.info or previous = LAST. • Make previous point to current. • Make current point to the next node in sequence. LAST 10 10 15 17 20 previous previous current Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node Between Two Nodes in the List (Contd.) • Make current point to the first node. • Make previous point to NULL. • Repeat steps 4 and 5 until current.info > newnode.info or previous = LAST. • Make previous point to current. • Make current point to the next node in sequence. Nodes located LAST 10 10 15 17 20 previous current current Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the End of the List Write an algorithm to insert a node at the end of a circular linked list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the End of the List (Contd.) 1. Allocate memory for the new node. Let us insert insert a after node 20 Algorithm to a node node at the 3. Assign value to the data field end of the list of the new node. 5. Make the next field of the new node point to the LAST successor of LAST. 7. Make the next field of LAST point to the new node. 10 15 17 20 9. Mark LAST point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the End of the List (Contd.) • Allocate memory for the new node. Let us insert a node after node 20 • Assign value to the data field of the new node. newnode • Make the next field of the new node point to the LAST successor of LAST. • Make the next field of LAST point to the new node. 10 10 15 17 20 • Mark LAST point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the End of the List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. newnode • Make the next field of the new node point to the LAST successor of LAST. 24 • Make the next field of LAST point to the new node. 10 10 15 17 20 • Mark LAST point to the new node. Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the End of the List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. newnode • Make the next field of the new node point to the LAST successor of LAST. 24 • Make the next field of LAST point to the new node. 10 10 15 17 20 • Mark LAST point to the new node. newnode.next = LAST. next Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the End of the List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. newnode • Make the next field of the new node point to the LAST successor of LAST. 24 • Make the next field of LAST point to the new node. 10 10 15 17 20 • Mark LAST point to the new node. newnode.next = LAST. next LAST. next = newnode Ver. 1.0 Session 8
• Data Structures and AlgorithmsInserting a Node at the End of the List (Contd.) • Allocate memory for the new node. Insertion complete • Assign value to the data field of the new node. newnode • Make the next field of the new node point to the LAST successor of LAST. 24 • Make the next field of LAST point to the new node. 10 10 15 17 20 • Mark LAST point to the new node. newnode.next = LAST. next LAST. next = newnode LAST = newnode Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node from a Circular Linked List You can delete a node from any of the following places in a circular linked list: Beginning of the list End of the list Between two nodes in the list Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the Beginning of the List Write an algorithm to delete a node from the beginning of a circular linked list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the Beginning of the List (Contd.) • If the node to be deleted is the only node in the list: // If LAST points to Algorithm for deleting a node from the itself beginning of a circular linked list. a. Mark LAST as NULL b. Exit 4. Make current point to the LAST successor of LAST 6. Make the next field of LAST point to the successor of current 10 15 17 20 8. Release the memory for the node marked as current Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the Beginning of the List (Contd.) • If the node to be deleted is the only node in the list: // If LAST points to itself a. Mark LAST as NULL b. Exit 4. Make current point to the LAST successor of LAST 6. Make the next field of LAST point to the successor of current 10 10 15 17 20 8. Release the memory for the node marked as current Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the Beginning of the List (Contd.) • If the node to be deleted is the only node in the list: // If LAST points to itself a. Mark LAST as NULL b. Exit • Make current point to the LAST successor of LAST • Make the next field of LAST point to the successor of current 10 10 15 17 20 • Release the memory for the node marked as current current Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the Beginning of the List (Contd.) • If the node to be deleted is the only node in the list: // If LAST points to itself a. Mark LAST as NULL b. Exit • Make current point to the LAST successor of LAST • Make the next field of LAST point to the successor of current 10 10 15 17 20 • Release the memory for the node marked as current current LAST. next = current.next Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the Beginning of the List (Contd.) • If the node to be deleted is the only node in the Deletion Complete list: // If LAST points to itself a. Mark LAST as NULL b. Exit • Make current point to the LAST successor of LAST • Make the next field of LAST point to the successor of current 10 15 17 20 • Release the memory for the node marked as current current LAST. next = current.next Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List Delete operation in between two nodes in a circular linked list is same as that of a singly-linked list. However, the algorithm to locate the node to be deleted is a bit different in a circular linked list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List 1. Make previous point to the successor of LAST Delete 17 for locating the node to be Algorithm 3. Make current point to the deleted from a circular linked list. successor of LAST 5. Repeat steps 4 and 5 until either the node is found, or previous = LAST LAST 7. Make previous point to current 10 15 20 9. Make current point to the 17 next node in sequence Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List • Make previous point to the successor of LAST Delete 17 • Make current point to the successor of LAST • Repeat steps 4 and 5 until either the node is found, or previous = LAST LAST • Make previous point to current • Make current point to the 10 10 15 17 20 next node in sequence previous Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List • Make previous point to the successor of LAST Delete 17 • Make current point to the successor of LAST • Repeat steps 4 and 5 until either the node is found, or previous = LAST LAST • Make previous point to current • Make current point to the 10 10 15 17 20 next node in sequence current previous Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List • Make previous point to the successor of LAST Delete 17 • Make current point to the successor of LAST • Repeat steps 4 and 5 until either the node is found, or previous = LAST LAST • Make previous point to current • Make current point to the 10 10 15 17 20 next node in sequence current previous Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List • Make previous point to the successor of LAST Delete 17 • Make current point to the successor of LAST • Repeat steps 4 and 5 until either the node is found, or previous = LAST LAST • Make previous point to current • Make current point to the 10 10 15 17 20 next node in sequence current previous Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List • Make previous point to the successor of LAST Delete 17 • Make current point to the successor of LAST • Repeat steps 4 and 5 until either the node is found, or previous = LAST LAST • Make previous point to current • Make current point to the 10 10 15 17 20 next node in sequence current previous current Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List • Make previous point to the successor of LAST Delete 17 • Make current point to the successor of LAST • Repeat steps 4 and 5 until either the node is found, or previous = LAST LAST • Make previous point to current • Make current point to the 10 10 15 17 20 next node in sequence previous current Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List • Make previous point to the successor of LAST Delete 17 • Make current point to the successor of LAST • Repeat steps 4 and 5 until either the node is found, or previous = LAST LAST • Make previous point to current • Make current point to the 10 10 15 17 20 next node in sequence previous current previous Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node Between Two Nodes in the List • Make previous point to the successor of LAST Once you locate the node to be • Make current point to the deleted along with its predecessor, successor of LAST the process of deletion is same as • Repeat steps 4 and 5 that of a singly-linked list. until either the node is found, or previous = LAST LAST • Make previous point to current • Make current point to the 10 10 15 17 20 next node in sequence previous current current Nodes located Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the End of the List Write an algorithm to delete a node from the end of a circular linked list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the End of the List (Contd.) 1. Make current point to LAST. 3. Mark the predecessor of LAST Let us perform a delete operation on the Algorithm for deleting a node from the as previous. To locate the last node circular linked list. list. end of a of the given circular predecessor of LAST, execute the following steps: LAST a. Make previous point to the successor of LAST. c. Repeat step c until the 10 15 17 20 successor of previous becomes LAST. e. Make previous point to the next node in its sequence. 5. Make the next field of previous point to the successor of LAST. 7. Mark previous as LAST. 9. Release the memory for the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the End of the List (Contd.) • Make current point to LAST. • Mark the predecessor of LAST as previous. To locate the predecessor of LAST, execute the following steps: LAST a. Make previous point to the successor of LAST. c. Repeat step c until the 10 10 15 17 20 successor of previous becomes LAST. e. Make previous point to current the next node in its sequence. 5. Make the next field of previous point to the successor of LAST. 7. Mark previous as LAST. 9. Release the memory for the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the End of the List (Contd.) 1. Make current point to LAST. 3. Mark the predecessor of LAST as previous. To locate the predecessor of LAST, execute the following steps: LAST • Make previous point to the successor of LAST. • Repeat step c until the 10 10 15 17 20 successor of previous becomes LAST. • Make previous point to previous current the next node in its sequence. 5. Make the next field of previous point to the successor of LAST. 7. Mark previous as LAST. 9. Release the memory for the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the End of the List (Contd.) 1. Make current point to LAST. 3. Mark the predecessor of LAST as previous. To locate the predecessor of LAST, execute the following steps: LAST • Make previous point to the successor of LAST. • Repeat step c until the 10 10 15 17 20 successor of previous becomes LAST. • Make previous point to previous current the next node in its sequence. 5. Make the next field of previous point to the successor of LAST. 7. Mark previous as LAST. 9. Release the memory for the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the End of the List (Contd.) 1. Make current point to LAST. 3. Mark the predecessor of LAST as previous. To locate the predecessor of LAST, execute the following steps: LAST • Make previous point to the successor of LAST. • Repeat step c until the 10 10 15 17 20 successor of previous becomes LAST. • Make previous point to previous previous current the next node in its sequence. 5. Make the next field of previous point to the successor of LAST. 7. Mark previous as LAST. 9. Release the memory for the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the End of the List (Contd.) 1. Make current point to LAST. 3. Mark the predecessor of LAST as previous. To locate the predecessor of LAST, execute the following steps: LAST • Make previous point to the successor of LAST. • Repeat step c until the 10 10 15 17 20 successor of previous becomes LAST. • Make previous point to previous current the next node in its sequence. 5. Make the next field of previous point to the successor of LAST. 7. Mark previous as LAST. 9. Release the memory for the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the End of the List (Contd.) 1. Make current point to LAST. 3. Mark the predecessor of LAST as previous. To locate the predecessor of LAST, execute the following steps: LAST • Make previous point to the successor of LAST. • Repeat step c until the 10 10 15 17 20 successor of previous becomes LAST. • Make previous point to previous previous current the next node in its sequence. 5. Make the next field of previous point to the successor of LAST. 7. Mark previous as LAST. 9. Release the memory for the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the End of the List (Contd.) 1. Make current point to LAST. 3. Mark the predecessor of LAST as previous. To locate the predecessor of LAST, execute the following steps: LAST a. Make previous point to the successor of LAST. c. Repeat step c until the 10 10 15 17 20 successor of previous becomes LAST. e. Make previous point to previous current the next node in its sequence. • Make the next field of previous previous.next = LAST.next point to the successor of LAST. • Mark previous as LAST. • Release the memory for the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the End of the List (Contd.) 1. Make current point to LAST. 3. Mark the predecessor of LAST as previous. To locate the predecessor of LAST, execute the following steps: LAST LAST a. Make previous point to the successor of LAST. c. Repeat step c until the 10 10 15 17 20 successor of previous becomes LAST. e. Make previous point to previous current the next node in its sequence. • Make the next field of previous previous.next = LAST.next point to the successor of LAST. LAST = previous • Mark previous as LAST. • Release the memory for the node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsDeleting a Node From the End of the List (Contd.) 1. Make current point to LAST. 3. Mark the predecessor of LAST as previous. To locate the predecessor of LAST, execute the following steps: LAST a. Make previous point to Memory released the successor of LAST. c. Repeat step c until the 10 10 15 17 20 successor of previous becomes LAST. e. Make previous point to previous current the next node in its sequence. • Make the next field of previous previous.next = LAST.next point to the successor of LAST. LAST = previous • Mark previous as LAST. • Release the memory for the Delete operation complete node marked as current. Ver. 1.0 Session 8
• Data Structures and AlgorithmsApplications of Linked Lists Linked lists provide an efficient mechanism for storing data. They are used to solve many programming problems easily. They form the basic foundation for implementing various other data structures such as stacks, queues, and binary trees. Ver. 1.0 Session 8
• Data Structures and AlgorithmsGaming Applications • Linked lists are implemented in various gaming applications. Consider a game in which the player protects himself from the enemy by firing bullets. Whenever a bullet is fired, its details (size, color, and coordinates) need to be stored somewhere. The details of the bullets are stored in a linked list, because the total number of bullets to be fired are not known in advance. The coordinates of the bullets stored in a node are updated on a regular basis to indicate that the bullet is moving forward. The same is then rendered on the screen. Ver. 1.0 Session 8
• Data Structures and AlgorithmsFile System in Operating System Linked lists are used to implement the internals of a file system. A file is divided into blocks, which are scattered randomly on the disk. When a new file is created, a new block of memory is allocated to it. The new block may not be contiguous to the block that was earlier allocated. Therefore, each block also contains the address of the next allocated block, forming a linked list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsAdding Polynomials Using Linked Lists Linked lists can be used to implement various arithmetic operations on polynomial expressions. In the polynomial expression, 4x5 + 5x4 + 2x3 + 3x2 + 7x, each occurrence of variable x is attached to two values: Coefficient Exponent Ver. 1.0 Session 8
• Data Structures and AlgorithmsAdding Polynomials Using Linked Lists (Contd.) Each node holds three pieces of information: Coefficient Exponent Address of the next node in sequence 910 6 coefficient exponent address of the next node Ver. 1.0 Session 8
• Data Structures and AlgorithmsAdding Polynomials Using Linked Lists (Contd.) Let us now perform addition of two polynomial expressions. Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x Polynomial 2: 9x6 + 6x4 + 3x2 Ver. 1.0 Session 8
• Data Structures and AlgorithmsAdding Polynomials Using Linked Lists (Contd.) Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x Polynomial 2: 9x6 + 6x4 + 3x2 Read the first node of Polynomial 1 and the first node of Polynomial 2. Compare the exponent values of the two nodes. The node read from Polynomial 2 has a greater exponent value (6>5). Add the node with exponent value 6 to the list for Polynomial 3. 9 10 6 Polynomial 3: 9x6 Ver. 1.0 Session 8
• Data Structures and AlgorithmsAdding Polynomials Using Linked Lists (Contd.) Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x Polynomial 2: 9x6 + 6x4 + 3x2 Read the next node from Polynomial 2. Compare the exponent value of current node of Polynomial 1 with that of Polynomial 2. The node read from Polynomial 1 has a greater exponent value (5>4). Therefore, add the node with the exponent value 5 to the list for Polynomial 3. 9 10 6 4 10 5 Polynomial 3: 9x6 + 4x5 Ver. 1.0 Session 8
• Data Structures and AlgorithmsAdding Polynomials Using Linked Lists (Contd.) Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x Polynomial 2: 9x6 + 6x4 + 3x2 Read the next node from Polynomial 1. Compare the exponent value of the current node of Polynomial 1 with that of Polynomial 2. Both the exponents are equal, that is, 4. Add the coefficients of both the nodes. The resultant node now has the coefficient value 11 (6+5). Add the resultant node to the list for Polynomial 3. 9 10 6 4 10 5 11 10 4 Polynomial 3: 9x6 + 4x5 + 11x4 Ver. 1.0 Session 8
• Data Structures and AlgorithmsAdding Polynomials Using Linked Lists (Contd.) Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x Polynomial 2: 9x6 + 6x4 + 3x2 Read the next node from both the lists. Compare the exponent value of the current node of Polynomial 1 with that of Polynomial 2. The node read from Polynomial 1 has a greater exponent value (3>2). Add the node with the exponent value 3 to the list for Polynomial 3. 9 10 6 4 10 5 11 10 4 2 10 3 Polynomial 3: 9x6 + 4x5 + 11x4 + 2x3 Ver. 1.0 Session 8
• Data Structures and AlgorithmsAdding Polynomials Using Linked Lists (Contd.) Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x Polynomial 2: 9x6 + 6x4 + 3x2 Read the next node from Polynomial 1. Compare the exponent value of the current node of Polynomial 1 with that of Polynomial 2. Both the exponents are equal, that is, 2. Add the coefficients of both the nodes. The resultant node now has the coefficient value 6 (3+3). Add the resultant node to the list for Polynomial 3. 9 10 6 4 10 5 11 10 4 2 10 3 6 10 2 Polynomial 3: 9x6 + 4x5 + 11x4 + 2x3 + 6x2 Ver. 1.0 Session 8
• Data Structures and AlgorithmsAdding Polynomials Using Linked Lists (Contd.) Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x Polynomial 2: 9x6 + 6x4 + 3x2 Now there are no more nodes in Polynomial 2. Therefore, add the remaining nodes of Polynomial 1 to the resultant list. 9 10 6 4 10 5 11 10 4 2 10 3 6 10 2 7 10 1 Polynomial 3: 9x6 + 4x5 + 11x4 + 2x3 + 6x2 + 7x Ver. 1.0 Session 8
• Data Structures and AlgorithmsSummary In this session, you learned that: In a doubly-linked list, each node needs to store: – The information. – The address of the next node in the list. – The address of the previous node in the list. – Doubly-linked list enables you to traverse the list in forward as well as backward direction. – A singly-linked list can be made circular by making the last node in the list point back to the first node in the list. Ver. 1.0 Session 8
• Data Structures and AlgorithmsSummary (Contd.) Linked lists offer various applications. For example: They form the basis of various other data structures such as stacks and queues, and binary trees. They are used in various gaming applications. They are used to implement internals of file system internals in various operating systems. They offer a simple and convenient mechanism to perform various arithmetic operations on polynomial expressions. Ver. 1.0 Session 8