我编写了一些简单的代码,用于对包含密码的列表以及此密码的使用频率进行排序。问题是,在读取文本文件后,节点没有连接在一起。我不明白为什么。它只保留最后一个值。
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct list_element{
char passwort[100];
int haufigkeit;
struct list_element *next;
} list_element;
typedef struct list{
list_element *first;
list_element *last;
} list;
void init_list(list* mylist)
{
mylist->first=NULL;
mylist->last=NULL;
}
// Diese Funktion fügt Listenelemente am Anfang der Liste an
void insert_front(list_element* le, list* mylist)
{
if(mylist->first == NULL){
le->next = mylist-> first;
mylist->first=le;
mylist->last=le;
printf("%s %d \n",le->passwort, le->haufigkeit);
}
else {
le->next = mylist-> first;
mylist->first= le;
}
// printf("%s %d \n",le->passwort, &le->haufigkeit);
}
// Speicher für Listenelemente wieder freigeben
void free_list(list* mylist)
{
free((mylist)->first);
free(mylist);
mylist=NULL;
}
// Namen, Zahlen Paare in Liste einlesen
void read_data(char* filename, list* mylist)
{
assert(mylist != NULL);
FILE* f=fopen(filename,"rb");
assert(f != NULL);
list_element* temp = malloc(sizeof(list_element));// * Speicher allozieren
while (fscanf(f,"%s %d",temp->passwort, &temp-> haufigkeit) != EOF)
{
fscanf(f,"%s %d",temp->passwort, &temp-> haufigkeit);// * Daten in list_element einlesen
printf("%s %d \n",temp->passwort, temp->haufigkeit);
insert_front(temp, mylist); // * insert_front benutzen um list_element in Liste einzufügen
}
fclose(f);
}
// Pivot finden, das die Liste aufteilt
list_element* partition( list* input, list* left, list* right ){
list_element* pivot= input->first;
printf("hi");
list_element *i;
for(i=input->first; i != NULL; i=i->next){
if ((i -> haufigkeit) < (pivot -> haufigkeit)){
insert_front( i, left);
}
else{
insert_front( i, right);
}
}
return pivot;
}
// Hauptfunktion des quicksort Algorithmus
void qsort_list(list* mylist){
// HIER Code einfügen
// list liste= mylist;
list right;
list left;
init_list(&right);
init_list(&left);
list_element* pivot;
printf("hi11");
if (mylist->last != mylist->first){
printf("d1 \n");
pivot = partition(mylist, &left, &right );
printf("pivot %s %d \n",pivot->passwort, pivot->haufigkeit);
qsort_list(&left);
qsort_list(&right);
/*
if(left.first == NULL){
pivot->next = right.first;
mylist->first = pivot;
mylist->last = right.last;
}
else if(right.first == NULL){
left.last-> next = pivot;
mylist->first = left.first;
mylist->last = pivot;
}
else{
left.last->next=pivot;
pivot->next = right.first;
mylist->first = left.first;
mylist->last = right.last;
}
printf("pivot %s %d \n",pivot->passwort, pivot->haufigkeit);
}
/* mylist->first = left-> first;
mylist->last=right->last;
pivot->next=right->last;
left.last->next=pivot;
pivot->next=right.first;
*/
if(left.first == NULL) {
// Special
left.first = pivot;
mylist->first = left.first;
} else {
// REGULAR
mylist->first = left.first;
left.last->next = pivot;
}
if(right.first == NULL) {
// Special
pivot->next = right.first;
mylist->last = pivot;
} else {
// Regular
pivot->next = right.first;
mylist->last = right.last;
}
}
}
// Liste ausgeben
void print_list(list* mylist){
list_element* current = mylist-> first;
while(current != NULL){
printf("%s %d \n",current->passwort,current->haufigkeit);
current=current->next;
}
}
// Argumente einlesen, Liste kreieren, verarbeiten und ausgeben
int main(int argc, char** args)
{
if (argc != 2)
{
printf("Nutzung: %s <Dateiname>\n",args[0]);
return 1;
}
list mylist;
init_list(&mylist);
read_data(args[1],&mylist);
qsort_list(&mylist);
printf("Sortierte Liste:\n");
print_list(&mylist);
free_list(&mylist);
return 0;
}
列表txt的格式为密码频率&#39;喜欢
daniel 27720
welcome 22204
adobeadobe 27840
superman 24499
7777777 19818
liverpool 18008
princess 28132
1qaz2wsx 22180
答案 0 :(得分:1)
您的问题始于read_data()
:您只为一个list_element
(temp
)分配memofry,并使用您从数据文件中读取的每条记录覆盖其内容。您需要为每个分配单独的list_element
。
答案 1 :(得分:1)
问题是当next不是NULL时,insert_front将不起作用。您最终将列表设置为NULL,这是您所看到的。您也无法知道指向该元素的内容。您的列表结构应包含一个prev点和一个下一个指针。
typedef struct list_element{
char passwort[100];
int haufigkeit;
struct list_element *prev;
struct list_element *next;
} list_element;
以下是程序insert_front方法的更新版本:
void insert_front(list_element* le, list* mylist)
{
if(mylist->first == NULL){
mylist->first=le;
mylist->last=le;
le->prev = NULL;
printf("%s %d \n",le->passwort, le->haufigkeit);
}
else {
if (le->prev != NULL) {
le->prev->next = le->next;
}
le->next = mylist-> first;
mylist->prev = le
mylist->first= le;
}
// printf("%s %d \n",le->passwort, &le->haufigkeit);
}
这应该可以解决您所看到的问题。