Subscribe Us

Responsive Advertisement

Advertisement

problem 24(almost everything about link list)

 


problem 24(almost everything about  link list)

by ujjal roy



#include<stdio.h>

struct node

{

    int data;

    struct node *next;

};

struct node*create(int value)//this function use for create a node

{

     struct node *temp;

    temp=(struct node *)malloc(sizeof(struct node));

    temp->data=value;

    temp->next=NULL;

};

void traversal(struct node* head1)//this function traverse all list

{

    while(head1!=NULL)

    {

        printf("%d ",head1->data);

        head1=head1->next;

    }

    printf("\n");

}

struct node *createlinklist(int brr[],int size)

{

    struct node *head=NULL,*temp=NULL,*current=NULL;

    for(int i=0;i<size;i++)

    {

       temp=(struct node *)malloc(sizeof(struct node));

       temp->data=brr[i];

       temp->next=NULL;

       if(head==NULL)

       {

           head=temp;

           current=temp;

       }

       else

       {

           current->next=temp;

           current=temp;

       }

    }


    return head;

};

int searchlinklist(struct node *head1,int val)

{

    int index=1;

    while(head1!=NULL)

    {

        if(head1->data==val)

        {

            return index;


        }

        index++;

        head1=head1->next;

    }

    return -1;

}

struct node *insertatbegin(struct node *head1,int val)

{

    struct node *temp;

    temp=(struct node *)malloc(sizeof(struct node));

       temp->data=val;

       temp->next=head1;

       return temp;


};

struct node *insertatend(struct node *head1,int val)

{

    struct node *temp,*current;

    temp=(struct node *)malloc(sizeof(struct node));

       temp->data=val;

       temp->next=NULL;

       current=head1;

       while(current->next!=NULL)

       {

           current=current->next;


       }

       current->next=temp;


       return head1;


};

struct node *insertatmiddle(struct node *head1,int val,int position)

{

    int cnt=0;

    struct node *temp;

    temp=head1;

    while(temp!=NULL)

    {

        cnt++;

        if(cnt==position)

        {

            struct node *newnode;

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

            newnode->data=val;

            newnode->next=temp->next;

            temp->next=newnode;


        }

        temp=temp->next;

    }

    return head1;


}

struct node *deletenode(struct node *head1,int val)

{

    struct node *dummy,*temp;

    dummy=(struct node *)malloc(sizeof(struct node));

    dummy->next=head1;

    temp=dummy;

    while(temp->next!=NULL)

    {

        if(temp->next->data==val)

        {

            temp->next=temp->next->next;

        }

        temp=temp->next;

    }


    return dummy->next;

};

int main()

{

    int val;

    struct node *head,*a,*b,*newhead;

    /*head=(struct node *)malloc(sizeof(struct node));

    head->data=10;

    head->next=NULL;

    a=head;

    printf("Enter value : ");

    scanf("%d",&val);

    b=create(val);//create link list by using create function

    a->next=b;

    a=b;

    printf("Enter value : ");

    scanf("%d",&val);

    b=create(val);//create link list by using create function

    a->next=b;

    a=b;

     printf("Enter value : ");

    scanf("%d",&val);

    b=create(val);//create link list by using create function

    a->next=b;

    a=b;

     printf("Enter value : ");

    scanf("%d",&val);

    b=create(val);//create link list by using create function

    a->next=b;

    a=b;

    head=head->next;

   traversal(head);//traversal function traverse all list*/


   int arr[5]={10,20,30,40,50};

   head=createlinklist(arr,5);

   traversal(head);

  /* int r=searchlinklist(head,50);

   if(r==-1)printf("Not found\n");

   else

   {

       printf("Found at position %d \n",r);

   }*/

  /* newhead=insertatbegin(head,5);

   traversal(newhead);*/

  /* newhead=insertatend(head,5);

   traversal(newhead);*/

   newhead=insertatmiddle(head,100,2);

   traversal(newhead);

   newhead=deletenode(head,30);

   traversal(newhead);



    return 0;

}


Post a Comment

0 Comments