linked list with its operations

Upload: sanchit-jain

Post on 03-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Linked List With Its Operations

    1/15

  • 7/28/2019 Linked List With Its Operations

    2/15

    Difference between Linked lists and arrays:

    An array is a static data structure. This means the

    length of array cannot be altered at run time.

    While, a linked list is a dynamic data structure. In an array, all the elements are kept at

    consecutive memory locations while in a linked

    list the elements (or nodes) may be kept at anylocation but still connected to each other.

  • 7/28/2019 Linked List With Its Operations

    3/15

    Insertion

    Insert as a first node

    Insert as a last node

    Insert after a node

    Insert before a node

    Deletion

    Delete first node

    Delete last node

    Traversal

  • 7/28/2019 Linked List With Its Operations

    4/15

    For insertion:

    A record is created holding the new item.

    The nextpointer of the new record is set to link it tothe item which is to follow it in the list.

    The nextpointer of the item which is to precede itmust be modified to point to the new item.

    For deletion:

    The nextpointer of the item immediately precedingthe one to be deleted is altered, and made to point tothe item following the deleted item.

  • 7/28/2019 Linked List With Its Operations

    5/15

    head 48 17 14293

    Note:

    There is no work to find the correct location

    Empty or not, head will point to the right location

  • 7/28/2019 Linked List With Its Operations

    6/15

    void insertf()

    {

    struct studinfo *newnode;

    newnode=(struct studinfo *) malloc(sizeof(struct studinfo)); /*Creating new node*/

    printf("\nEnter a new record : marks and name ");

    scanf("%d%s",&newnode->marks,newnode->name);newnode->next=NULL;

    if(start==NULL)

    start = newnode;

    else

    {

    newnode->next = start;

    start = newnode;

    }

    }

  • 7/28/2019 Linked List With Its Operations

    7/15

    Note: Find the end of the list

    Recursion or iteration

    48 17 142head //93 //

  • 7/28/2019 Linked List With Its Operations

    8/15

    void insertl()

    {

    struct studinfo *newnode, *ptr;

    newnode=(struct studinfo *) malloc(sizeof(struct studinfo));

    printf("\nEnter a new record : marks and name ");

    scanf("%d%s",&newnode->marks,newnode->name);newnode->next=NULL;

    if (start == NULL)

    start =newnode;

    else

    {

    ptr =start;

    while (ptr->next != NULL)

    ptr= ptr->next;

    ptr->next = newnode;

    }

    }

  • 7/28/2019 Linked List With Its Operations

    9/15

    Head17 48 142 //93 //142

    Note:

    Used when order is important

    Go to the node that should follow the one to add, Recursion or

    iteration

  • 7/28/2019 Linked List With Its Operations

    10/15

    void inserta()

    {

    int cnt=1, no;

    struct studinfo *ptr,*prevptr, *newnode;

    printf("\nEnter number ...");

    scanf("%d",&no);

    ptr=start;while (cnt != no)

    { ptr = ptr->next;

    cnt++; }

    newnode=(struct studinfo *) malloc(sizeof(struct studinfo));

    printf("\nEnter a new record : marks and name ");

    scanf("%d%s",&newnode->marks,newnode->name);newnode->next =NULL;

    newnode->next = ptr->next;

    ptr->next = newnode;

    }

  • 7/28/2019 Linked List With Its Operations

    11/15

    void insertb()

    {

    int cnt=1, no;

    struct studinfo *ptr,*prevptr, *newnode;

    printf("\nEnter number ...");

    scanf("%d",&no);

    ptr=start;if(no==1)

    {

    insertf();

    }

    else

    {while(cnt !=no)

    {

    prevptr=ptr;

    ptr = ptr->next;

    cnt++;

    }

  • 7/28/2019 Linked List With Its Operations

    12/15

    newnode=(struct studinfo *) malloc(sizeof(struct

    studinfo));

    printf("\nEnter a new record : marks and name ");

    scanf("%d%s",&newnode->marks,newnode->name);

    newnode->next=ptr;

    prevptr->next=newnode;

    }

    }

  • 7/28/2019 Linked List With Its Operations

    13/15

    void deletef(){

    struct studinfo *ptr;if (start == NULL)

    {printf("\n List is empty");return;}

    ptr=start;

    start=start->next;free(ptr);

    }

  • 7/28/2019 Linked List With Its Operations

    14/15

    void deletel(){

    struct studinfo *ptr,*prevptr;

    ptr=start;

    if (ptr->next==NULL)

    start = NULL;

    else

    {while(ptr->next!=NULL)

    {

    prevptr=ptr;

    ptr=ptr->next;

    }

    prevptr->next =NULL;

    }free(ptr);

    }

  • 7/28/2019 Linked List With Its Operations

    15/15

    void traverse(){

    struct studinfo *ptr;

    if (start == NULL)

    {

    printf("\n List is empty");

    return;}

    ptr= start;

    while (ptr !=NULL)

    {

    printf("\nRecord: marks and name %d %s\n",ptr->marks, ptr->name);

    ptr = ptr->next;

    }

    getch();

    }