Linked list issue in C

余生长醉 提交于 2019-12-13 08:52:17

问题


I have to generate random numbers and put them in a linked list sorted. my code runs fine on my home computer on cygwin, however when I run it on the schools system, i keep getting that the list is empty. Not sure what the issue is.

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

typedef struct node{
    int num;
    struct node *next;
}node_t;

node_t* insertNodeSorted(node_t *head, int x);
void printList(node_t *head);
void deleteList(node_t *head);

int main(int argc, char *argv[])
{
    node_t *dummy;
    int counter = 1;
    int a;

    if(argc != 4)
    {
            printf("Error. Exiting program...");
            exit(1);
    }//end if

    //seed random number
    srandom(atoi(argv[1]));
    dummy = NULL;

    while(counter < atoi(argv[2]))
    {
            a = random() % (atoi(argv[3]) + 1);

            printf("%d " ,a);

            insertNodeSorted(dummy, a);

            counter++;
    }//end while

    printf("\n\n");
    printList(dummy);

    deleteList(dummy);

    return 0;



 }

 node_t* insertNodeSorted(node_t *head, int x)
 {
    if(head == NULL)
    {
            head = (node_t *)malloc(sizeof(node_t));
            if(head == NULL)
            {
                    printf("Failed to create head node");
                    return head;
            }//end if
    head->num = x;
    head->next = NULL;
    return head;
    }//end if

    node_t *p;

    p = (node_t*)malloc(sizeof(node_t));
    if(p == NULL)
    {
            printf("Failed to create a new node.");
            return p;
    }//end if

    p->num = x;
    p->next = NULL;

    if(x < head->num)
    {
            p->next = head;
            return p;
    }//end if

    node_t *q, *r;
    q = head;
    while(q != NULL && q->num <= x)
    {
            r = q;
            q = q->next;
    }//end while
    p->next = q;
    r->next = p;

 }

 void printList(node_t *head)
 {
    node_t *p;

    if(head == NULL)
    {
            printf("List is empty");
            exit(1);
    }//end if

    p = head->next;
    while(p != NULL)
    {
            printf("%d ",p->num);
            p = p->next;
    }//end while
 }

 void deleteList(node_t *head)
 {
    node_t *p;
    while(p != NULL)
    {
            p = head->next;
            free(head);
            head = p;
    }//end while
 }

回答1:


Your function

  1. has undefined behaviour because in some cases it returns nothing and
  2. in the main you did not reassign the head dummy. So it is not changed.

The function can look the following way

 node_t* insertNodeSorted( node_t **head, int x )
 {
    node_t *p = ( node_t * )malloc( sizeof( node_t ) );

    if ( p == NULL )
    {
            printf( "Failed to create a new node.\n" );
    }//end if

    else
    {  
        p->num = x;

        if ( *head == NULL || x < ( *head )->num )
        {
            p->next = *head;
            *head = p;
        }
        else
        {
            node_t *current = *head;
            while ( current->next != NULL && !( x < current->next->num ) )
            {
                current = current->next;
            }

            p->next = current->next;
            current->next = p;
        }
    }

    return p;  
}

And the function must be called like

insertNodeSorted( &dummy, a );



回答2:


Since it's homework I will not give you the full answer.... but look at what is going on with main's dummy, and the local value of head inside insertNodeSorted.

If you are just using gcc and no debugger... add more printfs it'll help a ton...



来源:https://stackoverflow.com/questions/33525014/linked-list-issue-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!