C 如何对名称的链接列表进行气泡排序?
我需要帮助创建一个函数,该函数对名称的链接列表进行冒泡排序。提前谢谢你的帮助C 如何对名称的链接列表进行气泡排序?,c,string,linked-list,scanf,bubble-sort,C,String,Linked List,Scanf,Bubble Sort,我需要帮助创建一个函数,该函数对名称的链接列表进行冒泡排序。提前谢谢你的帮助 #include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<string.h> #define MAX_STR_LEN 25 typedef struct Data_ { char *name; struct Data_ *next; }Data; Data* bubble_sort
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define MAX_STR_LEN 25
typedef struct Data_ {
char *name;
struct Data_ *next;
}Data;
Data* bubble_sort(Data *list);
Data* read_from_file(const char* file, const int size);
void display(Data *list);
void push(Data **head, char *name);
int main(int argc, char **argv){
if(argc != 2){
printf("Not enough parameters!");
exit(0);
}
Data *head = NULL;
int size = 10;
head = read_from_file(argv[1], size);
printf("\nBubble Sort\n");
head = bubble_sort(head);
display(head);
}
Data *bubble_sort(Data *list){
}
void push(Data **head, char *name){
Data *temp = malloc(sizeof(Data*));
temp->name = name;
temp->next = *head;
*head = temp;
}
Data* read_from_file(const char* file, const int size){
FILE *input;
input = fopen(file, "r");
Data *new_ = (Data*)malloc(sizeof(Data*));
new_->next = NULL;
int i;
char name[MAX_STR_LEN];
for(i = 0; i < size; i++){
fscanf(input, "%25s", &name);
push(&new_, name);
}
return new_;
}
void display(Data *list){
Data *current = list;
while(current){
printf("\n%s", current->name);
current = current->next;
}
}
#包括
#包括
#包括
#包括
#定义最大长度25
类型定义结构数据{
字符*名称;
结构数据*next;
}数据;
数据*气泡_排序(数据*列表);
数据*从文件读取(常量字符*文件,常量整数大小);
作废显示(数据*列表);
作废推送(数据**头,字符*名称);
int main(int argc,字符**argv){
如果(argc!=2){
printf(“参数不足!”);
出口(0);
}
数据*头=空;
int size=10;
head=从_文件读取_(argv[1],大小);
printf(“\n气泡排序\n”);
头部=气泡_排序(头部);
显示器(头部);
}
数据*气泡_排序(数据*列表){
}
无效推送(数据**头,字符*名称){
数据*温度=malloc(sizeof(数据*);
临时->名称=名称;
温度->下一步=*头部;
*压头=温度;
}
数据*从文件读取(常量字符*文件,常量整数大小){
文件*输入;
输入=fopen(文件“r”);
数据*new=(数据*)malloc(sizeof(数据*);
新建->下一步=空;
int i;
字符名[MAX_STR_LEN];
对于(i=0;i名称);
当前=当前->下一步;
}
}
我要读取的名称文件名为names.txt,如下所示:
德里克
德鲁
兰德尔
特雷尔
卡门
科林
涡流
巴勃罗
拉蒙特
德克斯特
提前感谢您的帮助。您需要为所有这些名称预留空间,您可以使用
strdup()
:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define MAX_STR_LEN 25
typedef struct Data_ {
char *name;
struct Data_ *next;
}Data;
Data* bubble_sort(Data *list);
Data* read_from_file(const char* file, const int size);
void display(Data *list);
void push(Data **head, char *name);
int main(int argc, char **argv){
if(argc != 2){
printf("Not enough parameters!");
exit(0);
}
Data *head = NULL;
int size = 10;
head = read_from_file(argv[1], size);
printf("\nBubble Sort\n");
head = bubble_sort(head);
display(head);
}
Data *bubble_sort(Data *list){
}
void push(Data **head, char *name){
Data *temp = malloc(sizeof(Data*));
temp->name = name;
temp->next = *head;
*head = temp;
}
Data* read_from_file(const char* file, const int size){
FILE *input;
input = fopen(file, "r");
Data *new_ = (Data*)malloc(sizeof(Data*));
new_->next = NULL;
int i;
char name[MAX_STR_LEN];
for(i = 0; i < size; i++){
fscanf(input, "%25s", &name);
push(&new_, name);
}
return new_;
}
void display(Data *list){
Data *current = list;
while(current){
printf("\n%s", current->name);
current = current->next;
}
}
或malloc
和strcpy
:
void push(Data **head, char *name){
Data *temp = malloc(sizeof(Data*));
temp->name = malloc(strlen(name) + 1);
strcpy(temp->name, name);
temp->next = *head;
*head = temp;
}
不要忘了在结尾时释放
还要注意,您将错误的大小传递给malloc
:
Data *temp = malloc(sizeof(Data*));
一定是
Data *temp = malloc(sizeof(Data));
或
如果您在某些unix下,valgrind和gdb是检测此类问题的优秀工具。1)
Data*temp=malloc(sizeof(Data*)代码>-->Data*temp=malloc(sizeof(Data))代码>同上。2) push(&new,name)代码>:名称是本地值。3)fscanf(输入,“%25s”,&name)代码>-->fscanf(输入,“%24s”,名称)代码>该代码一次最多读取10个字符,但这仅适用于当前文件。什么是长度大于9个字符的名称?关于此行:Data temp=malloc(sizeof(Data));这总是只分配4个字节(指针的大小),因此将行更改为:Data*temp=malloc(sizeof(Data));由于结构数据的长度为32字节,因此malloc的长度需要为32字节,这一行是:fscanf(输入,“%25s”,&name);name已经是char数组的地址,因此它不应该有前导“&”
Data *temp = malloc(sizeof(*temp));