Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

05 ds and algorithm session_07

1,349 views

Published on

Published in: Technology
  • Be the first to comment

05 ds and algorithm session_07

  1. 1. Data Structures and AlgorithmsObjectives In this session, you will learn to: Identify the features of linked lists Implement a singly-linked list Ver. 1.0 Session 7
  2. 2. Data Structures and AlgorithmsLinked List Suppose you have to write an algorithm to generate and store all prime numbers between 1 and 10,00,000 and display them. How will you solve this problem? Ver. 1.0 Session 7
  3. 3. Data Structures and AlgorithmsLinked List (Contd.) Consider the following algorithm, which uses an array to solve this problem: 1. Set I = 0 2. Repeat step 3 varying N from 2 to 1000000 3. If N is a prime number a. Set A[I] = N // If N is prime store it in an array b. I = I + 1 • Repeat step 5 varying J from 0 to I-1 • Display A[J] // Display the prime numbers // stored in the array Ver. 1.0 Session 7
  4. 4. Data Structures and AlgorithmsLinked List (Contd.) What is the problem in this algorithm? The number of prime numbers between 1 and 10,00,000 is not known. Since you are using an array to store the prime numbers, you need to declare an array of arbitrarily large size to store the prime numbers. Disadvantages of this approach, suppose you declare an array of size N: If the number of prime numbers between 1 and 10,00,000 is more than N then all the prime numbers cannot be stored. If the number of prime numbers is much less than N, a lot of memory space is wasted. Ver. 1.0 Session 7
  5. 5. Data Structures and AlgorithmsLinked List (Contd.) • Thus, you cannot use an array to store a set of elements if you do not know the total number of elements in advance. • How do you solve this problem? By having some way in which you can allocate memory as and when it is required. Ver. 1.0 Session 7
  6. 6. Data Structures and AlgorithmsDynamic Memory Allocation • If you knowdeclare an array, a contiguous block ofarray you When you the address of the first element in the memory can calculate the address of any other elements as shown: is allocated. Let Address of theyou declare an array of size 10 to store first us suppose first element + (size of the element × index of the element) 10 prime numbers. 0 1000 1 1002 2 1004 One contiguous block of 3 1006 4 1008 memory allocated for 5 1010 the array to store 10 6 1012 7 1014 elements. 8 1016 9 1018 Memory representation Ver. 1.0 Session 7
  7. 7. Data Structures and AlgorithmsDynamic Memory Allocation (Contd.) When memory is allocated dynamically, a block of memory is assigned arbitrarily from any location in the memory. Therefore, unlike arrays, these blocks may not be contiguous and may be spread randomly in the memory. 0 1000 1 1002 2 1004 One contiguous block of 3 1006 4 1008 memory allocated for 5 1010 the array to store 10 6 1012 7 1014 elements. 8 1016 9 1018 Memory representation Ver. 1.0 Session 7
  8. 8. Data Structures and AlgorithmsDynamic Memory Allocation (Contd.) Let us see how this happens by allocating memory dynamically for the prime numbers. Allocate memory for 2 1002 2 Memory allocated for 2 Memory representation Ver. 1.0 Session 7
  9. 9. Data Structures and AlgorithmsDynamic Memory Allocation (Contd.) Let us see how this happens. Allocate memory for 3 1002 2 Memory allocated for 3 1036 3 Memory representation Ver. 1.0 Session 7
  10. 10. Data Structures and AlgorithmsDynamic Memory Allocation (Contd.) Let us see how this happens. Allocate memory for 5 1002 2 Memory allocated for 5 1008 5 1036 3 Memory representation Ver. 1.0 Session 7
  11. 11. Data Structures and AlgorithmsDynamic Memory Allocation (Contd.) Let us see how this happens. Allocate memory for 7 1002 2 Memory allocated for 7 1008 5 1020 7 1036 3 Memory representation Ver. 1.0 Session 7
  12. 12. Data Structures and AlgorithmsDynamic Memory Allocation (Contd.) Let us see how this happens. Allocate memory for 11 1002 2 Memory allocated for 11 1008 5 1020 7 1030 11 1036 3 Memory representation Ver. 1.0 Session 7
  13. 13. Data Structures and AlgorithmsDynamic Memory Allocation (Contd.) To access know the address of the know its address. Now, if youany element, you need to first element, you cannot calculate the address of the rest of the elements. This is because, all the elements are spread at random locations in the memory. 1002 2 1008 5 1020 7 1030 11 1036 3 Memory representation Ver. 1.0 Session 7
  14. 14. Data Structures and AlgorithmsDynamic Memory Allocation (Contd.) Therefore, it would be good if every allocated block of memory contains the address of the next block in sequence. This gives the list a linked structure where each block is linked to the next block in sequence. 1002 2 1036 1008 5 1020 1020 7 1030 1030 11 1036 3 1008 Memory representation Ver. 1.0 Session 7
  15. 15. Data Structures and AlgorithmsDynamic Memory Allocation (Contd.) An example of a a variable, START, that stores this concept You can declare data structure that implements the address is the first list. of a linked block. You can now begin at START and move through the list by following the links. START 2 1036 1002 1008 5 1020 1020 7 1030 1030 11 1036 3 1008 Memory representation Ver. 1.0 Session 7
  16. 16. Data Structures and AlgorithmsDefining Linked Lists Linked list: Is a dynamic data structure. Allows memory to be allocated as and when it is required. Consists of a chain of elements, in which each element is referred to as a node. Ver. 1.0 Session 7
  17. 17. Data Structures and AlgorithmsDefining Linked Lists (Contd.) A node is the basic building block of a linked list. A node consists of two parts: – Data: Refers to the information held by the node – Link: Holds the address of the next node in the list Data Link Node Ver. 1.0 Session 7
  18. 18. Data Structures and AlgorithmsDefining Linked Lists (Contd.) The last node in a linked list does not point to any other All the nodes in a linked list are present at arbitrary memory node. Therefore, it points to NULL. locations. Therefore, every node in a linked list has link field that stores the address of the next node in sequence. NULL 10 Data 3352 10 Data 5689 10 Data 1012 10 Data 2403 3352 5689 1012 Ver. 1.0 Session 7
  19. 19. Data Structures and AlgorithmsDefining Linked Lists (Contd.) To keep track of the first node, declare a variable/pointer, START, which always points to the first node. When the list is empty, START contains null. NULL START 10 Data 3352 10 Data 5689 10 Data 1012 10 Data 2403 3352 5689 1012 Ver. 1.0 Session 7
  20. 20. Data Structures and AlgorithmsImplementing a Singly-Linked List Let us solve the given problem of storing prime numbers using a linked list. 1. Repeat step 2 varying N from 2 to 1000000. 2. If N is a prime number, insert it in the linked list: a. Allocate memory for a new node. b. Store the prime number in the new node. c. Attach the new node in the linked list. 3. Display the prime numbers stored in the linked list. Ver. 1.0 Session 7
  21. 21. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List • Consider the following linked list that stores prime numbers. START 2 3 5 7 • When a new prime number is generated, it should be inserted at the end of the list. • Initially, when the list is empty, START contains NULL. Ver. 1.0 Session 7
  22. 22. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. Consider the given algorithm to 3. Assign value to the data field of the new node. insert a node in a linked list. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  23. 23. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. START = that the list is initially Consider NULL 3. Assign value to the data field of the new node. empty. Insert a prime number 2. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  24. 24. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. START = NULL • Assign value to the data field of the new node. Insert a prime number 2. • If START is NULL, then: a. Make START point to the new node. b. Go to step 6. 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  25. 25. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. START = NULL • Assign value to the data field of the new node. • If START is NULL, then: a. Make START point to the new node. b. Go to step 6. 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  26. 26. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. START = NULL • Assign value to the data field of the new node. • If START is NULL, then: a. Make START point to the new node. b. Go to step 6. 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  27. 27. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. START = NULL 3. Assign value to the data field of the new node. 5. If START is NULL, then: • Make START point to the new node. • Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  28. 28. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then: • Make START point to the new node. • Go to step 6. START • Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  29. 29. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode Insertion complete becomes NULL. c. Make currentNode point to the next node in sequence. • Make the next field of currentNode point to the new node. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  30. 30. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. Insert a prime number 3. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  31. 31. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. • Insert a prime number 3. • Assign value to the data field of the new node. • If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  32. 32. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. • If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  33. 33. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. • If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  34. 34. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START • Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  35. 35. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 following steps: • Mark the first node as currentNode. • Repeat step c until the currentNode successor of currentNode becomes NULL. • Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  36. 36. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 following steps: • Mark the first node as currentNode. • Repeat step c until the currentNode successor of currentNode becomes NULL. • Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  37. 37. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the currentNode successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. • Make the next field of currentNode point to the new node. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  38. 38. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the currentNode successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. Insertion complete • Make the next field of currentNode point to the new node. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  39. 39. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. • Insert a prime number 5. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the currentNode successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  40. 40. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. Insert a prime number 5. • Assign value to the data field of the new node. • If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  41. 41. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. • If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 5 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  42. 42. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. • If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 5 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  43. 43. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START • Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 5 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  44. 44. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 5 10 following steps: • Mark the first node as currentNode. • Repeat step c until the currentNode successor of currentNode becomes NULL. • Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  45. 45. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 5 10 following steps: • Mark the first node as currentNode. • Repeat step c until the currentNode successor of currentNode becomes NULL. • Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  46. 46. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 5 10 following steps: • Mark the first node as currentNode. • Repeat step c until the currentNodecurrentNode successor of currentNode becomes NULL. • Make currentNode point to the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  47. 47. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 5 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the currentNode successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. • Make the next field of currentNode point to the new node. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  48. 48. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then: a. Make START point to the new node. b. Go to step 6. START 7. Locate the last node in the list, and mark it as currentNode. To locate the last node in the list, execute the 2 10 3 10 5 10 following steps: a. Mark the first node as currentNode. b. Repeat step c until the currentNode successor of currentNode becomes NULL. c. Make currentNode point to the next node in sequence. Insertion complete • Make the next field of currentNode point to the new node. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  49. 49. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. What is thea better approach is to Therefore, problem with this 3. Assign value to the data field of the new node. algorithm?variable, LAST, which declare a 5. If START is NULL, then: will Consider a address of the last store the list of 10000 numbers. a. Make START point to the node in the a number at the end of To insert list. new node. b. Go to step 6. the list, you first need to locate the Hence, you need to maintain two 7. Locate the last node in the list, and variables, STARTlist. LAST, to last node in the and mark it as currentNode. To locate To reach the last node, you have keep track of the first and last the last node in the list, execute the following steps: to start from the first node and nodes in the list respectively. a. Mark the first node as visit all the preceding nodes currentNode. If the list is empty, START node. before you reach the last and b. Repeat step c until the successor of currentNode LAST point to NULL. becomes NULL. This approach is very time c. Make currentNode point to consuming for lengthy lists. the next node in sequence. 8. Make the next field of currentNode point to the new node. 10. Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  50. 50. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. Refer to the given algorithm to 3. Assign value to the data field of the new node. insert a node at the end of the 5. If START is NULL, then (If the list is linked list. empty): a. Make START point to the new node. b. Make LAST point to the new node. c. Go to step 6. • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  51. 51. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. START = NULL Consider that the list is initially 3. Assign value to the data field of the new node. empty. NULL LAST = 5. If START is NULL, then (If the list is Insert a prime number 2. empty): a. Make START point to the new node. b. Make LAST point to the new node. c. Go to step 6. • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  52. 52. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. START = NULL • Assign value to the data field of the new node. LAST = NULL • If START is NULL, then (If the list is Insert a prime number 2. empty): a. Make START point to the new node. b. Make LAST point to the new node. c. Go to step 6. • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  53. 53. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. START = NULL • Assign value to the data field of the new node. LAST = NULL • If START is NULL, then (If the list is empty): a. Make START point to the new node. b. Make LAST point to the new node. c. Go to step 6. 2 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  54. 54. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. START = NULL • Assign value to the data field of the new node. LAST = NULL • If START is NULL, then (If the list is empty): a. Make START point to the new node. b. Make LAST point to the new node. c. Go to step 6. 2 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  55. 55. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. START = NULL 3. Assign value to the data field of the new node. LAST = NULL 5. If START is NULL, then (If the list is empty): • Make START point to the new node. START • Make LAST point to the new node. • Go to step 6. 2 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  56. 56. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. LAST = NULL 5. If START is NULL, then (If the list is empty): • Make START point to the new node. START LAST • Make LAST point to the new node. • Go to step 6. 2 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  57. 57. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then (If the list is empty): • Make START point to the new node. START LAST • Make LAST point to the new node. • Go to step 6. 2 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  58. 58. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then (If the list is empty): a. Make START point to the new node. START LAST b. Make LAST point to the new node. c. Go to step 6. 2 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node Insertion complete point to NULL. Ver. 1.0 Session 7
  59. 59. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. Insert a prime number 3. 3. Assign value to the data field of the new node. 5. If START is NULL, then (If the list is empty): a. Make START point to the new node. START LAST b. Make LAST point to the new node. c. Go to step 6. 2 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  60. 60. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. Insert a prime number 3. • Assign value to the data field of the new node. • If START is NULL, then (If the list is empty): a. Make START point to the new node. START LAST b. Make LAST point to the new node. c. Go to step 6. 2 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  61. 61. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. • If START is NULL, then (If the list is empty): a. Make START point to the new node. START LAST b. Make LAST point to the new node. c. Go to step 6. 2 10 3 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  62. 62. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. • If START is NULL, then (If the list is empty): a. Make START point to the new node. START LAST b. Make LAST point to the new node. c. Go to step 6. 2 10 3 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  63. 63. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then (If the list is empty): a. Make START point to the new node. START LAST b. Make LAST point to the new node. c. Go to step 6. 2 10 3 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  64. 64. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then (If the list is empty): a. Make START point to the new node. START LAST LAST b. Make LAST point to the new node. c. Go to step 6. 2 10 3 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  65. 65. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. 3. Assign value to the data field of the new node. 5. If START is NULL, then (If the list is empty): a. Make START point to the new node. START LAST b. Make LAST point to the new node. c. Go to step 6. 2 10 3 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node Insertion complete point to NULL. Ver. 1.0 Session 7
  66. 66. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) 1. Allocate memory for the new node. Insert a prime number 5. 3. Assign value to the data field of the new node. 5. If START is NULL, then (If the list is empty): a. Make START point to the new node. START LAST b. Make LAST point to the new node. c. Go to step 6. 2 10 3 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  67. 67. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. Insert a prime number 5. • Assign value to the data field of the new node. • If START is NULL, then (If the list is empty): a. Make START point to the new node. START LAST b. Make LAST point to the new node. c. Go to step 6. 2 10 3 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  68. 68. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. • If START is NULL, then (If the list is empty): a. Make START point to the new node. START LAST b. Make LAST point to the new node. c. Go to step 6. 2 10 3 10 5 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7
  69. 69. Data Structures and AlgorithmsInserting a Node in a Singly-Linked List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. • If START is NULL, then (If the list is empty): a. Make START point to the new node. START LAST b. Make LAST point to the new node. c. Go to step 6. 2 10 3 10 5 10 • Make the next field of LAST point to the new node. • Mark the new node as LAST. • Make the next field of the new node point to NULL. Ver. 1.0 Session 7

×