p12 linked list

Upload: abhishek-ek

Post on 06-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 p12 Linked List

    1/18

  • 8/3/2019 p12 Linked List

    2/18

    Structures Containing PointersStructures Containing Pointers

    main()

    {

    structint int_pointers

    {

    int *p1;

    int *p2;

    };struct int _pointers pointers;

    int i1 =100, i2;

  • 8/3/2019 p12 Linked List

    3/18

    pointers.p1 = &i1;

    pointers.p2 = &i2;

    *pointers.p2 = -97;printf(i1 =%d,*pointers.p1=%d\n,i1,*pointers.p1)

    printf(i2 =%d,*pointers.p2=%d\n,i2,*pointers.p2)

    }Output:

    i1 = 100, *pointers.p1 = 100

    I2 = -97, *pointers.p2 = -97

  • 8/3/2019 p12 Linked List

    4/18

    LINKED LISTLINKED LIST

    Linear collection of data elements that

    stores a group of values of the same data

    type. Dynamic data structures - The successive

    elements are not stored at contiguous

    memory location.

    Stored at a location, wherever they find

    the free memory space.

  • 8/3/2019 p12 Linked List

    5/18

    LINKED LISTLINKED LIST

    It is a collection of components, callednodes.

    Every node contains the address of the

    next node. Every node in a linked list has two

    components:

    One to store relevant information (data)One to store the address, called link

    data link Structure of a node

  • 8/3/2019 p12 Linked List

    6/18

    LINKED LISTLINKED LIST

    A list of items, called nodes, in which theorder of the nodes is determined by the

    address, called the link, stored in each

    node.45 65 34 76

    head

    Down arrow in the last node indicates that this link field is

    NULL.

    Arrow in each indicates that the address of the node to

    which it is pointing is stored in that node.

  • 8/3/2019 p12 Linked List

    7/18

    Linked listsLinked lists

    main(){

    struct entry

    {

    int value;struct entry *next;

    };

    struct entry n1,n2;

    n1.next = &n2;}struct entry *next; - Struct entry contains a pointer to an

    entry structure called next

  • 8/3/2019 p12 Linked List

    8/18

    Linked listsLinked listsmain()

    {struct entry

    {

    int value;

    struct entry *next;};

    struct entry n1,n2,n3;

    int i;

    n1.value = 100;

    n2.value = 200;

    n3.value = 300;

  • 8/3/2019 p12 Linked List

    9/18

    n1.next = &n2;n2.next = &n3;

    i = n1.nextvalue;printf (%d , i);

    printf (%d\n, n2.nextvalue);} Output: 200 300

  • 8/3/2019 p12 Linked List

    10/18

    i = n1.nextvalue; - the value member of theentry structure pointed to by n1.next isaccessed and assigned to the integer

    variable i.

    i = *(n1.next).value

    n1.nextvalue is correct and n1.next.value isincorrect as n1.next field contains apointer to a structure

  • 8/3/2019 p12 Linked List

    11/18

    LINKED LISTLINKED LIST

    Basic operations on a list are:

    Search the list for a particular item

    Insert an item in the list

    Delete an item from the list

    Using the pointer given to the first node,

    we must step through the nodes of the list.

  • 8/3/2019 p12 Linked List

    12/18

    Inserting an ElementInserting an Element

    n2_3 to be inserted after n2 but before n3

    struct entry n1, n2, n3, n2_3;

    n2_3.next = n2.next;

    n2.next = &n2_3;

    n2_3 shall be inserted to the list immdiately aftern2 and before n3

    Sequence of statements is important.

    n2.next = &n2_3; n2_3.next = n2.next not correctas pointer in n2 is overwritten

    n2_3 can be anywhere in memory

  • 8/3/2019 p12 Linked List

    13/18

    Removing an ElementRemoving an Element

    struct entry n1,n2,n3;

    n1.next = &n2;

    n2.next = &n3;

    ........

    To remove n2 from the list

    n1.next = n2.next

    This is same as n1.next = &n3

    n2 is removed from the list

  • 8/3/2019 p12 Linked List

    14/18

    Traversing the Linked ListTraversing the Linked List

    Normally a Pointer to the linked list is usedfor traversing the entries in the linked list

    Pointer is initially set to the start of the list

    struct entry *list_pointer = &n1

    The end of the list has to be identified

    A null pointer (pointer value of 0) is used forsuch a purpose

    n3.next = 0;

  • 8/3/2019 p12 Linked List

    15/18

    Linked List TraversalLinked List Traversalmain()

    {struct entry

    {

    int value;

    struct entry *next;};

    struct entry n1,n2,n3;

    struct entry *list_pointer = &n1;

    n1.value=100; n1.next = &n2;n2.value =200; n2.next = &n3;

    n3.value =300; n3.next = 0; (null pointer)

  • 8/3/2019 p12 Linked List

    16/18

    Traversing through the List

    while (list_pointer !=0)

    {

    printf (%d\n, list_pointervalue);

    list_pointer = list_pointernext;

    }}

    Output = 100 200 300

  • 8/3/2019 p12 Linked List

    17/18

    I request Electronics and communication

    ENGINEERING students to visit my blogfor

    more

    abhishek1ek.blogspot.com

    awhengineering.blogspot.com

  • 8/3/2019 p12 Linked List

    18/18

    Thank you