为什么搜索功能中的年龄无法打印?

 收藏

我被要求制作一个输入姓名和年龄的哈希表程序。我宣布姓名和年龄为char。在执行搜索功能时,我需要打印名称和年龄,但是不打印年龄。我的代码有什么问题?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

int n;
int count;

struct data
{
    char name [100];
    char age[5];
    struct data *next;
};


struct data *chain[100000];

struct data* insert(char name[], char age[]){
    struct data *curr = (struct data*) malloc (sizeof(data));
    strcpy(curr->name, name);
    strcpy(curr->age, age);
    curr->next = NULL;

    return curr;
};

//hash function
int hashing(char name[]){
    int first = name[0];    
    return first % n;
}

//buat tau di index ke x dia data ke brp 
void add(struct data *newNode){ 
    int hash = hashing(newNode->name);
    printf("index->%d\n", hash);
    if(chain[hash] == NULL){
        chain[hash] = newNode;

    }
    else
    {
        struct data *temp = chain[hash];
        while(temp->next != NULL){
            temp = temp->next;
            //count++;
        }
        count++;
        temp->next = newNode;
        //count++;
    }
    printf("data-> %d\n", count);
}

void view(){

    for(int i = 0 ; i < n ; i ++)
        {
            int count = 0;
            if(chain[i]!=NULL){
                printf("=== index %d data ===\n", i );
                struct data *temp = chain[i];
                while(temp!=NULL){
                    printf("data %d\n", count);
                    printf("name=%s   age=%s\n", temp->name, temp->age);
                    temp = temp->next;
                    count ++;
        }
      }
    }
    printf("\n");
}


void search(int hash){
    char age[5];
    char search[100];
    printf("Search name:");
    scanf(" %[^\n]s", search);
    int key = hashing(search);
    printf("Search data %s from index %d data....\n", search, key);

    if(strcmp(chain[key]->name, search)==0){
            printf("data found at node %d\n", key);
            printf("name = %s     age = %s\n", search, age);
    }
    else
    {
        struct data *temp = chain[key];
     while(temp->next!=NULL){
        if(strcmp(chain[key]->name, search)==0){
            printf("data found at node %d\n", key);
            printf("name = %s     age = %s\n", search, age);

         }
         temp = temp->next;
     }
     if(strcmp(temp->name, search)==0 && temp->next==NULL){
        printf("data found at node %d\n", key);
        printf("name = %s     age = %s\n", search, age);
     }

     else{
        printf("no data found!\n");
     }
    }
}



//void remove(int hash){
//  //int key = item->key;
//  int hash = hashing(key);
//  int option;
//  printf("input option:");
//  scanf("%d", &option);
//  char search[100];
//  printf("delete name:");
//  scanf(" %[^\n]s", search);
//  int first = search[0];
//  int key = first % n;
//  printf("Search data %s from index %d data....\n", search, key);
//  if(chain[key]==hash){
//      chain[key]=-1;
//  }
//  else
//  {
//      printf("try");
//  }
//}

int main(){
    char name[100]; 
    char age[5];


    printf("input the number of hash table: ");
    scanf("%d", &n); getchar();
    struct data *chain[n];
    int chainSize[n]={0};

    printf("\n");


    int option;
    do{
        printf("=== Option Menu ===\n");
        printf("1. insert data\n");
        printf("2. delete data\n");
        printf("3. search data\n");
        printf("4. view data\n");
        printf("5. exit\n");
        printf("input option: ");
        scanf("%d", &option); getchar();

        switch(option){
            case 1:{
                printf("input name: ");
                scanf(" %[^\n]s", name);
                printf("input age: ");
                scanf(" %[^\n]s", age);
                add(insert(name,age));
                printf("\n");
                break;
            }

            case 2:
                {
                //remove();
                break;  
                }
            case 3:
                {
                search(n);
                break;  
            }
            case 4:
                {
                    view();
                    break;
                }

        }
    }while(option!=5);
}

输出是这样的 名字= hansol 年龄=

应该是这样的时候: 名字= hansol 年龄= 23

因此执行搜索功能时不会打印出年龄变量

回复