C中的链接列表程序

时间:2011-01-19 10:10:19

标签: c linked-list

我正在编写一个简单的链接列表,但面临一个小问题。该程序就像它通过用户接受名称,年龄和DOB,并且它的内存是动态分配的。在从用户获取数据后,它正在搜索用户询问的名称,如果名称存在,则应打印与其相关的所有详细信息。 这是我的代码 -

//function declarations
struct node *initnode(char *, char *, char *);

void add(struct node *);

struct node *printnode(struct node *);

struct node *searchname(struct node *, char *);

struct node {
    char name[25];
    char age[10];
    char dob[10];
    struct node *next;
};

struct node *head = (struct node *) NULL;

struct node *initnode(char *name, char *age, char *dob1)
{
    struct node *ptr;
    ptr = (struct node *) malloc(sizeof(struct node));
    if (ptr == NULL)
        return (struct node *) NULL;
    else {
        strcpy(ptr->name, name);
        strcpy(ptr->age, age);
        strcpy(ptr->dob, dob1);
        ptr->next = NULL;
        return ptr;
    }

}

struct node *printnode(struct node *ptr)
{
    printf("Name -> %s\n", ptr->name);
    printf("age -> %s \n", ptr->age);
    printf("dob ->%s\n", ptr->dob);
    return ptr;
}

void add(struct node *newp)
{
    struct node *temp = (struct node *) malloc(sizeof(struct node));
    if (head == NULL)
        head = newp;
    else {
        for (temp = head; temp->next != NULL; temp = temp->next);
        temp->next = newp;
        temp = newp;
    }
    free(temp);
}

struct node *searchname(struct node *ptr, char *name1)
{

    if (strcmp(name1, ptr->name) == 0) {
        printf("\n name found \n");
        printf("\n details are -\n");
        printnode(ptr);
        return ptr;
    } else {
        printf("\n name not found in the database \n");
    }

}

int main()
{
    char name[25];
    char name1[25];
    char rep;
    char age[10];
    char dob[10];
    int i;
    int flag = 1;
    struct node *ptr;

    do {
        fflush(stdin);
        while (flag != 0) {
            printf("Enter name -- ");
            gets(name);

            for (i = 0; name[i] != '\0'; i++)
                if (isdigit(name[i])) {
                    printf("Error in user input, name should be in alphabets\n");
                    flag = 1;
                    break;
                }

                else
                    flag = 0;
        }

        flag = 1;

        while (flag != 0) {
            printf("Enter age -- ");
            scanf("%s", &age);
            fflush(stdin);

            for (i = 0; age[i] != '\0'; i++)
                if (isalpha(age[i])) {

                    printf("Error in user input, age should be in numbers\n");
                    flag = 1;
                    break;
                } else {
                    flag = 0;
                }
        }

        flag = 1;
        while (flag != 0) {
            printf("Enter dob in DD/MM/YY format-- ");
            scanf("%s", &dob);
            fflush(stdin);

            for (i = 0; dob[i] != '\0'; i++) {
                if (isalpha(dob[i])) {
                    printf("Error in user input, dob should be in numbers\n");
                    flag = 1;
                    break;
                } else
                    flag = 0;

            }

        }

        flag = 1;

        ptr = initnode(name, age, dob);
        add(ptr);

        printf("\n Do you want to continue?<Y/N>:\n ");
        scanf("%s", &rep);
        //rep = getchar();

    }
    while (rep == 'Y' || rep == 'y');

    printf("\n do u want to search for a name in the database? <Y/N>:\n");
    scanf("%s", &rep);

    if (rep == 'Y' || rep == 'y') {
        printf("Enter name you want to search-- ");
        scanf("%s", &name1);

        ptr = searchname(head, name1);
    } else {
        printf("\n goodbye \n");
    }

    do {
        printf("\n do u want to search again? <Y/N>:\n");
        scanf("%s", &rep);

        if (rep == 'Y' || rep == 'y') {

            printf("Enter name you want to search-- ");
            scanf("%s", &name1);

            ptr = searchname(head, name1);
        } else {
            printf("\n goodbye \n");
        }
    }
    while (rep == 'Y' || rep == 'y');
    return 0;

}

问题在于它只搜索第一个条目而不是其他条目,是否有人可以帮我解决这个问题?我正在通过“gcc”进行编译。

2 个答案:

答案 0 :(得分:1)

检查你的添加功能,这是错误的。精神上通过它,看看会发生什么 到指针,指向它指向的内存。

添加功能中使用的malloc和free是什么?

答案 1 :(得分:1)

乍一看,您的搜索功能只是比较一个元素,即列表的头部。

比较一个元素后,您必须转到下一个元素。这可以递归地执行,也可以使用while:

递归使用:

struct node *searchname(struct node *ptr, char *name1)
{
 if (ptr==NULL) //Reached end of the list
 {
  printf("\n name not found in the database \n");
  return NULL;
 }
    if (strcmp(name1, ptr->name) == 0) { //found the element
        printf("\n name found \n");
        printf("\n details are -\n");
        printnode(ptr);
        return ptr;
    } else { //search next element
        return searchname(ptr->next,name1);   //this will call your function again for the next element on the list
    }
}

使用时:

struct node *searchname(struct node *ptr, char *name1)
{
 struct node *aux;  // usually i use an auxiliary pointer in order to avoid unwanted overwrite at my pointer.
 aux = ptr;
 while (aux!=NULL)
 {
     if (strcmp(name1, aux->name) == 0) { //found the element
         printf("\n name found \n");
         printf("\n details are -\n");
         printnode(aux);
         return aux;
     }
  else { //move pointer to next element
    aux=aux->next;
   }
 }
 //if it reaches this point, the element was not found
 printf("\n name not found in the database \n");
 return NULL;
}