Linked List tutorials

Embed Size (px)

Citation preview

  • 8/8/2019 Linked List tutorials

    1/25

    Linked list

    Array is not always good choice for data storage though all the usual operations , i.e.deletion , insertion , updation are performed easily on arrays.

    Specifically they are not suited in following conditions:(i) Unpredictable storage requirement(ii) Extensive manipulation of stored data (insertion / deletion)

    Array Linked list

    1. Insertion/ deletion at a specificprint in array requires moving of

    other elements.

    2. Fixed size is a limitation. It acts ascap on data storage

    3. Allows constant-time randomaccess to the data

    4. There are no overhead ofreferences.

    1. Insertion/ deletion at a specificprint of a list is a constant-time

    operation.

    2. There is no cap on the datastorage

    3. Sequential access to theelements.

    4. Requirement of extra storage forreferences.

    What is a linked list ?

    A Linked list is a collection of nodes. Each node consists of two parts:(i) Information(ii) Pointer to next node reference

    Information (address of next node or NULL)

    A linked list also contains a list pointer variable called start which contains the addressof first node in the list.

    Building a linked list

    (1)Declare the structure that define the list entriesStruct list

    {

    Int data ; /* information * /

    Struct list *next; /* reference */

    };

  • 8/8/2019 Linked List tutorials

    2/25

    A simple example of linked list

    # include

    #includemain()

    {

    struct list

    {

    int data;

    struct list * next;

    };

    struct list * start, *p, *r;

    start = (struct list *) malloc ( sizeof (struct list)); start

    start->data=15;q=(struct list *) malloc (sizeof( struct list));

    q->data =18; start

    q->next =NULL;

    start->next =q;

    r = p; q

    while (r!=NULL) data= 15

    { dat = 18

    printf(Data = %d\n,r->data);

    r=r->next;

    }

    }

    General Algorithm to create linked list

    1. Start =NULL /* initially the linked list is empty */2. Ptr1 = create (node) /* create first node */

    node.info = info

    node . next = NULL

    3. Start = ptr14. While(condition = true) /* add other node */

    Ptr2 = create (node)

    Node.info = info

    Node.next = NULL

    Ptr1.next = ptr2;

    Ptr1 = ptr2;

    5. Exit

    15

    15 18 NULL

  • 8/8/2019 Linked List tutorials

    3/25

    main()

    {

    struct list * start;

    - - - - - - - - - - - -

    create_list(start);

    - - - - - - - - - - - -}

    void create_list (struct list * start)

    {

    struct list * ptr1 , + ptr2;

    char ch;

    int i = 1, data ;

    ptr1 = (struct list *) malloc (sizeof (struct list));

    printf (\n Enter data of node %d , i);

    i++;scanf(%d, &data);

    ptr1 -> data = data;

    ptr1->next =NULL;

    start = ptr1;

    printf(\n Enter n to stop);

    ch = getch();

    while (ch != n)

    {

    ptr2 = (struct list *) malloc (sizeof (struct list));

    printf(\n Enter the data of node %d, i);i++;

    scanf(%d,&data);

    ptr2-> data =data;

    ptr2-> next= NULL;

    ptr1-> next = ptr2;

    ptr1 = ptr2;

    printf(\n Enter n to stop);

    ch = getch();

    }

    }

  • 8/8/2019 Linked List tutorials

    4/25

    Traversing a linked list

    1 . Current = start /*initialize current pointer*/

    2. Repeat while (current!= NULL) /*Traverse whole list*/

    Process node

    Current = current + next /*move pointer*/

    3. Exit

    void traverse _l list (struct list * start)

    {

    int i = 1;

    struct list * current;

    current = start ;while (current != NULL)

    {

    printf(\n Node %d data = %d, i , current-> data);

    i++;

    current = current-> next;

    }

    }

    Insertion

    Insert as a first node Insert as a last node Insert in-between the linked list

    Insert as a first node

    Create node (1)

    (1). Create new node.

    (2). New node will point to first node.(3). Start will point to new node.

    NULL

  • 8/8/2019 Linked List tutorials

    5/25

    main()

    {

    struct list * start;

    - - - - - - - -insert_fnode (start);

    }

    void insert_fnode (struct list * start)

    {

    struct list *ptr;

    int data;

    ptr = (struct node *) malloc (sizeof (struct list));

    printf(\n Enter data for new node);

    scanf(%d, &data);

    ptr-> data = data

    ptr-> next =start;start = ptr;

    }

    Insert as a last node

    start

    `

    Main()

    {

    Struct list * start;

    - - - - - -Insert_lnode(start)

    }

    NULL

    NULL

  • 8/8/2019 Linked List tutorials

    6/25

    void insert_lnode (start list * start)

    {

    struct list * ptr1, *ptr2;

    int data;

    if ( start == NULL)

    {

    start = (struct list *) malloc (sizeof (struct list));

    print(\n Enter data for new node);

    scanf(%d, & data);

    start-> data = data;

    start-> next = NULL;

    }

    else

    {

    ptr2 = start;

    while (ptr2-> next != NULL)ptr2 = ptr2-> next;

    ptr1 = (struct list *)malloc (sizeof(struct list *));

    printf(\n Enter data for new node);

    scanf(%d, &data);

    ptr1-> next = NULL;

    ptr2-> next = ptr1;

    }

    void traverse _l list (struct list * start)

    {

    int i = 1;struct list * current;

    current = start ;

    while (current != NULL)

    {

    printf(\n Node %d data = %d, i , current-> data);

    i++;

    current = current-> next;

    }

    }

  • 8/8/2019 Linked List tutorials

    7/25

    Insert in-between the linked list

    start

    Main()

    {

    Int at_number;

    Struct list * start;

    . . . . . . . . . . . . . . .

    Insert_bnode (start , at_number);

    . . . . . . . . . . . . .. . . . . .. . . ....

    }

    void insert_bnode (struct list * start , int number)

    {

    struct list * ptr1, *ptr2;

    int i, data;

    ptr2 = start;

    for (i=1;inext;

    ptr1 = (struct list *) malloc (sizeof (struct list));printf(\n Enter data for new node);

    scanf(%d, &data);

    ptr1-> data = data;

    ptr1-> next = ptr2-> next;

    ptr2-> next = ptr1;

    }

    NULL

  • 8/8/2019 Linked List tutorials

    8/25

    Deletion

    Delete first node Delete last node Delete in-between node

    Delete first node

    If list is empty

    Do nothing

    Else

    Make the start

    Pointer to point to second node.

    main()

    {

    struct list * start;

    . . . . . .. . . .. . . . . .

    delete_lnode (start)

    . . . . . . . . . . . . . . . . .

    }

    void delete_lnode (struct list * start)

    {

    struct list * ptr1, ptr2;

    if (start == NULL)

    {

    printf(\n there is no node to delete the list is empty);

    return;

    }

    if(start-> next == NULL)

    {

    ptr1 = start;

    start= NULL;

    fflush (ptr1);

    }

    }

    NULL

  • 8/8/2019 Linked List tutorials

    9/25

    Delede last node

    start

    If list is empty

    Do nothing

    Else make the next pointer of second-last to poin to NULL

    main()

    {

    struct list * start;

    . . . . . . . . .. . .. . . .

    delete_ldalete (start);

    . . . .. . .. . .. . . . . . . . . ..

    }

    void() delete_lnode (struct list * start)

    {

    struct list *ptr1, *ptr2;

    if (start == NULL)

    {

    printf(\n there is no node to delete . The is empty);

    return;

    }

    if (start->next == NULL)

    {

    ptr1 = start;

    start = NULL; ptr1

    fflush (ptr1);

    NULL NULL

    start

    NULL

  • 8/8/2019 Linked List tutorials

    10/25

    else

    {

    Ptr1 =start ;

    Ptr2 = ptr1->next;

    While (ptr2-> next != NULL) ptr1 ptr2{

    ptr1 = ptr1->next;

    ptr2 = ptr2->next;

    } /*end while*/

    Ptr1-> next =NULL;

    Fflush (ptr2);

    } /*end else*/

    }

    Another variation of the delete_lnode()

    void Delete_lnode (struct list * start)

    {

    struct list *ptr;

    if (start == NULL)

    {

    printf(\n There is no node to delete. The is empty);

    return;

    }

    if (start-> next == NULL)

    {

    fflush (start);

    start = NULL;

    }

    else

    {

    ptr = start;

    while (ptr-> next->next !=NULL)ptr = ptr-> next;

    fflush (ptr -> next);

    ptr-> next =NULL;

    }

    }

    start

  • 8/8/2019 Linked List tutorials

    11/25

    Delete in_between node

    (1)

    (1)Let node represents the node to deleted.make the next pointer of the previous (node)in the list.

    main ()

    {

    int node _num;

    struct list *start;

    . . . . .. . . . . . . . . .

    delete_bnode (stsrt , node_num)

    . . .. . . . . . . .. . . . . . . . . . .. . . . . . . .

    }

    void delete_bnode (struct list * start , int number)

    {

    int i;

    struct list *ptr1, *ptr2;

    ptr1 = start;

    ptr2 = ptr1->next;

    for(i=2 ; i next;ptr2 = ptr2 -> next;

    }

    ptr1-> next = ptr2 ->next;

    fflush(ptr2);

    }

    ]

    NULL NULL

  • 8/8/2019 Linked List tutorials

    12/25

    Searching a linked list

    (i)Whether information is present in the list ?(ii)At which location or how many location ?

    1. Current = start /*initialize current poiner*/2. Set counter = start /*increment poinetr*/3. Repeat while (current !=NULL) / *find information*/

    If ( information is found)

    Print location and

    Exit

    Else

    Counter ++ / * increment counter* /

    Counter = current -> next /* increment pointer*/

    4. Information isa not present ,5. Exit

    main()

    {

    int info;

    struct list * start;

    . . .. . . . . . . . . . . . . . . .

    search_llist (start , info)

    . . . . . . . . . . . . . . . . . . . . . . . . .

    }

    void search_llist (struct list *start , int info)

    {

    int location;

    struct list * curret;

    location =1;

    current start;

    while (current !=NULL)

    {

    if (current -> data == info)

    {

    P[rintf(\n The information %d is present at node %d, info, location);

    return;

    }

    }}

  • 8/8/2019 Linked List tutorials

    13/25

    main()

    {

    struct list *start;

    delete_fnode(start);

    }

    void delete_fnode(struct list *start)

    {

    struct list *ptr;

    if(start==NULL)

    printf(\n There is no node to delete.The list is empty);

    else

    {

    ptr=start;

    start=start->next;

    fflush(ptr);

    return;}

    }

    Delete Last node

    if list is empty

    do nothing

    else make the next pointer

    of second_last to point

    to NULL

    main()

    {

    struct list *start;

    ----------------------

    delete_lnode (start);

    -------------------------

    }

    void delete_lnode(struct list *start)

    {

    struct list *ptr1,*ptr2;

    if(start==NULL)

    {

    printf(\n There is no node to delete. The list is empty);

    return;

    }

  • 8/8/2019 Linked List tutorials

    14/25

    if (start->next==NULL)

    {

    ptr1=start;

    start=NULL;

    fflush(ptrr1);

    }

    else ptr1 ptr2

    {

    ptr1=start;

    ptr2=ptr1->next;

    while(ptr2->next !=NULL)

    {

    ptr1=ptr1->next;

    ptr2=ptr2->next;

    }

    ptr1->next=NULL;

    fflush(ptr2);}

    }

    Another variation of the Delete_lnode()

    Void delete_lnode(struct list *start)

    {struct list *ptr;

    if(start==NULL)

    {

    printf(\n There is no node to delete. The list is empty);

    return;

    }

    if (start->next==NULL)

    {

    fflush(start);

    start=NULL;}

    else

    {

    ptr1=start;

    while(ptr->next ->next!=NULL)

    {

    ptr=ptr->next;fflush(ptr->next);

    ptr->next=NULL;

    }

    }

  • 8/8/2019 Linked List tutorials

    15/25

  • 8/8/2019 Linked List tutorials

    16/25

  • 8/8/2019 Linked List tutorials

    17/25

    printf(the information %d is not present in the linked list , info);

    }/*end function*/

    Merging two linked list

    LIST 1

    Start

    start List2

    start

    Final merged list

    1. Move the last node of list1 /*list2 is to be merged to the list1 */2. Make this node to point to first node of the list1.3. Exit.main()

    {

    struct list *start1,*start2;

    -----------------------------------

    merge_lists(start1,start2);

    -----------------------------------

    }

    Void merge_lists(struct list *start1,struct list *start2)

    {

    struct list *current;

    if(start1==NULL) list1 list2

    { 0 0

    start1=start2; 0 1

    return; 1 0

    } 1 1

    if(start2==NULL)

    return;

    current=start1;

  • 8/8/2019 Linked List tutorials

    18/25

    while(current>next!=NULL)

    current=current->next;

    current->next=start2;

    }

    Doubly Linked List

    Left link Right Link

    Information Field

    Declaration

    struct list

    {int data;

    struct dlist *left;

    struct dlist *right;

    }

    Struct doublelist

    {

    struct dlist *fnode;

    struct dlist *lnode;

    }

    Create a double linked list

    Main()

    {

    struct doublelist dblist;

    ------------------------------

    create_dlist(dblist);

    ------------------------------

    }

    Void create_dlist(struct doublelist dblist)

    {

    struct dlist *ptr; /*creating first node*/

    ptr=(struct dlist *)malloc(sizeof(struct dlist));

    ptr->right=NULL; ptr

    ptr->left=NULL;

    dblist->lnode=ptr; lnode fnode

    dblist->fnode=ptr;

  • 8/8/2019 Linked List tutorials

    19/25

    while(condition is true)

    {

    ptr=(struct dlist*) malloc(sizeof(structdlist));

    ptr->right=Null;

    ptr->left=dblist->lnode;

    ptr->left->right=ptr; lnode

    or fnode

    dblist->lnode->right=ptr;

    dblist->lnode=ptr;

    }

    } ptr

    Traverse a linked list

    Main()

    {

    struct doublelist dblist;

    ------------------------------

    traverse_dlist(dblist);

    ------------------------------

    }

    Void traverse_dlist(struct doublelist dblist)

    {

    Struct dblist *ptr;

    ptr=dblist->fnode;

    while(ptr!=NULL)

    {

    process the node;

    ptr=ptr->right;}

    }

    Insert a new node in the doubly linked list

    1. Insert before first node.2. Insert after a last node trivial3. Insert in between nodes.

  • 8/8/2019 Linked List tutorials

    20/25

    Insert in between nodes

    Main()

    {

    struct doublelist dblist;------------------------------

    traverse_dlist(dblist);

    ------------------------------

    }

    Void insert_bdlist(struct doublelist dblist)

    { temptr

    Struct dlist *ptr,*temptr;

    Ptr=dblist->fnode; (3)

    Move the pointer upto node (a) (4) (1) (2)

    The new node is to be inserted between

    node (a) and node(b)

    Temptr=(struct dlist*)malloc(sizeof(struct dlist)); (a) (b)

    Temptr->right=ptr->right; (1) ptr

    Ptr->right->left=temptr; (2)

    Temptr->left=ptr; (3)

    Ptr->right=temptr; (4)

    }

    Deletion

    Delete the leftmost(first) node Delete the rightmost (last) node Delete a node in between the list

    Leftmost (first) node

    Fnode lnode

    (Before)

    (a) (b) (c)

  • 8/8/2019 Linked List tutorials

    21/25

    Fnode lnode

    (After)

    (b) (c)

    1. Make the fnode to point to node (b) of node (a)2. Make the left pointer to NULL3. Do garbage collection for node(a)

    Rightmost (last) node

    Fnode lnode

    (Before)

    (a) (b) (c)

    Fnode lnode

    (After)

    (a) (b)

    1. Move to the node (b)2. Make the lnode to point to node (b)3. Make the right pointer of node (b) to null4. Do garbage collection for node(c)

    Delete in-between node

    (2)

    (4) (a) (c)

    (a) (b) (c)

    ptr (3)

    (Before) (After)

    1. Move to node (a)2. Make right pointer of node (a) to point to node (c)3. Make left pointer of node (c) to point to node (a)4.

    Do garbage collection for node (b).

  • 8/8/2019 Linked List tutorials

    22/25

    Main()

    {

    struct doublelist dblist;

    -------------------------------

    delete dblist(dblist);-----------------------------

    }

    Void delete_dblist(struct doublelist dblist)

    {

    struct dlist *ptr;

    ptr=dblist->fnode;

    (let node (b) is to be deleted)

    move the pte upto node (a)ptr->right=ptr->right->right; (2)

    fflush(ptr->right->left); (4)

    ptr->left->right=ptr; (3)

    }

    Merging two linked list (3)

    fnode1 lnode1 (1) fnode2 lnode2

    (a) (b) (c) (2) (d) (e) (f)

    Before merging

    fnode1 lnode1

    (a) (b) (c) (d) (e) (f)

    After Merging:Merged List

    Lnode1->right=fnode2; (1)

    fnode2->left=lnode1; (2)lnode1=lnode2; (3)

  • 8/8/2019 Linked List tutorials

    23/25

    Header Linked List

    A header node is a special node which is found in front of the list. A list which contains

    this type of node is called header linked list.

    Struct list Strruct head

    { {char f_name[25]; int num_node;

    char l_name[25] struct list *next;

    float cgpa; }

    struct list *next;

    }

    node of a list header of a node

    Header node list nodes

    Addition of polynomials

    F1=5x5-3x

    3+2x

    2+x

    1+10x

    0

    Struct polynomial struct head_poly

    { {

    int coefficient; int num_terms;

    int exponent; struct polynomial *next;

    struct polynomial *next; }

    }

    start

    F2=6x6+4x

    4+2x

    2-x

    start

    p(x)=F1(x)+F2(x)

    =6x6

    +5x5

    +4x4

    -3x3

    +4x2

    +10x0

    3

    5 5 5 -3 3 2 2 1 1 10 0

    4 6 6 4 4 2 2 -1 1

  • 8/8/2019 Linked List tutorials

    24/25

    start

    #define NUMNODES 500

    Struct nodetype

    {

    int info;

    int next;

    };

    Struct node type node[NUMNODES];

    ------------------------------------------------

    Avail=0;

    For(i=0;i

  • 8/8/2019 Linked List tutorials

    25/25

    Void delafter(intp,int *px)

    {

    int q;

    if(p===-1)!!(node[p].next==-1))

    {

    printf(void deletion);

    return;

    q=node[p].next;

    *px=node[q].info;

    node[p].next=node[q].next;

    freenode(q);

    return;

    }/*end delafter*/

    }