05 DS and Algorithm Session 07

Embed Size (px)

Citation preview

  • 8/2/2019 05 DS and Algorithm Session 07

    1/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Objectives

    In this session, you will learn to:

    Identify the features of linked lists

    Implement a singly-linked list

  • 8/2/2019 05 DS and Algorithm Session 07

    2/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Suppose you have to write an algorithm to generate andstore all prime numbers between 1 and 10,00,000 anddisplay them.

    How will you solve this problem?

    Linked List

  • 8/2/2019 05 DS and Algorithm Session 07

    3/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Consider the following algorithm, which uses an array tosolve this problem:

    1. Set I = 0

    2. Repeat step 3 varying N from 2 to 1000000

    3. If N is a prime numbera. Set A[I] = N // If N is prime store it in an array

    b. I = I + 1

    4. Repeat step 5 varying J from 0 to I-1

    5. Display A[J] // Display the prime numbers// stored in the array

    Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    4/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    What is the problem in this algorithm?

    The number of prime numbers between 1 and 10,00,000 is notknown. Since you are using an array to store the primenumbers, you need to declare an array of arbitrarily large sizeto store the prime numbers.

    Disadvantages of this approach, suppose you declare an arrayof size N:

    If the number of prime numbers between 1 and 10,00,000is more than N then all the prime numbers cannot bestored.

    If the number of prime numbers is much less than N, a lotof memory space is wasted.

    Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    5/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Thus, you cannot use an array to store a set of elements ifyou 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.

    Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    6/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    When you declare an array, a contiguous block of memoryis allocated.

    Let us suppose you declare an array of size 10 to store first10 prime numbers.

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    1000

    1002

    1004

    1018

    1008

    1010

    1012

    1014

    1016

    1006One contiguous block ofmemory allocated forthe array to store 10

    elements.

    Memory representation

    If you know the address of the first element in the array youcan calculate the address of any other elements as shown:

    Address of the first element + (size of the element index ofthe element)

    Dynamic Memory Allocation

  • 8/2/2019 05 DS and Algorithm Session 07

    7/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    When memory is allocated dynamically, a block of memoryis assigned arbitrarily from any location in the memory.

    Therefore, unlike arrays, these blocks may not becontiguous and may be spread randomly in the memory.

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    1000

    1002

    1004

    1018

    1008

    1010

    1012

    1014

    1016

    1006One contiguous block ofmemory allocated forthe array to store 10

    elements.

    Memory representation

    Dynamic Memory Allocation (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    8/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    Let us see how this happens by allocating memorydynamically for the prime numbers.

    Allocate memory for 2 Memory allocated for 221002

    Memory representation

    Dynamic Memory Allocation (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    9/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    Let us see how this happens.

    Allocate memory for 3 Memory allocated for 32

    3

    1002

    1036

    Memory representation

    Dynamic Memory Allocation (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    10/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    Let us see how this happens.

    Allocate memory for5 Memory allocated for 52

    3

    51008

    1002

    1036

    Memory representation

    Dynamic Memory Allocation (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    11/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    Let us see how this happens.

    Allocate memory for 7 Memory allocated for 72

    3

    5

    71020

    1008

    1002

    1036

    Memory representation

    Dynamic Memory Allocation (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    12/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    Let us see how this happens.

    Allocate memory for 11 Memory allocated for 112

    3

    5

    7

    111030

    1020

    1008

    1002

    1036

    Memory representation

    Dynamic Memory Allocation (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    13/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    Now, if you know the address of the first element, youcannot calculate the address of the rest of the elements.

    This is because, all the elements are spread at randomlocations in the memory.

    2

    3

    5

    7

    111030

    1020

    1008

    1002

    1036

    Memory representation

    To access any element, you need to know its address.

    Dynamic Memory Allocation (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    14/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    Therefore, it would be good if every allocated block ofmemory contains the address of the next block in sequence.

    This gives the list a linked structure where each block islinked to the next block in sequence.

    2

    3

    5

    7

    11

    1036

    1008

    1020

    1030

    1030

    1020

    1008

    1036

    1002

    Memory representation

    Dynamic Memory Allocation (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    15/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    You can declare a variable, START, that stores the addressof the first block.

    You can now begin at START and move through the list byfollowing the links.

    START

    1030

    1020

    1008

    1036

    1002 2

    3

    5

    7

    11

    1036

    1008

    1020

    1030

    Memory representation

    An example of a data structure that implements this conceptis a linked list.

    Dynamic Memory Allocation (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    16/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    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 isreferred to as a node.

    Defining Linked Lists

  • 8/2/2019 05 DS and Algorithm Session 07

    17/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    Defining Linked Lists (Contd.)

    Data Link

    Node

    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

  • 8/2/2019 05 DS and Algorithm Session 07

    18/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    10Data 3352 10Data 5689 10Data 1012 10Data

    2403 3352 5689 1012

    All the nodes in a linked list are present at arbitrary memorylocations.

    Therefore, every node in a linked list has link field thatstores the address of the next node in sequence.

    The last node in a linked list does not point to any othernode. Therefore, it points to NULL.

    NULL

    Defining Linked Lists (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    19/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    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.

    START

    10Data 3352 10Data 5689 10Data 1012 10Data

    2403 3352 5689 1012

    NULL

    Defining Linked Lists (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    20/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    Let us solve the given problem of storing prime numbersusing 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.

    Implementing a Singly-Linked List

  • 8/2/2019 05 DS and Algorithm Session 07

    21/194Ver. 1.0 Session 7

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List

    START

    2 3 5 7

    Consider the following linked list that stores primenumbers.

    When a new prime number is generated, it should beinserted at the end of the list.

    Initially, when the list is empty, START contains NULL.

  • 8/2/2019 05 DS and Algorithm Session 07

    22/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Consider the given algorithm toinsert a node in a linked list.

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    23/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Consider that the list is initiallyempty.START = NULL

    Insert a prime number 2.

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    24/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    START = NULL

    Insert a prime number 2.

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    25/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    START = NULL

    102

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    26/194

  • 8/2/2019 05 DS and Algorithm Session 07

    27/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    START = NULL

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    102

    START

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    28/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    29/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    Insertion complete

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    30/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Insert a prime number 3.

    102

    START

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    31/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Insert a prime number 3.

    102

    START

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    32/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    103

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    33/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    103

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    34/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    103

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    S d l i h

  • 8/2/2019 05 DS and Algorithm Session 07

    35/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    103

    currentNode

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    D S d Al i h

  • 8/2/2019 05 DS and Algorithm Session 07

    36/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    103

    currentNode

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    D t St t d Al ith

  • 8/2/2019 05 DS and Algorithm Session 07

    37/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    103

    currentNode

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    D t St t d Al ith

  • 8/2/2019 05 DS and Algorithm Session 07

    38/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Insertion complete

    102

    START

    103

    currentNode

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    D t St t d Al ith

  • 8/2/2019 05 DS and Algorithm Session 07

    39/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Insert a prime number 5.

    102

    START

    103

    currentNode

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    D t St t d Al ith

  • 8/2/2019 05 DS and Algorithm Session 07

    40/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Insert a prime number 5.

    102

    START

    103

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data St t e a d Al o ith

  • 8/2/2019 05 DS and Algorithm Session 07

    41/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    103 105

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    42/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    103 105

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    43/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    103 105

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    44/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    103 105

    currentNode

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    45/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    103 105

    currentNode

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    46/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    103 105

    currentNode currentNode

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    47/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START

    103 105

    currentNode

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    48/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Insertion complete

    102

    START

    103 105

    currentNode

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    49/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    What is the problem with thisalgorithm?

    Consider a list of 10000 numbers.

    To insert a number at the end ofthe list, you first need to locate the

    last node in the list.To reach the last node, you haveto start from the first node andvisit all the preceding nodesbefore you reach the last node.

    This approach is very timeconsuming for lengthy lists.

    Therefore, a better approach is todeclare a variable, LAST, whichwill store the address of the lastnode in the list.

    Hence, you need to maintain two

    variables, START and LAST, tokeep track of the first and lastnodes in the list respectively.

    If the list is empty, START andLAST point to NULL.

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then:a. Make START point to the new

    node.b. Go to step 6.

    4. Locate the last node in the list, andmark it as currentNode. To locate thelast node in the list, execute thefollowing steps:a. Mark the first node as

    currentNode.b. Repeat step c until the

    successor of currentNodebecomes NULL.

    c. Make currentNode point tothe next node in sequence.

    5. Make the next field of currentNodepoint to the new node.

    6. Make the next field of the new nodepoint to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    50/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Refer to the given algorithm toinsert a node at the end of thelinked list.

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    51/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Consider that the list is initiallyempty.

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    START = NULL

    LAST = NULL

    Insert a prime number 2.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    52/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    START = NULL

    LAST = NULL

    Insert a prime number 2.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    53/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START = NULL

    LAST = NULL

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    54/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START = NULL

    LAST = NULL

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    55/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START = NULL

    LAST = NULL

    START

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    56/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START LAST

    LAST = NULL

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    57/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START LAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    58/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Insertion complete

    102

    START LAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    59/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    Insert a prime number 3.

    102

    START LAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    60/194

    Ver. 1.0 Session 7

    Data Structures and Algorithms

    102

    START LAST

    Insert a prime number 3.

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    61/194

    Ver. 1.0 Session 7

    ata St uctu es a d go t s

    102

    START

    103

    LAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    62/194

    Ver. 1.0 Session 7

    g

    102

    START

    103

    LAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    63/194

    Ver. 1.0 Session 7

    g

    102

    START

    103

    LAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    64/194

    Ver. 1.0 Session 7

    g

    102

    START

    103

    LASTLAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    65/194

    Ver. 1.0 Session 7

    g

    Insertion complete

    102

    START

    103

    LAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    66/194

    Ver. 1.0 Session 7

    g

    Insert a prime number 5.

    102

    START

    103

    LAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    67/194

    Ver. 1.0 Session 7

    g

    102

    START

    103

    LAST

    Insert a prime number 5.

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    68/194

    Ver. 1.0 Session 7

    g

    102

    START

    103 105

    LAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    69/194

    Ver. 1.0 Session 7

    102

    START

    103 105

    LAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    70/194

    Ver. 1.0 Session 7

    102

    START

    103 105

    LAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    71/194

    Ver. 1.0 Session 7

    102

    START

    103 105

    LASTLAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    72/194

    Ver. 1.0 Session 7

    Insertion complete

    102

    START

    103 105

    LAST

    1. Allocate memory for the new node.

    2. Assign value to the data field of thenew node.

    3. If START is NULL, then (If the list isempty):a. Make START point to the new

    node.b. Make LAST point to the new

    node.c. Go to step 6.

    4. Make the next field of LAST point tothe new node.

    5. Mark the new node as LAST.

    6. Make the next field of the new node

    point to NULL.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    73/194

    Ver. 1.0 Session 7

    Now consider another problem.Suppose you have to store the marks of 20 students in theascending order.

    How will you solve this problem?

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    74/194

    Ver. 1.0 Session 7

    Consider the following algorithm, which uses an array tosolve this problem.

    1. Set N = 0 // Number of marks entered

    2. Repeat until N = 20

    a. Accept marks.

    b. Locate position I where the marks must be inserted.

    c. For J = N 1 down to I

    Move A[J] to A[J+1] // Move elements to make

    // place for the new element

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    75/194

    Ver. 1.0 Session 7

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks.

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Let us perform some insertoperations in the array by placing theelements at their appropriatepositions to get a sorted list.

    0 1 2 3 4 19

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    76/194

    Ver. 1.0 Session 7

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    10

    0 1 2 3 4 19

    Let us perform some insert operationsin the array by placing the elements attheir appropriate positions to get asorted list.

    N = 0

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    77/194

    Ver. 1.0 Session 7

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    10

    0 1 2 3 4 19

    N = 0

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    78/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 0

    marks = 10

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    79/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 0

    marks = 10

    I

    I = 0

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    80/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 0

    marks = 10

    I = 0

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    81/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 0

    marks = 10

    I = 0

    10

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    82/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 0

    I = 0

    10

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    83/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 1

    I = 0

    10

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    84/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 1

    10

    I

    I = 0

    marks = 20

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    85/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 1

    marks = 20

    I = 1

    10

    II

    I = 0

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    86/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 1

    marks = 20

    I = 1

    10

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    87/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 1

    marks = 20

    I = 1

    10 20

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    88/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 1

    I = 1

    10 20

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    89/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 2

    I = 1

    10 20

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    90/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 2

    marks = 17

    10 20

    I

    I = 1

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    91/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 2

    marks = 17

    I = 1

    10 20

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    92/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 2

    marks = 17

    I = 1

    10 20

    J = 1

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    93/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 2

    marks = 17

    I = 1

    10 20

    J = 1

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    94/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 2

    marks = 17

    I = 1

    10 20

    J = 1

    20

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    95/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 2

    marks = 17

    I = 1

    10

    J = 1

    2017

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 05 DS and Algorithm Session 07

    96/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 3

    I = 1

    10 2017

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    i d i i l i k d i ( d )

  • 8/2/2019 05 DS and Algorithm Session 07

    97/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 3

    marks = 15

    10 2017

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    I i N d i Si l Li k d Li (C d )

  • 8/2/2019 05 DS and Algorithm Session 07

    98/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 3

    marks = 15

    I = 1

    10 2017

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    I ti N d i Si l Li k d Li t (C td )

  • 8/2/2019 05 DS and Algorithm Session 07

    99/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 3

    marks = 15

    I = 1

    10

    J = 2

    2017

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    I ti N d i Si l Li k d Li t (C td )

  • 8/2/2019 05 DS and Algorithm Session 07

    100/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 3

    marks = 15

    I = 1

    10

    J = 2

    2017

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    I ti N d i Si l Li k d Li t (C td )

  • 8/2/2019 05 DS and Algorithm Session 07

    101/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 3

    marks = 15

    I = 1

    10

    J = 2

    2017 20

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    I ti N d i Si l Li k d Li t (C td )

  • 8/2/2019 05 DS and Algorithm Session 07

    102/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 3

    marks = 15

    I = 1

    10

    J = 1

    17 20

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    Inse ting a Node in a Singl Linked List (Contd )

  • 8/2/2019 05 DS and Algorithm Session 07

    103/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 3

    marks = 15

    I = 1

    10

    J = 1

    17 20

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly Linked List (Contd )

  • 8/2/2019 05 DS and Algorithm Session 07

    104/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 3

    marks = 15

    I = 1

    10

    J = 1

    17 2017

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly Linked List (Contd )

  • 8/2/2019 05 DS and Algorithm Session 07

    105/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 3

    marks = 15

    I = 1

    10

    J = 1

    201715

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly Linked List (Contd )

  • 8/2/2019 05 DS and Algorithm Session 07

    106/194

    Ver. 1.0 Session 7

    10

    0 1 2 3 4 19

    N = 4

    I = 1

    10

    J = 1

    201715

    I

    1. Set N = 0

    2. Repeat until N = 20a. Accept marks

    b. Locate position I wherethe marks must beinserted

    c. For J = N-1 down to I

    Move A[J] to A[J+1]

    d. Set A[I] = marks

    e. N = N + 1

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly Linked List (Contd )

  • 8/2/2019 05 DS and Algorithm Session 07

    107/194

    Ver. 1.0 Session 7

    What is the problem in this algorithm?To insert an element at any position other than the end of thelist, there is an additional overhead of shifting all succeedingelements one position forward.

    Similarly, to delete an element at any position other than the

    end of the list, you need to shift the succeeding elements oneposition backwards.

    When the list is very large, this would be very time consuming.

    From the preceding example we conclude that insertion anddeletion at any position other than the end of an array iscomplex and inefficient.

    How can you overcome this limitation?

    By using a data structure that does not require shifting of data

    elements after every insert / delete operation.A linked list offers this flexibility.

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd )

  • 8/2/2019 05 DS and Algorithm Session 07

    108/194

    Ver. 1.0 Session 7

    Consider a linked list that stores the marks of students in anascending order.

    Insert marks (17).

    17 should be inserted after 15.

    START

    20

    10

    25

    15

    Memory representation

    1002

    1020

    2496

    1011

    15 1002 20 102010 2496 25 NULL

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd )

  • 8/2/2019 05 DS and Algorithm Session 07

    109/194

    Ver. 1.0 Session 7

    Allocate memory for 17.

    10

    START

    10 2496 1015 1002 1020 1020 1025

    20

    10

    25

    15

    Memory representation

    17Memory allocated for 17

    1002

    1020

    2496

    1011

    1008

    NULL

    17

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd )

  • 8/2/2019 05 DS and Algorithm Session 07

    110/194

    Ver. 1.0 Session 7

    In the given list, node 15 contains the address of node 20.To add node 17 after node 15, you need to update theaddress field of node 15 so that it now contains the addressof node 17.

    20

    10

    25

    15

    Memory representation

    17

    17

    10

    START

    10 2496 1015 1002 1020 1020 1025 NULL

    1002

    1020

    2496

    1011

    1008Memory allocated for 17

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd )

  • 8/2/2019 05 DS and Algorithm Session 07

    111/194

    Ver. 1.0 Session 7

    Update the address field of node 15 to store the address ofnode 17.

    20

    10

    25

    15

    Memory representation

    17

    1011

    1020

    2496

    1008

    1002

    17

    10

    START

    10 2496 1015 1002 1020 1020 1025 NULL17

    Inserting a Node in a Singly-Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd )

  • 8/2/2019 05 DS and Algorithm Session 07

    112/194

    Ver. 1.0 Session 7

    Update the address field of node 15 to store the address ofnode 17.

    20

    10

    25

    15

    Memory representation

    17

    1011

    1020

    2496

    1008

    1002

    10

    START

    10 2496 1015 1002 1020 1020 1025 NULL17

    Updating the address field

    1008

    Inserting a Node in a Singly Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd )

  • 8/2/2019 05 DS and Algorithm Session 07

    113/194

    Ver. 1.0 Session 7

    20

    10

    25

    15

    Memory representation

    17

    1011

    1020

    2496

    1008

    1002

    10

    START

    10 2496 15 1020 1020 1025 NULL17

    1008

    Store the address of node 20 in the address field of node 17to make a complete list.

    Address updated

    1002

    Node Inserted

    Inserting a Node in a Singly Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    114/194

    Ver. 1.0 Session 7

    Now, let us solve the given problem using a linked list.Suppose the linked list currently contains the followingelements.

    The linked list needs to be created in the ascending order of

    values.Therefore, the position of a new node in the list will bedetermined by the value contained in the new node.

    START

    10 15 17 20

    Inserting a Node in a Singly Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    115/194

    Ver. 1.0 Session 7

    Write an algorithm to locate the position of the new node tobe inserted in a linked list, where the nodes need to bestored in the increasing order of the values contained inthem.

    Inserting a Node in a Singly Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    116/194

    Ver. 1.0 Session 7

    Algorithm for locating the positionof a new node in the linked list.

    After executing this algorithm thevariables/pointers previous andcurrent will be placed on the

    nodes between which the newnode is to be inserted.

    1. Make current point to the first node.

    2. Make previous point to NULL.

    3. Repeat step 4 and step 5 untilcurrent.info becomes greater than thenewnode.info or current becomesequal to NULL.

    4. Make previous point to current.

    5. Make current point to the next node in

    sequence.

    Inserting a Node in a Singly Linked List (Contd.)

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    117/194

    Ver. 1.0 Session 7

    START

    15 17 20

    Insert 16 in the given list.

    Inserting a Node in a Singly Linked List (Contd.)

    1. Make current point to the first node.

    2. Make previous point to NULL.

    3. Repeat step 4 and step 5 untilcurrent.info becomes greater than thenewnode.info or current becomesequal to NULL.

    4. Make previous point to current.

    5. Make current point to the next node in

    sequence.

    10

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    118/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    Insert 16 in the given list.

    Inserting a Node in a Singly Linked List (Contd.)

    1. Make current point to the first node.

    2. Make previous point to NULL.

    3. Repeat step 4 and step 5 untilcurrent.info becomes greater than thenewnode.info or current becomesequal to NULL.

    4. Make previous point to current.

    5. Make current point to the next node in

    sequence.

    current

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    119/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    previous = NULL

    Inserting a Node in a Singly Linked List (Contd.)

    1. Make current point to the first node.

    2. Make previous point to NULL.

    3. Repeat step 4 and step 5 untilcurrent.info becomes greater than thenewnode.info or current becomesequal to NULL.

    4. Make previous point to current.

    5. Make current point to the next node in

    sequence.

    current

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    120/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    previous = NULL

    g g y ( )

    1. Make current point to the first node.

    2. Make previous point to NULL.

    3. Repeat step 4 and step 5 untilcurrent.info becomes greater than thenewnode.info or current becomesequal to NULL.

    4. Make previous point to current.

    5. Make current point to the next node in

    sequence.

    current

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    121/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    currentprevious

    g g y ( )

    1. Make current point to the first node.

    2. Make previous point to NULL.

    3. Repeat step 4 and step 5 untilcurrent.info becomes greater than thenewnode.info or current becomesequal to NULL.

    4. Make previous point to current.

    5. Make current point to the next node in

    sequence.

    previous = NULL

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    122/194

    Ver. 1.0 Session 7

    current

    g g y ( )

    10

    START

    10 15 17 20

    current

    1. Make current point to the first node.

    2. Make previous point to NULL.

    3. Repeat step 4 and step 5 untilcurrent.info becomes greater than thenewnode.info or current becomesequal to NULL.

    4. Make previous point to current.

    5. Make current point to the next node in

    sequence.

    previous

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    123/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    current

    g g y ( )

    1. Make current point to the first node.

    2. Make previous point to NULL.

    3. Repeat step 4 and step 5 untilcurrent.info becomes greater than thenewnode.info or current becomesequal to NULL.

    4. Make previous point to current.

    5. Make current point to the next node in

    sequence.

    previous

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    124/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    previous current

    g g y ( )

    1. Make current point to the first node.

    2. Make previous point to NULL.

    3. Repeat step 4 and step 5 untilcurrent.info becomes greater than thenewnode.info or current becomesequal to NULL.

    4. Make previous point to current.

    5. Make current point to the next node in

    sequence.

    previous

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    125/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    current

    g g y ( )

    1. Make current point to the first node.

    2. Make previous point to NULL.

    3. Repeat step 4 and step 5 untilcurrent.info becomes greater than thenewnode.info or current becomesequal to NULL.

    4. Make previous point to current.

    5. Make current point to the next node in

    sequence.

    currentprevious

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    126/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    current

    Node located

    1. Make current point to the first node.

    2. Make previous point to NULL.

    3. Repeat step 4 and step 5 untilcurrent.info becomes greater than thenewnode.info or current becomesequal to NULL.

    4. Make previous point to current.

    5. Make current point to the next node in

    sequence.

    previous

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    127/194

    Ver. 1.0 Session 7

    Once the position of the new node has been determined,the new node can be inserted in the linked list.

    A new node can be inserted at any of the following positionsin the list:

    Beginning of the list

    End of the listBetween two nodes in the list

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    128/194

    Ver. 1.0 Session 7

    Write an algorithm to insert a node in the beginning of alinked list.

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    129/194

    Ver. 1.0 Session 7

    START

    15 17 20

    1. Allocate memory for the newnode.

    2. Assign value to the data field ofthe new node.

    3. Make the next field of the newnode point to the first node in thelist.

    4. Make START, point to the new

    node.

    Algorithm to insert a node in thebeginning of a linked list

    10

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    130/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    newnode

    1. Allocate memory for the newnode.

    2. Assign value to the data field ofthe new node.

    3. Make the next field of the newnode point to the first node in thelist.

    4. Make START, point to the new

    node.

    Insert 7

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    131/194

    Ver. 1.0 Session 7

    10

    START

    10 15 20

    newnode

    7

    1. Allocate memory for the newnode.

    2. Assign value to the data field ofthe new node.

    3. Make the next field of the newnode point to the first node in thelist.

    4. Make START, point to the new

    node.

    17

    Insert 7

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    132/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    newnode

    7

    newnode. next = START

    1. Allocate memory for the newnode.

    2. Assign value to the data field ofthe new node.

    3. Make the next field of the newnode point to the first node in thelist.

    4. Make START, point to the new

    node.

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    133/194

    Ver. 1.0 Session 7

    1. Allocate memory for the newnode.

    2. Assign value to the data field ofthe new node.

    3. Make the next field of the newnode point to the first node in thelist.

    4. Make START, point to the new

    node.

    START = newnode

    newnode. next = START

    Insertion complete

    10

    START

    10 15 17 20

    newnode

    7

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    134/194

    Ver. 1.0 Session 7

    Write an algorithm to insert a node between two nodes in alinked list.

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    135/194

    Ver. 1.0 Session 7

    Algorithm to insert a node betweentwo nodes in a linked list.

    1. Identify the nodes between which thenew node is to be inserted. Mark them

    as previous and current. To locateprevious and current, execute thefollowing steps:a. Make current point to the first

    node.b. Make previous point to NULL.c. Repeat step d and step e until

    current.info becomes greaterthan newnode.info or current

    becomes equal to NULL.d. Make previous point to current.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 newnode.

    4. Make the next field of the new nodepoint to current.

    5. Make the next field of previous point tothe new node.

    Insert 16

    START

    15 17 2010

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    136/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    Insert 16

    1. Identify the nodes between which thenew node is to be inserted. Mark them

    as previous and current. To locateprevious and current, execute thefollowing steps:a. Make current point to the first

    node.b. Make previous point to NULL.c. Repeat step d and step e until

    current.info becomes greaterthan newnode.info or current

    becomes equal to NULL.d. Make previous point to current.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 newnode.

    4. Make the next field of the new nodepoint to current.

    5. Make the next field of previous point tothe new node.

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    137/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    current

    Insert 16

    1. Identify the nodes between which thenew node is to be inserted. Mark them

    as previous and current. To locateprevious and current, execute thefollowing steps:a. Make current point to the first

    node.b. Make previous point to NULL.c. Repeat step d and step e until

    current.info becomes greaterthan newnode.info or current

    becomes equal to NULL.d. Make previous point to current.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 newnode.

    4. Make the next field of the new nodepoint to current.

    5. Make the next field of previous point tothe new node.

  • 8/2/2019 05 DS and Algorithm Session 07

    138/194

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    139/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    current

    previous = NULL

    Insert 16

    1. Identify the nodes between which thenew node is to be inserted. Mark them

    as previous and current. To locateprevious and current, execute thefollowing steps:a. Make current point to the first

    node.b. Make previous point to NULL.c. Repeat step d and step e until

    current.info becomes greaterthan newnode.info or current

    becomes equal to NULL.d. Make previous point to current.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 newnode.

    4. Make the next field of the new nodepoint to current.

    5. Make the next field of previous point tothe new node.

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    140/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    current

    previous = NULL

    previous

    Insert 16

    1. Identify the nodes between which thenew node is to be inserted. Mark them

    as previous and current. To locateprevious and current, execute thefollowing steps:a. Make current point to the first

    node.b. Make previous point to NULL.c. Repeat step d and step e until

    current.info becomes greaterthan newnode.info or current

    becomes equal to NULL.d. Make previous point to current.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 newnode.

    4. Make the next field of the new nodepoint to current.

    5. Make the next field of previous point tothe new node.

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    141/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    current current

    Insert 16

    1. Identify the nodes between which thenew node is to be inserted. Mark them

    as previous and current. To locateprevious and current, execute thefollowing steps:a. Make current point to the first

    node.b. Make previous point to NULL.c. Repeat step d and step e until

    current.info becomes greaterthan newnode.info or current

    becomes equal to NULL.d. Make previous point to current.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 newnode.

    4. Make the next field of the new nodepoint to current.

    5. Make the next field of previous point tothe new node.

    previous

    Data Structures and Algorithms

    Inserting a Node in a Singly-Linked List (Contd.)

  • 8/2/2019 05 DS and Algorithm Session 07

    142/194

    Ver. 1.0 Session 7

    10

    START

    10 15 17 20

    current

    Insert 16

    1. Identify the nodes between which thenew node is to be inserted. Mark them

    as prev