Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.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_Linked List_Bubble Sort - Fatal编程技术网

C 将文本文件读入链表(打印顺序错误)

C 将文本文件读入链表(打印顺序错误),c,sorting,linked-list,bubble-sort,C,Sorting,Linked List,Bubble Sort,我使用了气泡排序函数对链表进行排序,但打印时 它实际上并没有打印已排序的列表。 您能告诉我故障所在吗?这样我可以更进一步: 完整运行程序: #include <stdlib.h> #include <string.h> #include <time.h> #include <stdio.h> struct employee { char name[30]; char surname[30]; char address[30]; char cell

我使用了
气泡排序
函数对链表进行排序,但打印时 它实际上并没有打印已排序的列表。 您能告诉我故障所在吗?这样我可以更进一步:

完整运行程序:

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

struct employee
{
char name[30];
char surname[30];
char address[30];
char cell[30];
struct emp *next;
}*start=NULL;

struct employee *new_node;
struct employee *current;
FILE *fp;

int main(char *argv[],int argc)
{
file_open();
printf("Before bubble sort:\n");
print_list(start);
printf("After  bubble sort:\n");
bubble_sort();
print_list(start);
return 0;
}

file_open()
{
char fname[20];
char line[128];
printf("ENTER THE FILE NAME:");
scanf("%s",fname);
fp=fopen(fname,"r");
if(fp != NULL)
{
    while(fgets(line,sizeof(line),fp) != NULL)
    {
        //printf("FILE OPEN SUCCESSFULL");
        splitline(line);
        //printf("%s",line);
    }
    fclose ( fp );
}
else
{
    printf("ERROR OPEN FILE\n");
    return (1);
}
return 0;

}

 int splitline(char str[])
 {
 new_node=(struct employee*)malloc(sizeof(struct employee));
 char *store;
 store=strtok(str,", ");
 strcpy(new_node->name,store);
 store=strtok(NULL,", ");
 strcpy(new_node->surname,store);
 store=strtok(NULL,", ");
 strcpy(new_node->address,store);
 store=strtok(NULL,", ");
 strcpy(new_node->cell,store);
 new_node->next=NULL;

 if(start == 0)
 {
 start=new_node;
 current=new_node;
 }
 else
 {
current->next=new_node;
current=new_node;
 }
//print_list(current);
 return 0;
 }

 void print_list(struct employee *start)
 {
struct employee *ptr;
ptr=start;
while(ptr!=NULL)
{
printf("%s\n%s\n%s\n%s\n",ptr->name,ptr->surname,ptr->address,ptr->cell);
ptr=ptr->next;
}
printf("\n");

}

void bubble_sort(struct employee *start)
{
struct employee *a = NULL;
struct employee *b = NULL;
struct employee *c = NULL;
struct employee *e = NULL;
struct employee *tmp = NULL;


while(e != start->next) {
c = a = start;
b = a->next;
while(a != e) {
if(a->name > b->name) {
if(a == start) {
 tmp = b -> next;
 b->next = a;
 a->next = tmp;
 start = b;
 c = b;
} else {
 tmp = b->next;
 b->next = a;
 a->next = tmp;
 c->next = b;
 c = b;
}
} else {
c = a;
a = a->next;
}
b = a->next;
if(b == e)
e = a;
}
}
#包括
#包括
#包括
#包括
结构雇员
{
字符名[30];
查氏[30];
字符地址[30];
炭细胞[30];
结构emp*下一步;
}*start=NULL;
struct employee*新建_节点;
结构雇员*当前;
文件*fp;
int main(字符*argv[],int argc)
{
文件_open();
printf(“气泡排序前:\n”);
打印列表(开始);
printf(“气泡排序后:\n”);
气泡_排序();
打印列表(开始);
返回0;
}
文件_open()
{
char-fname[20];
字符行[128];
printf(“输入文件名:”);
scanf(“%s”,fname);
fp=fopen(fname,“r”);
如果(fp!=NULL)
{
while(fgets(line,sizeof(line),fp)!=NULL)
{
//printf(“文件打开成功”);
分裂线;
//printf(“%s”,第行);
}
fclose(fp);
}
其他的
{
printf(“错误打开文件\n”);
申报表(1);
}
返回0;
}
int分割线(字符str[])
{
新的_节点=(结构雇员*)malloc(sizeof(结构雇员));
字符*存储;
store=strtok(str,“,”);
strcpy(新建节点->名称、存储);
store=strtok(空,“”,“”);
strcpy(新节点->姓氏,商店);
store=strtok(空,“”,“”);
strcpy(新节点->地址、存储);
store=strtok(空,“”,“”);
strcpy(新节点->单元,存储);
新建节点->下一步=空;
如果(开始==0)
{
开始=新的_节点;
当前=新节点;
}
其他的
{
当前->下一步=新节点;
当前=新节点;
}
//打印列表(当前);
返回0;
}
作废打印列表(结构员工*开始)
{
结构员工*ptr;
ptr=启动;
while(ptr!=NULL)
{
printf(“%s\n%s\n%s\n%s\n”,ptr->name,ptr->姓氏,ptr->address,ptr->cell);
ptr=ptr->next;
}
printf(“\n”);
}
无效气泡排序(结构员工*开始)
{
结构employee*a=NULL;
结构employee*b=NULL;
结构employee*c=NULL;
结构employee*e=NULL;
结构employee*tmp=NULL;
while(e!=开始->下一步){
c=a=启动;
b=a->next;
while(a!=e){
如果(a->name>b->name){
如果(a==开始){
tmp=b->next;
b->next=a;
a->next=tmp;
开始=b;
c=b;
}否则{
tmp=b->next;
b->next=a;
a->next=tmp;
c->next=b;
c=b;
}
}否则{
c=a;
a=a->next;
}
b=a->next;
如果(b==e)
e=a;
}
}

}您在每个字段中都复制了相同的内容

strcpy(new_node->name,store);
strcpy(new_node->surname,store);
strcpy(new_node->address,store);
strcpy(new_node->cell,store);
您应该为
分割线
创建节点
添加边框

int AddNodeFromLine(char str[])
{
new_node=(struct employee*)malloc(sizeof(struct employee));
new_node->next=NULL;
char *store;
store=strtok(str,", ");
strcpy(new_node->name,store);
store=strtok(NULL,", ");
strcpy(new_node->surname,store);
store=strtok(NULL,", ");
strcpy(new_node->address,store);
store=strtok(NULL,", ");
strcpy(new_node->cell,store);
new_node->next=NULL;

if(start == 0)
{
    start=new_node;
    current=new_node;
}
else
{
    current->next=new_node;
    current=new_node;
}
print_list(current);
return 0;
}

你在每个领域都复制了同样的东西

strcpy(new_node->name,store);
strcpy(new_node->surname,store);
strcpy(new_node->address,store);
strcpy(new_node->cell,store);
您应该为
分割线
创建节点
添加边框

int AddNodeFromLine(char str[])
{
new_node=(struct employee*)malloc(sizeof(struct employee));
new_node->next=NULL;
char *store;
store=strtok(str,", ");
strcpy(new_node->name,store);
store=strtok(NULL,", ");
strcpy(new_node->surname,store);
store=strtok(NULL,", ");
strcpy(new_node->address,store);
store=strtok(NULL,", ");
strcpy(new_node->cell,store);
new_node->next=NULL;

if(start == 0)
{
    start=new_node;
    current=new_node;
}
else
{
    current->next=new_node;
    current=new_node;
}
print_list(current);
return 0;
}

你在每个领域都复制了同样的东西

strcpy(new_node->name,store);
strcpy(new_node->surname,store);
strcpy(new_node->address,store);
strcpy(new_node->cell,store);
您应该为
分割线
创建节点
添加边框

int AddNodeFromLine(char str[])
{
new_node=(struct employee*)malloc(sizeof(struct employee));
new_node->next=NULL;
char *store;
store=strtok(str,", ");
strcpy(new_node->name,store);
store=strtok(NULL,", ");
strcpy(new_node->surname,store);
store=strtok(NULL,", ");
strcpy(new_node->address,store);
store=strtok(NULL,", ");
strcpy(new_node->cell,store);
new_node->next=NULL;

if(start == 0)
{
    start=new_node;
    current=new_node;
}
else
{
    current->next=new_node;
    current=new_node;
}
print_list(current);
return 0;
}

你在每个领域都复制了同样的东西

strcpy(new_node->name,store);
strcpy(new_node->surname,store);
strcpy(new_node->address,store);
strcpy(new_node->cell,store);
您应该为
分割线
创建节点
添加边框

int AddNodeFromLine(char str[])
{
new_node=(struct employee*)malloc(sizeof(struct employee));
new_node->next=NULL;
char *store;
store=strtok(str,", ");
strcpy(new_node->name,store);
store=strtok(NULL,", ");
strcpy(new_node->surname,store);
store=strtok(NULL,", ");
strcpy(new_node->address,store);
store=strtok(NULL,", ");
strcpy(new_node->cell,store);
new_node->next=NULL;

if(start == 0)
{
    start=new_node;
    current=new_node;
}
else
{
    current->next=new_node;
    current=new_node;
}
print_list(current);
return 0;
}


嘿,迪普托,你的回答让我松了一口气,我明白了。这个程序花了我很长时间才解决。我被吓坏了。但现在我可以继续了。还有一件事我想问的是,我看到你们在print_列表(current)中发送current,但在main中,我在print_列表(new_节点)中发送new_节点。这并不冲突!我认为,您应该从main调用
print\u list(start)
来打印整个列表。
打印列表(当前)
打印列表(新节点)
都将打印最后一个节点。试着确认一下。嗨,迪普托,我再次需要你的帮助。实际上现在我想先按字母顺序对链表排序,然后按数字升序排序。你能带我去吗?我非常感谢你的帮助。按字母顺序排序是一项复杂的任务,请阅读相关内容,搜索并尝试自己完成。然后,如果你有任何具体的问题,发布一个新的问题。解决那个问题不在这个问题的范围之内。如果这个答案对你有帮助,请接受。好的,我会先试试,然后如果我遇到任何问题,我会问你。我感谢你的帮助。谢谢迪普托,你的回答真的让我松了一口气,我明白了。这个程序花了我很长时间才解决。我被吓坏了。但现在我可以继续了。还有一件事我想问的是,我看到你们在print_列表(current)中发送current,但在main中,我在print_列表(new_节点)中发送new_节点。这并不冲突!我认为,您应该从main调用
print\u list(start)
来打印整个列表。
打印列表(当前)
打印列表(新节点)
都将打印最后一个节点。试着确认一下。嗨,迪普托,我再次需要你的帮助。实际上现在我想先按字母顺序对链表排序,然后按数字升序排序。你能带我去吗?我非常感谢你的帮助。按字母顺序排序是一项复杂的任务,请阅读相关内容,搜索并尝试自己完成。然后,如果你有任何具体的问题,发布一个新的问题。解决那个问题不在这个问题的范围之内。如果这个答案对你有帮助,请接受。好的,我会先试试,然后如果我遇到任何问题,我会问你。我感谢你的帮助。谢谢迪普托,你的回答真的让我松了一口气,我明白了。这个程序花了我很长时间才解决。我被吓坏了。但现在我可以继续了。还有一件事我想问的是,我看到你们在print_列表(current)中发送current,但在main中,我在print_列表(new_节点)中发送new_节点。这并不冲突!我认为,您应该从main调用
print\u list(start)
来打印整个列表。
打印列表(当前)
打印列表(新节点)
都将打印最后一个节点。试着确认一下。嗨,迪普托,我再次需要你的帮助。实际上现在我想先按字母顺序对链表排序,然后按数字升序排序。你能带我去吗?我非常感谢你的帮助。按字母顺序排序是一项复杂的任务,请阅读相关内容,搜索并尝试自己完成。然后,如果你有任何具体的问题,发布一个新的问题。解决那个问题并不重要