大一狗遇到了一个问题,请大手帮忙看看!!!!

  1. #include
  2. #include
  3. #include
  4. struct Node
  5. {
  6. int num;
  7. char name[10];
  8. char sex;
  9. int phone_number;
  10. char addr[31];
  11. struct Node *pnext;
  12. struct Node *pfront;
  13. };
  14. struct Node * creat()
  15. {
  16. struct Node *head,*temp,*tail=0;
  17. int numtemp;
  18. printf("请输入编号姓名性别电话号码和住址,编号为0退出:\n");
  19. scanf("%d",&numtemp);
  20. while(numtemp>0)
  21. {
  22. temp=(struct Node *)malloc(sizeof(struct Node));
  23. temp->num=numtemp;
  24. scanf("%s%c%d%s",temp->name,&temp->sex,&temp->phone_number,temp->addr);
  25. if(!head)
  26. {
  27. head=tail=temp;
  28. }
  29. else
  30. {
  31. tail->pnext=temp;
  32. temp->pfront=tail;
  33. tail=temp;
  34. }
  35. printf("请输入编号姓名性别电话号码和住址,编号为0退出\n");
  36. scanf("%d",&numtemp);
  37. }
  38. return head;
  39. }
  40. void getin(struct Node *head)
  41. {
  42. struct Node *p;
  43. struct Node *temp=head;
  44. p=(struct Node *)malloc(sizeof(struct Node));
  45. printf("输入新添加联系人的编号姓名性别电话号码和住址");
  46. scanf("%d%s%c%d%s",&p->num,&p->name,&p->sex,&p->phone_number,&p->addr);
  47. while(temp->pnext!=NULL)
  48. {
  49. if((temp->num)>(p->num))
  50. {
  51. p->pfront=temp->pfront;
  52. temp->pfront->pnext=p;
  53. p->pnext=temp;
  54. temp->pfront=p;
  55. }
  56. else
  57. temp=temp->pnext;
  58. }
  59. if(p->pnext==NULL)
  60. {
  61. temp->pnext=p;
  62. p->pfront=temp;
  63. }
  64. }
  65. void put(struct Node * head)
  66. {
  67. struct Node *temp=head;
  68. while(temp->pnext!=NULL)
  69. {
  70. printf("%d\t%s\t%c\t%d\t%s\t",temp->num,temp->name,temp->sex,temp->phone_number,temp->addr);
  71. temp=temp->pnext;
  72. printf("\n");
  73. }
  74. printf("%d\t%s\t%c\t%d\t%s\t",temp->num,temp->name,temp->sex,temp->phone_number,temp->addr);
  75. }
  76. struct Node * serch(struct Node *head,char *names)
  77. {
  78. while(head!=NULL)
  79. {
  80. if(strcmp(head->name,names)==0)
  81. return head;
  82. else
  83. head=head->pnext;
  84. }
  85. printf("没有找到相关联系人");
  86. return 0;
  87. }
  88. void dele(struct Node *head,char *names)
  89. {
  90. while(head!=NULL)
  91. {
  92. if(strcmp(head->name,names)==0)
  93. {
  94. head->pfront->pnext=head->pnext;
  95. head->pnext->pfront=head->pfront;
  96. free(head);
  97. break;
  98. }
  99. else
  100. head=head->pnext;
  101. }
  102. }
  103. int main()
  104. {
  105. struct Node *head;
  106. int n;
  107. char names[20];
  108. char names2[20];
  109. struct Node *p;
  110. printf("创建通讯录请输入1,插入输入2,删除输入3,输出输入4,查找输入5,退出输入0\n");
  111. scanf("%d",&n);
  112. while(n!=0)
  113. {
  114. switch(n)
  115. {
  116. case 1:
  117. head=creat();
  118. break;
  119. case 2:
  120. getin(head);
  121. break;
  122. case 3:
  123. printf("请输入要删除的联系人姓名\n");
  124. gets(names);
  125. dele(head,names);
  126. break;
  127. case 4:
  128. put(head);
  129. break;
  130. case 5:
  131. printf("请输入要查找联系人的姓名\n");
  132. gets(names2);
  133. p=serch(head,names2);
  134. printf("%d\t%s\t%c\t%d\t%s",p->num,p->name,p->sex,p->phone_number,p->addr);
  135. break;
  136. case 0:
  137. while(head!=NULL)
  138. {
  139. struct Node *p=head;
  140. head=head->pnext;
  141. free(p);
  142. }
  143. return 0;
  144. }
  145. }
  146. return 0;
  147. }

代码太长了,直接说你遇到了什么问题吧。

请输入编号姓名性别电话号码和住址,编号为0退出;
我输入了编号姓名性别,
程序就直接结束了,返回了一个乱七八糟的数

图片说明

p=(struct Node *)malloc(sizeof(struct Node));
只看到你分配了一次内存,后面循环都没有再分配过

你先调试下,估计是内存分配问题,你仔细看下,我长期用C++,对C不熟了。最后直接说你遇到的问题,你这样提问题,也不好解决

scanf的时候,格式加空格试试。 scanf("%s %c %d %s", ...)
看到没,那个%d、%c、%d、%s之间加上空格。