C中的链接列表问题

时间:2015-11-04 14:57:49

标签: c linked-list

我必须生成随机数并将它们放入已排序的链表中。我的代码在cygwin上的家用电脑上运行正常,但是当我在学校系统上运行时,我一直认为列表是空的。不确定是什么问题。

#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
 }

2 个答案:

答案 0 :(得分:3)

你的功能

  1. 有未定义的行为,因为在某些情况下它不返回任何内容和
  2. 主要是你没有重新分配头部dummy。所以事实并非如此 改变。

    该功能可以按以下方式查看

     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;  
    }
    

    必须像

    一样调用该函数
    insertNodeSorted( &dummy, a );
    

答案 1 :(得分:0)

由于它的作业我不会给你完整的答案....但是看看主要假人的内容是什么,以及headNodeSorted中头部的局部值。

如果你只是使用gcc而没有调试器...添加更多printfs它会帮助你...