[英]compare list items, and sort in order in c
代碼已編譯,但是,我的代碼中存在邏輯錯誤。 我想比較數組中的字符串,然后在列表中按順序列出它們。我不知道如何在不使用索引的情況下比較列表項,以及如何將當前名稱與下一個名稱進行比較。 任何人都可以幫忙嗎?
#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);
}
}
這段代碼看起來不對:
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;
...然后跳出循環。
第一個主要問題在這里:
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
,因此headptr
tail->next
將為NULL
並且您永遠不會執行該循環。
第二個主要問題在這里:
if(compare_people(ptr->name,tail->name)== 1){
您正在向該函數傳遞一個字符串(record->name 是一個字符串),但在函數本身中您已將其設置為:
int compare_people(Record *a, Record *b)
以記錄(不是char *
)作為輸入。 一旦您解決了第一個問題並實際使用了此功能,這將殺死您。
您可能必須使用雙鏈表(添加指向列表前一條記錄的指針)。 然后對列表的元素進行排序會更容易。 我希望它有幫助。
您的代碼包含許多錯誤。 我無法檢查您的代碼和評論中的所有錯誤。 我試圖修復你的代碼。
#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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.