【问题标题】:Reading text file into linked list (error on print sequence)将文本文件读入链表(打印顺序错误)
【发布时间】:2014-01-17 20:47:57
【问题描述】:

我使用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[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;
}
}

}

【问题讨论】:

  • 你的冒泡排序没有排序任何东西,你在哪里比较名字??你不应该像这样改变问题。用冒泡排序发布另一个问题。如果您完全更改问题,那么解决您先前查询的答案会发生什么?使用冒泡排序发布一个新问题并接受解决您问题的答案。如果你不表现出感激之情,没有人会回答你。如果您不知道如何接受,请阅读常见问题页面。
  • 对不起,我实际上是想在另一个部分发帖,但不能很好地工作。但是下次我会尝试按照说明进行操作。谢谢你的建议。

标签: c sorting linked-list bubble-sort


【解决方案1】:

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

strcpy(new_node->name,store);
strcpy(new_node->surname,store);
strcpy(new_node->address,store);
strcpy(new_node->cell,store);

你应该标记你的splitlinecreate_node

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

【讨论】:

  • Hey Dipto,您的回答真的让我感到宽慰和理解。这个程序花了我很长时间才解决。我被吓坏了。但现在我可以继续前进了。我只想问的一件事是,我看到你在 print_list(current) 中发送电流,但在 main 中我在 print_list(new_node) 中发送 new_node。不冲突!
  • 我想,从 main 你应该调用print_list(start) 来打印整个列表。 print_list(current)print_list(new_node) 都将打印最后一个节点。只需尝试一下即可确定。
  • 您好 Dipto,我再次需要您的帮助。实际上,现在我想首先按字母顺序对链表进行排序,然后根据数字升序对链表进行排序。你能带我去吗?非常感谢您的帮助。
  • 按字母排序是一项复杂的任务,请阅读它,搜索并尝试自己完成。那么如果您有任何具体问题,请发布一个新问题。解决这个问题不在这个问题的范围内。如果这个答案对你有帮助,请采纳。
  • 好的,我先试试看,有问题再问你。我感谢您的帮助。谢谢
猜你喜欢
  • 2015-03-22
  • 2020-08-18
  • 2019-03-12
  • 2021-09-25
  • 1970-01-01
  • 2016-07-29
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多