用C链表问题 [英] Linked list issue in C

查看:145
本文介绍了用C链表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要生成随机数,并把它们的排序链表。我的code运行在Cygwin的我家的电脑上很好,但是当我在学校系统上运行它,我不断收到该列表是空的。不知道是什么问题。

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;typedef结构节点{
    INT NUM;
    结构节点*接下来的;
} node_t;node_t * insertNodeSorted(node_t *头,INT X);
无效的printList(node_t *头);
无效deleteList(node_t *头);INT主(INT ARGC,CHAR *的argv [])
{
    node_t *假人;
    INT计数器= 1;
    int类型的;    如果(argc个!= 4)
    {
            的printf(错误退出程序...。);
            出口(1);
    }//万一    //种子随机数
    srandom(与atoi(ARGV [1]));
    虚设= NULL;    而(计数器<与atoi(argv的[2]))
    {
            一个=随机()%(与atoi(argv的[3])+ 1);            的printf(%d个,一);            insertNodeSorted(假人,一个);            反++;
    } //结束时    的printf(\\ n \\ n);
    的printList(虚设);    deleteList(虚设);    返回0; } node_t * insertNodeSorted(node_t *头,INT X)
 {
    如果(头== NULL)
    {
            头=(node_t *)malloc的(的sizeof(node_t));
            如果(头== NULL)
            {
                    的printf(无法创建头节点);
                    返回头;
            }//万一
    流浆> NUM = X;
    流浆>接着= NULL;
    返回头;
    }//万一    node_t * P;    P =(node_t *)malloc的(的sizeof(node_t));
    如果(P == NULL)
    {
            的printf(无法创建一个新的节点。);
            回磷;
    }//万一    P-> NUM = X;
    对 - >接着= NULL;    如果(X<流浆> NUM)
    {
            P->接下来=头;
            回磷;
    }//万一    node_t * Q,* R;
    Q =头;
    而(Q = NULL&放大器;!&安培; Q-> NUM< = X)
    {
             - [R = Q;
            Q = Q->接着,
    } //结束时
    P->接下来= Q;
    R->接下来= P; } 无效的printList(node_t *头)
 {
    node_t * P;    如果(头== NULL)
    {
            的printf(列表为空);
            出口(1);
    }//万一    P =流浆>接下来,
    而(P!= NULL)
    {
            的printf(%d个,P-> NUM);
            p值=对 - >接着,
    } //结束时
 } 无效deleteList(node_t *头)
 {
    node_t * P;
    而(P!= NULL)
    {
            P =流浆>接下来,
            免费(头);
            头= P;
    } //结束时
 }


解决方案

您的功能


  1. 是未定义行为,因为在某些情况下,它没有返回值和

  2. 在主你没有重新分配头。因此,它是不
    改变了。

该功能可以看看下面的方式

  node_t * insertNodeSorted(node_t **头,INT X)
 {
    node_t * P =(node_t *)malloc的(的sizeof(node_t));    如果(P == NULL)
    {
            的printf(无法创建一个新的节点\\ n);
    }//万一    其他
    {
        P-> NUM = X;        如果(*头== NULL || X'≤(*头) - > NUM)
        {
            P->接下来= *头;
            *头= P;
        }
        其他
        {
            node_t *电流= *头;
            而(电流 - >接下来= NULL&放大器;!&安培;!(X<电流 - >下一步 - > NUM))
            {
                电流=电流 - >接着,
            }            P->接下来=电流 - >接下来,
            电流 - >接下来= P;
        }
    }    回磷;
}

和函数必须这样调用

  insertNodeSorted(安培;假人,一个);

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
 }

解决方案

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

这篇关于用C链表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆