【发布时间】:2017-03-09 11:51:59
【问题描述】:
这个函数应该在每次 for 循环 for j 运行时打印一个文件的链表。用户输入他们希望排序的数字,例如,如果他们输入“3”,程序将按列表中的第 3 个元素排序。但是,它给出了不正确的输出。
typedef struct Node
{
char **data;
struct Node *next;
}Node;
Node *head = ( Node* )malloc( sizeof( Node ) );
head = NULL; //empty list
void categoryStore( FILE* fpi, int numCols, int numRows, Node *head )
{
void addStart(Node**, char**, int );
void insertionSort( Node**, char**, int, int );
void printLinkedList( Node*, int );
int i = 0, j = 0, ret = 0, userinput;
char *firstLine, *dump, **catstore;
dump = ( char* )malloc( 50*sizeof( char ) );
firstLine = ( char* )malloc( 50*sizeof( char ) );
catstore = ( char** )malloc( 50*numCols*sizeof( char* ) );
do
{
scanf( "%d", &userinput ); /* let the user select the catagory via a number */
if( userinput >= 0 && userinput < numCols)
{
fscanf( fpi,"%s%*[^\n]\n", firstLine );
for( j = 0; j < numRows; j++ )
{
for(i = 0; i <= numCols - 1; i++)
{
catstore[i] = ( char* )malloc( 50*sizeof( char ) );
if( i < numCols - 1 )
{
fscanf( fpi, "%[^,],", catstore[i] );
}
else
{
fscanf( fpi, "%[^\n]\n", catstore[i] );
}
}
if( j == 0 )
{
addStart( &head, catstore, numCols );
printLinkedList( head, numCols );
}
else
{
insertionSort( &head, catstore, userinput, numCols );
printLinkedList( head, numCols );
}
}
}
else if( userinput == numCols )
{
printf( "goodbye\n" );
}
else
{
printf( "Invalid input, please try again\n" );
ret = -1;
}
}while( ret != 0 );
}
/* function to add a node to the start of the linkedlist */
void addStart( Node** head, char **line, int numCols )
{
int i;
Node *temp = ( Node* )malloc( sizeof( Node ) ); //create and temporarily store the address of temp
temp->data = ( char** )malloc( 50*numCols*sizeof( char* ) );
temp->data = line;
for( i = 0; i < numCols; i++ )
{
strcpy( temp->data[i], line[i] );
}
temp->next = *head; //dereferences temp and points the element "next" to what head is pointing to
*head = temp; //now head points to temp
}
/* function to insert addition lines and sort them */
void insertionSort(Node **head, char **line, int select, int numCols)
{
int i;
Node *temp = ( Node* )malloc( sizeof( Node ) );
temp->data = ( char** )malloc( 50*numCols*sizeof( char* ) );
temp->data = line;
for( i = 0; i < numCols; i++ )
{
strcpy( temp->data[i], line[i] );
}
Node *temp1, *temp2;
temp1 = *head;
temp2 = *head;
temp1 = temp1->next;
if( strcmp( temp2->data[select], temp->data[select] ) >= 0 )
{
temp->next = temp2;
*head = temp;
}
else
{
if(temp1->next != NULL)
{
while( strcmp( temp1->data[select], temp->data[select] ) < 0 && temp1->next != NULL )
{
temp1 = temp1->next;
temp2 = temp2->next;
}
if ( temp1->next == NULL && strcmp( temp1->data[select], temp->data[select] ) < 0 )
{
temp->next = NULL;
temp1->next = temp;
}
else
{
temp2->next = temp;
temp->next = temp1;
}
}
else
{
if( strcmp( temp1->data[select], temp->data[select] ) < 0 )
{
temp->next = NULL;
temp1->next = temp;
}
else
{
temp2->next = temp;
temp->next = temp1;
}
}
}
}
/* print the linked list */
void printLinkedList( Node* head, int numCols )
{
while( head != NULL )
{
int i=0;
for (i = 0; i < numCols; i++)
{
printf( "%s", head->data[i] );
}
printf( "\n" );
head = head->next;
}
}
输入文件包含:
姓名(字符串)、年龄(整数)、地址(字符串)、食物(字符串)、颜色(字符串)
马克,21 岁,在家,奶酪,黄色
约旦,19,他的地方,纸杯蛋糕,深蓝色
约翰,40,珠穆朗玛峰,沙拉,红色
杰洛特,100,卡尔莫汉,狼,白
山姆,50 岁,郡,土豆和草莓,绿色
如果输入是 3
输出应该是:
在家里标记 21 奶酪黄色
在家里标记 21 奶酪黄色
乔丹 19 他的地方纸杯蛋糕深蓝色
在家里标记 21 奶酪黄色
乔丹 19 他的地方纸杯蛋糕深蓝色
约翰 40 吨珠穆朗玛峰沙拉红
在家里标记 21 奶酪黄色
乔丹 19 他的地方纸杯蛋糕深蓝色
杰洛特 100 卡尔莫汉狼白
约翰 40 吨珠穆朗玛峰沙拉红
在家里标记 21 奶酪黄色
乔丹 19 他的地方纸杯蛋糕深蓝色
杰洛特 100 卡尔莫汉狼白
约翰 40 吨珠穆朗玛峰沙拉红
山姆 50 郡土豆和草莓绿
目前的输出是:
在家里标记 21 奶酪黄色
乔丹 19 他的地方纸杯蛋糕深蓝色
乔丹 19 他的地方纸杯蛋糕深蓝色
约翰 40 吨珠穆朗玛峰沙拉红
约翰 40 吨珠穆朗玛峰沙拉红
约翰 40 吨珠穆朗玛峰沙拉红
杰洛特 100 卡尔莫汉狼白
杰洛特 100 卡尔莫汉狼白
杰洛特 100 卡尔莫汉狼白
杰洛特 100 卡尔莫汉狼白
山姆 50 郡土豆和草莓绿色
山姆 50 郡土豆和草莓绿色
山姆 50 郡土豆和草莓绿色
山姆 50 郡土豆和草莓绿色
山姆 50 郡土豆和草莓绿
看起来好像每次调用函数时都会读取最后一行,并替换当前在链接列表中的每一行。谁能告诉我为什么会这样。任何帮助将不胜感激!谢谢。
【问题讨论】:
-
这三个原型应该不在
categoryStore函数中,不是吗? (澄清一下,显然这不会解决您的问题)。 -
在
addStart和insertionSort的开头,变量temp->data被赋值了两次,首先是malloc的结果,然后是参数line。当您进行第二次分配时,您将丢失分配内存的引用,因此您以后无法释放它。这肯定会导致内存泄漏。 -
嗨@Milack,感谢您的评论。当我尝试删除
temp->data(第二次出现)时,strcpy 行出现分段错误。 -
@wildplasser 对不起,我忘了在我的结构中添加
Node。它现在位于代码的顶部。 -
哦,我在这里看到了一些可疑点。首先,您要初始化
head两次。我很惊讶您的编译器允许您在函数之外执行此操作。我认为您的意思是Node *head = NULL,删除 malloc。此外,在addStart处,temp->next是一个指针,但您将*head分配给它,这是一个完整的结构。同样,我很惊讶您的编译器允许您在没有警告甚至错误的情况下执行此操作。我想你的意思是temp->next = head。下一行,你正在做相反的事情,用一个指针填充一个结构。我想你的意思是head = temp。类似的考虑适用于insertionSort。
标签: c file sorting linked-list