代码已编译,但我的代码中存在逻辑错误。我想比较数组中的字符串,然后按列表中的顺序列出它们。我无法弄清楚,如何在不使用索引的情况下比较列表项,以及如何将当前名称与下一个名称进行比较。任何人都可以帮忙吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* these arrays are just used to give the parameters to 'insert',
to create the 'people' array */
char names[][10]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
"Harriet"};
int ages[7]= {22, 24, 106, 6, 18, 32, 24};
/* declare your struct for a person here */
typedef struct Record{
char *name;
int age;
struct Record *next;
} Record;
//set the head pointer at the start of the list
Record *headptr = NULL;
int compare_people( Record *a, Record *b)
{
return strcmp((*(Record *)a).name, (*(Record *)b).name);
}
static void insert (Record *p, char *s, int n) {
/* create a new space for the new person */
Record *ptr = ( Record *) malloc(sizeof(Record));
/* check if it is succeeded */
if( ptr == NULL){
abort();
printf("memory allocation fail");
exit(1);
}else{
printf("memory allocation to person - %s - \n", s);
}
//set the data for the new person
ptr->name=s;
ptr->age=n;
ptr->next= NULL;
//ptr= NULL;
//printf("%i", p->age);
/* do not compare when the list is empty*/
if(headptr==NULL)
{
ptr->next=headptr;
headptr=ptr;
printf("ok1\n");
}else{
Record *tail = headptr;
/* go through all the list */
while(tail->next!=NULL)
{
if(compare_people(ptr->name,tail->name)== 1){
tail = tail->next;
}else{
tail->next=headptr;
}
}//while
//tail->next=ptr;
}
}
int main( int argc, char **argv) {
/* declare the people array here */
Record *p=headptr;
headptr = NULL;
//insert the members and age into the unusage array.
for (int i=0; i < 7; i++) {
insert (p,names[i], ages[i]);
/* do not dereference the pointer */
}
/* print out a line before printing the names and ages */
printf("\n");
//set the pointer at the start of the list
p = headptr;
/* print the people array here*/
for ( int i=0; i < 7; i++, p = p->next ) {
printf("The name is: %s, the age is:%i\n", p->name, p->age);
}
/* This is the third loop for call free to release the memory allocated by malloc */
/* the free()function deallocate the space pointed by ptr. */
for( int i=0; i<7; i++){
free(p->next);
}
}
答案 0 :(得分:1)
此代码看起来不正确:
Record *tail =headptr;
/* go through all the list */
while(tail->next!=NULL)
{
if(compare_people(ptr->name,tail->name)== 1){
tail = tail->next;
} else {
tail->next=headptr;
}
} //while
如果您想在tail
之后插入内容,只需设置tail->next = headptr
即可(a)泄漏tail
之后的所有内容,以及(b)将您的链接列表转换为没有结束的循环。
如果要将ptr
插入列表,则应该执行类似
ptr->next = tail->next;
tail->next = ptr;
...然后突破循环。
答案 1 :(得分:1)
第一个主要问题就在这里:
Record *tail =headptr;
/* go through all the list */
while(tail->next!=NULL) {
...
您永远不会进入此while()
循环。在第一次迭代中,您执行了此操作:
ptr->next= NULL; // setting the pointer's next pointer to NULL
...
headptr=ptr; // have headptr point at what ptr is pointing to
这意味着headptr->next
将是NULL
。然后在上面的代码段中,您将tail
设置为headptr
,因此tail->next
将为NULL
,并且您永远不会执行该循环。
第二个主要问题就在这里:
if(compare_people(ptr->name,tail->name)== 1){
您将字符串传递给此函数(记录 - &gt;名称是字符串),但在函数本身中,您将其设置为:
int compare_people(Record *a, Record *b)
记录(不是char *
)作为输入。一旦你解决了第一个问题并实际进入这个功能,这将会杀了你。
答案 2 :(得分:0)
您可能必须使用doubled链式列表(添加指向列表上一个记录的指针)。然后,更容易对列表的元素进行排序。我希望它有所帮助。
答案 3 :(得分:0)
您的代码包含许多错误。我无法回答您的代码和评论的所有错误。我试图修复你的代码。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char names[][10]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
"Harriet"};
int ages[7]= {22, 24, 106, 6, 18, 32, 24};
/* declare your struct for a person here */
typedef struct Record{
char *name;
int age;
struct Record *next;
} Record;
//set the head pointer at the start of the list
Record *headptr = NULL;
int compare_people(char *a, char *b)
{
return strcmp(a, b);
}
void insert (char *s, int n) {
Record *t, *pnew, *prv;
int i;
prv=NULL;
pnew=(Record *)malloc(sizeof(struct Record));
if(pnew == NULL){
abort();
printf("memory allocation fail");
exit(1);
}else{
printf("memory allocation to person - %s - \n", s);
}
pnew->name = s;
pnew->age = n;
pnew->next = NULL;
if (headptr==NULL)
{
headptr = pnew;
return;
}
for (t=headptr;t!=NULL;t=t->next) { // look for the right place to insert in order to get a tri list
if (compare_people(s,t->name)<0) {
pnew->next=t;
if (prv!=NULL)
prv->next = pnew;
else
headptr=pnew;
return;
}
prv=t;
}
prv->next=pnew;
return;
}
int main(int argc, char **argv) {
Record *p, *q;
int i;
for (i=0; i < 7; i++) {
insert (names[i], ages[i]);
}
printf("\n");
for (p = headptr; p!=NULL; p = p->next) {
printf("The name is: %s, the age is:%i\n", p->name, p->age);
}
/* To free your linked list: */
p = headptr;
while (p!=NULL){
q = p;
p = p->next;
free(q);
}
}
执行上述代码的输出:
linux$ ./test
memory allocation to person - Simon -
memory allocation to person - Suzie -
memory allocation to person - Alfred -
memory allocation to person - Chip -
memory allocation to person - John -
memory allocation to person - Tim -
memory allocation to person - Harriet -
The name is: Alfred, the age is:106
The name is: Chip, the age is:6
The name is: Harriet, the age is:24
The name is: John, the age is:18
The name is: Simon, the age is:22
The name is: Suzie, the age is:24
The name is: Tim, the age is:32