简体   繁体   中英

I'm trying to add numbers into a linked list in ascending order. what can I fix so it'll work? Here's my code:

This code is supposed to put numbers into a linked list in ascending order. This is one function of my program that takes keyboard input(int x) from the user of the program.

node* insert(node *head, int x)
{
    node *newPtr;
    node *prePtr;
    node *currentPtr;

    newPtr = malloc(sizeof(node));

    if(newPtr != NULL){
            newPtr->value = x;
            newPtr->next = NULL;

            prePtr = NULL;
            currentPtr = head;

            while(currentPtr != NULL && x > currentPtr->value){
                    prePtr = currentPtr;
                    currentPtr = currentPtr->next;
            }

            if(prePtr == NULL){
                    newPtr->next = head;
                    head = newPtr;
            }
            else{
                    prePtr->next = newPtr;
                    newPtr->next = currentPtr;
            }
    }
}
    int main(void)//calling input function in main
    {
        node *head = NULL;
        int x=0;
        while(x!=-1){
                printf("?: ");
                scanf("%d", &x);
                head=insert(head,x);
                print(head);
                printf("\n");
        }
        return 0;
    }

//It seems to only put a few numbers in, then it resets

The main() function asks for a numerical input, and sends that input into the insert function which is supposed to put the numbers in ascending order. Sample output:

$ gcc prelab2.c
$ ./a.out
?: 4
4 -> NULL
?: 3
3 -> 4 -> NULL
?: 9
3 -> 4 -> 9 -> NULL
?: 7
3 -> 4 -> 7 -> 9 -> NULL
?: 2
2 -> 3 -> 4 -> 7 -> 9 -> NULL
?: -1

There is only a small mistake. In main() method

head=insert(head,x);

your Insert method return nothing (NULL). so your head is never changed it is always NULL;

return head;

Just return head in insert method and it will work fine.

Try the following changes it should work fine.

node* insert(node *head, int x)
{
node *newPtr;
node *prePtr;
node *currentPtr;

newPtr = malloc(sizeof(node));

if(newPtr != NULL)
{
        newPtr->value = x;
        newPtr->next = NULL;
        // check if the linked list is empty add the node directly and return 
        if(head == NULL)
        {
            head = newptr;
            return head;

        }
        else 
        {

        currentPtr = head;

        while(x > currentPtr->value && currentPtr->next != NULL)
        {         
                currentPtr = currentPtr->next;
        }
             // make the new node point where current next is pointing
               newPtr->next = currentPtr->next;
             // make the current point to new node
               currentPtr->next = newPtr; 
        }
return head;
}
else
     return NULL; // signifying malloc failed

}

EDIT: corrected the code missed out a condition check while copying

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM