Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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_Struct_Linked List_Bubble Sort - Fatal编程技术网

c链表中的冒泡排序

c链表中的冒泡排序,c,sorting,struct,linked-list,bubble-sort,C,Sorting,Struct,Linked List,Bubble Sort,我需要做的是把输入文件读入一个链表。文件的一部分是: 娜玛,25岁 NameB,33 NameC,23 名字是,39岁 之后,我需要按数字排序(气泡排序)并将其写入另一个文件 以下是我所拥有的: #包括 #包括 #包括 结构节点{ 字符名[20]; 整数; 结构节点*下一步; 结构节点*prev; }*头部; 内部主(空){ 结构节点*temp; temp=malloc(sizeof(struct node)); temp->next=NULL; 压头=温度; 文件*ifp; 字符fnamer

我需要做的是把输入文件读入一个链表。文件的一部分是:

娜玛,25岁
NameB,33
NameC,23
名字是,39岁

之后,我需要按数字排序(气泡排序)并将其写入另一个文件

以下是我所拥有的:

#包括
#包括
#包括
结构节点{
字符名[20];
整数;
结构节点*下一步;
结构节点*prev;
}*头部;
内部主(空){
结构节点*temp;
temp=malloc(sizeof(struct node));
temp->next=NULL;
压头=温度;
文件*ifp;
字符fnamer[100]=“”;
字符行[128];
//printf(“\n\n请输入文件的完整路径:\n”);
//scanf(“%s”和&fnamer);
ifp=fopen(“mintaadatok.txt”,“r”);
如果(ifp==NULL){
printf(“\n%s\”文件未找到!”,fnamer);
出口(1);
}
int c=0;
字符缓冲区[1024];
memset(缓冲区,0,1024);
而(c<15){
fgets(缓冲区,1024,ifp);
sscanf(缓冲区,“%19[^,],%d”,临时->名称,&临时->编号);
printf(“%d%s%d\n”,c,temp->name,temp->number);
temp->next=malloc(sizeof(结构节点));
温度=温度->下一步;
temp->next=NULL;
C++;
}
int i,步骤;
用于(温度=头部;温度;温度=温度->下一步){
printf(“%s”,临时->名称);
printf(“%d\n”,临时->编号);
对于(步骤=0;步骤编号>临时->下一步)
{
温度=温度->编号;
临时->编号=临时->下一步;
温度->下一步=温度;
}
}
}
printf(“按升序:”);
}
你能帮我把这些数据分类吗?
我们初学者应该互相帮助。:)

我没有看完你所有的代码,但是它显然是不正确的,例如,由于这个循环中节点的分配顺序不正确

while (c < 15) {
    fgets(buffer, 1024, ifp);
    sscanf(buffer, "%19[^,], %d", temp->name, &temp->number);
    printf("%d %s %d\n", c, temp->name, temp->number);
    temp->next = malloc(sizeof(struct node));
    temp = temp->next;
    temp->next = NULL;
    c++;
}
程序输出为

{ NameA, 25 } { NameB, 33 } { NameC, 23 } { NameD, 39 } 
{ NameC, 23 } { NameA, 25 } { NameB, 33 } { NameD, 39 } 
{ NameA, 25 } { NameB, 33 } { NameC, 23 } { NameD, 39 } 
在演示程序中,列表首先按ID排序,然后按名称排序


因此,您现在所需要的就是根据所用文件中的数据正确地构建列表。

对于链表的冒泡排序,您首先需要一个头指针,然后再需要两个指针,实现方式与冒泡排序相同,但略有不同,因为链表元素不能通过索引直接访问,您必须使用t他遵循两个指针来比较值,就像对冒泡排序中的数组进行比较一样

    pptr=head;  //for the previou element
    ptr=head->next;//for the next element

   if(pptr->value > ptr->value)  //comparing the value in linked list

别忘了增加两个指针。

您到底面临什么问题?@AditiRawat我无法对链接进行气泡排序list@coderredoc是的,这将有助于LOT。首先,您没有为排序方法编写任何代码;您只是填充列表。其次,为了在链接列表上执行几乎所有操作,您需要
head
指针,您没有存储在这里。最后,如果您有
head
指针,请使用简单的谷歌搜索(您的问题标题)将产生有用的结果。然后您可能应该向我们展示您编写的排序代码,然后我们可能会提供帮助。:)谢谢您的回答,看起来不错:)@Vlad来自莫斯科。您忘记了
free()
==>>>
堆的总使用量:5个allocs,1个frees,1152个字节分配
@Michi我没有忘记。这是一个演示程序,只包含提供排序所需的那些函数。@Vladfrommosco我同意,但通知OP有关
free
的信息并不坏:)。@Michi他的任务是正确地编写所有f单链表的功能/我的任务是展示如何使用冒泡排序对单链表进行排序。
    pptr=head;  //for the previou element
    ptr=head->next;//for the next element

   if(pptr->value > ptr->value)  //comparing the value in linked list