hydra123
hydra123

Reputation: 347

linked list: how to return the whole linked list after insterting

I am trying to return the head of a linked list in the function Insert of the following program. However, it is failing with compilation error. Can anyone please tell me what wrong I have done:

#include<stdio.h>
#include<stdlib.h>

struct ListNode
{
    int data;
    struct ListNode *next;
};


int ListLength(struct ListNode *head)
{
    int count = 0;
    struct ListNode *temp=head;
    while(temp!=NULL)
    {
        count++;
        temp=temp->next;
    }
    return count;
}

struct ListNode *Insert(struct ListNode *head, int value, int pos)
{
    struct ListNode *temp,*curr;
    curr=head;
    int k=1;
    temp=(struct ListNode *)malloc(sizeof(struct ListNode));
    if(pos==1)
    {
        temp->data=value;
        if(head==NULL)
        {
            temp->next=NULL;
            head=temp;
        }
        else
        {
            temp->next=head;
            head=temp;
        }
    }
    else
    {
        while((curr!=NULL) && (k<pos))
        {
            k++;
            curr=curr->next;
        }
        temp->data=value;
        temp->next=curr->next;
        curr->next=temp;

    }
    return head;
}
void printList(struct ListNode *head)
{
    struct ListNode *temp;
    temp=head;
    while(temp!=NULL)
    {
        printf("%d",temp->data);
        printf(" ");
        temp=temp->next;
    }
}
int main
{
    struct ListNode *head=NULL;
    //head = NULL;
    head=Insert(head,10,1);
    //Insert(head,11,2);
    printList(head);

    return 0;
}

I am trying to return the head of the new linked list after the insertion. I don't know where I am going wrong. Thanks in advance for the help.

Upvotes: 1

Views: 10324

Answers (2)

nj2237
nj2237

Reputation: 1278

(i) Firstly, include int main(void) as mentioned in the comments.

(ii) Next, with your current code, when you try printing the list, you are going to be in an infinite loop and get a stack overflow.

To avoid this, increment the temp to point to the next node after each print.

So your print function should look like:

void printList(struct ListNode *head)
{
    struct ListNode *temp;
    temp=head;
    while(temp!=NULL)
    {
        printf("%d",temp->data);
        printf(" ");
        temp=temp->next; // this line is required
    }
}

(iii) And in your main function, call the printList with an argument, that is the head of the node like this:

printList(head);

(iv) And don't forget to return the count in your finding the length of the list function. Add the return statement at the end of your ListLength function:

return count;

(v) Your current code does not handle a case when head is NULL, and user wants to insert at a position greater than 1. Or more generally, when a user wants to insert at a position that is greater than the current list's length.

While you trust such an input would not be given, always handle such exceptions (you would probably get a SEGMENTATION FAULT here when trying to access memory of null nodes).

To handle this, you can add a check at the start of the Insert function like,

int lenList = ListLength(head);
if (lenList < pos)
    {
        printf("Please provide a position less than %d to insert", lenList);
        return 0; // don't proceed with inserting node with NULL pointers
    }

Upvotes: 2

purec
purec

Reputation: 318

If head is declared global you don't have to return it. (Sorry, my answer is short)

Upvotes: -1

Related Questions