chainsearch.c
上传用户:bjtelijie
上传日期:2010-01-01
资源大小:87k
文件大小:2k
源码类别:

数学计算

开发平台:

Visual C++

  1. #include <stdio.h>
  2. #include <stdlib.h> 
  3. #include <string.h>
  4. #define NUM 4
  5. struct chain
  6. {
  7. char name[20];
  8. char city[20];
  9. char sex[10];
  10. char age[10];
  11. char job[10];
  12. struct chain *next;
  13. };
  14. struct chain *create();
  15. struct chain *SequelSeach(head,name);
  16. void print_data(point);
  17. struct chain Datas[NUM]=
  18. {
  19. "Sun","Weifang","Male","24","student",NULL,
  20. "Tom","Beijing","Male","31","doctor",NULL,
  21. "Marry","Shanghai","Female","19","techer",NULL,
  22. "Willing","Tianjing","Female","21","worker",NULL
  23. };
  24. int main(void)
  25. {
  26. struct chain *head;
  27. struct chain *p;
  28. char name[30];
  29. head = create();
  30. printf("请输入要查找的人名n");
  31. scanf("%s",name);
  32. p = SequelSeach(head,name);
  33. print_data(p);
  34. return 0;
  35. }
  36. struct chain *create()
  37. {
  38. struct chain *head, *tail, *p;
  39. int i;
  40. head = tail = NULL;
  41. printf("将名单数据输入到链表中:n");
  42. for(i= 0;i < NUM; i++)
  43. {
  44. p = (struct chain *)malloc (sizeof (struct chain));
  45. strcpy(p->name, Datas[i].name);
  46. strcpy(p->city,Datas[i].city);
  47. strcpy(p->sex,Datas[i].sex);
  48. strcpy(p->age,Datas[i].age);
  49. strcpy(p->job,Datas[i].job);
  50. p->next = NULL;
  51. if(head == NULL)
  52. head = tail = p;
  53. else 
  54. tail = tail ->next;
  55. tail ->next = p;
  56. }
  57. return head;
  58. }
  59. struct chain *SequelSeach(head,name)
  60. struct chain *head;
  61. char name[];
  62. {
  63. struct chain *temp;
  64. temp = head;
  65. for(temp=head;temp!=NULL;)
  66. {
  67. if(strcmp(temp->name,name) == 0)
  68. break;
  69. else
  70. temp = temp->next;
  71. }
  72. if(temp ==NULL)
  73. printf("没有查找到该人资料n");
  74. return temp;
  75. }
  76. void print_data(point)
  77. struct chain *point;
  78. {
  79. if(point ==NULL)
  80. return;
  81. printf("查找结果:n");
  82. printf(" 姓名:%sn",point->name);
  83. printf(" 城市:%sn",point->city);
  84. printf(" 性别:%sn",point->sex);
  85. printf(" 年龄:%sn",point->age);
  86. printf(" 工作:%sn",point->job);
  87. }