Upcoming SlideShare
×

# 06 ds and algorithm session_08

906
-1

Published on

Published in: Technology, Education
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
906
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
0
0
Likes
2
Embeds 0
No embeds

No notes for slide
• 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_08

1. 1. 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
2. 2. 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
3. 3. 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
4. 4. 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
5. 5. 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
6. 6. 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
7. 7. 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
8. 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
9. 9. 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
10. 10. 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
11. 11. 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
12. 12. 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
13. 13. 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
14. 14. 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
15. 15. 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
16. 16. 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
17. 17. 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
18. 18. 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
19. 19. 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
20. 20. 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
21. 21. 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
22. 22. 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
23. 23. 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
24. 24. 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
25. 25. 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
26. 26. 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
27. 27. 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
28. 28. 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
29. 29. 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
30. 30. 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
31. 31. 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
32. 32. 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
33. 33. 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
34. 34. 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
35. 35. 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
36. 36. 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
37. 37. 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
38. 38. 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
39. 39. 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
40. 40. 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
41. 41. 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
42. 42. 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
43. 43. 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
44. 44. 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
45. 45. 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
46. 46. 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
47. 47. 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
48. 48. 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
49. 49. 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
50. 50. 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
51. 51. 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
52. 52. 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
53. 53. 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
54. 54. 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
55. 55. 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
56. 56. 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
57. 57. 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
58. 58. 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
59. 59. 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
60. 60. 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