Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用双链表排序的问题_C_Sorting_Debugging_Segmentation Fault - Fatal编程技术网

C 使用双链表排序的问题

C 使用双链表排序的问题,c,sorting,debugging,segmentation-fault,C,Sorting,Debugging,Segmentation Fault,我试图通过以下方法在C中实现排序-接受整数并将它们保存到数组中。传递每个数组值并将其按升序排列在双链接列表中。我已经写了这个程序,但我面临崩溃,调试说这是显示功能中的一个分段错误,但我仍然无法理解。 如有任何提示,将不胜感激 #include<stdio.h> #include<stdlib.h> struct node //Doubly linked list to store in ascending order { int info; struct

我试图通过以下方法在C中实现排序-接受整数并将它们保存到数组中。传递每个数组值并将其按升序排列在双链接列表中。我已经写了这个程序,但我面临崩溃,调试说这是显示功能中的一个分段错误,但我仍然无法理解。 如有任何提示,将不胜感激

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

struct node //Doubly linked list to store in ascending order
{
    int info;
    struct node *llink; //Left link
    struct node *rlink; //Right link
};

typedef struct node *NODE; //Define a custom data type for pointer to node(a structure)

/*Accept an integer argument and insert into a doubly linked list at appropriate position to arrange in ascending order*/
void list_sort(NODE head,int item)
{
    NODE cur,prev,temp,prev_to_prev;
    temp=malloc(sizeof(struct node)); //Allocate block of memory for a new node to be inserted
    temp->info=item; //Assign the integer to info field of the node
    if(head->rlink==NULL) //head->rlink==NULL when the first node is being inserted
    {
        head->rlink=temp;
        temp->llink=head;
        temp->rlink=head; //Last points to first because of circular representation used here
        return;
    }
        cur=head->rlink;
        while(cur!=head)
        {
            prev=cur->llink;
            if(temp->info<cur->info&&temp->info>=prev->info) //when  prev->info<temp->info<cur->info insert between prev and cur
            {
                prev->rlink=temp;
                temp->llink=prev;
                cur->llink=temp;
                temp->rlink=cur;
                return;
            }
            else if(temp->info>=cur->info && temp->info>prev->info) // when temp->info>cur->info and also > prev->info insert at last
            {
                cur->rlink=temp;
                temp->llink=cur;
                return;
            }
            else if(temp->info<cur->info && temp->info<prev->info) // when temp->info<cur->info and also < prev->info insert before them
            {
                prev_to_prev = prev->llink;
                prev_to_prev->rlink=temp;
                temp->rlink=prev;
                return;
            }
        cur=cur->rlink;
        }
}

void list_disp(NODE head)
{
    NODE cur;
    printf("\nSorted elements are - \n");
    cur=head->rlink;
    while(cur!=head)
    {
        printf("%d\n",cur->info);
        cur=cur->rlink;
    }

}

void main()
{
    int items[10],i;
    NODE head,cur;
    head=malloc(sizeof(struct node));
    head->llink=head->rlink=NULL;
    head->info=0;
    printf("Enter 5 items to sort: \n"); //Take only 5 items for now
    for(i=0;i<5;i++)
        scanf("%d",&items[i]); //Read 5 items
    for(i=0;i<5;i++)
        list_sort(head,items[i]); //Call function for all 5 items
    list_disp(head); //Display the linked list entry by entry 

}
#包括
#包括
结构节点//按升序存储的双链接列表
{
国际信息;
结构节点*llink;//左链接
结构节点*rlink;//右链接
};
类型定义结构节点*节点//为指向节点(结构)的指针定义自定义数据类型
/*接受整型参数,并将其插入到双链接列表中的适当位置,以按升序排列*/
无效列表\排序(节点头,整数项)
{
节点cur、prev、temp、prev_至_prev;
temp=malloc(sizeof(struct node));//为要插入的新节点分配内存块
temp->info=item;//将整数分配给节点的info字段
如果(head->rlink==NULL)//插入第一个节点时head->rlink==NULL
{
压头->rlink=温度;
温度->llink=压头;
temp->rlink=head;//由于此处使用循环表示,最后一个点指向第一个点
返回;
}
cur=头->链接;
while(cur!=头)
{
prev=当前->llink;
if(temp->infoinfo&&temp->info>=prev->info)//when prev->infoinfo在prev和cur之间插入
{
prev->rlink=温度;
温度->llink=上一个;
cur->llink=温度;
温度->rlink=cur;
返回;
}
否则如果(temp->info>=cur->info&&temp->info>prev->info)//当temp->info>cur->info和也>prev->info最后插入时
{
cur->rlink=温度;
温度->llink=电流;
返回;
}
else if(temp->infoinfo&&temp->infoinfo)//when temp->infoinfo以及在它们前面插入info
{
prev_to_prev=prev->llink;
上一个到上一个->链接=温度;
温度->链接=上一个;
返回;
}
cur=cur->rlink;
}
}
无效列表显示(节点头)
{
节点电流;
printf(“\n未选元素为-\n”);
cur=头->链接;
while(cur!=头)
{
printf(“%d\n”,cur->info);
cur=cur->rlink;
}
}
void main()
{
国际项目[10],i;
节点头,cur;
head=malloc(sizeof(结构节点));
head->llink=head->rlink=NULL;
头部->信息=0;
printf(“输入5个要排序的项目:\n”);//现在只取5个项目

对于(i=0;i一个潜在问题是,在某些情况下,您没有初始化
rlink
llink

另一个事实是,您使用的是简单赋值运算符,而不是等价运算符:

temp->info>=cur->info

现在是学习如何在调试器中单步执行代码的好时机,在执行过程中检查变量
如果不通过引用传递,请始终保持不修改?好的,所以我做了一些工作,问题是未初始化的rlink和llink。另外两行将其整理好,程序现在运行良好。谢谢!