5_43.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:2k
源码类别:

文章/文档

开发平台:

C/C++

  1. # include<iostream.h>
  2. #include <iomanip.h>
  3. # include<string.h>
  4. struct date
  5. {
  6. int year; int month; int day;
  7. };
  8. struct friends
  9. {
  10. char name[10]; char sex;char tel[12]; date birthday;friends* next;
  11. };
  12. int n;  //动态分配结构friends的个数
  13. struct friends* create()   //新建链表,此函数返回一个指向链表头的指针
  14. {
  15. struct friends* head,*p1,*p2;
  16. n=0;
  17. p1=p2=new friends;
  18.     cout<<"Input my friends'name ,with'#' to end:"<<endl;
  19. cin>>p1->name;
  20. cout<<"Continue to input my friends'sex(F/M),tel and birthday:"<<endl;
  21.     cin>>p1->sex;
  22.     cin>>p1->tel;
  23. cin>>p1->birthday.year>>p1->birthday.month>>p1->birthday.day;
  24. head=NULL;
  25.     while (strcmp(p1->name,"#")!=0)
  26. {
  27. n++;
  28. if(n==1)
  29. head=p1;
  30. else
  31. p2->next=p1;
  32. p2=p1;
  33. p1=new friends;
  34. cout<<"Input my friends'name ,with'#' to end:"<<endl;
  35.     cin>>p1->name;
  36. if(strcmp(p1->name,"#")!=0)
  37. {
  38.     cout<<"Continue to input my friends'sex(F/M),tel and birthday:"<<endl;
  39.         cin>>p1->sex;
  40.         cin>>p1->tel;
  41.     cin>>p1->birthday.year>>p1->birthday.month>>p1->birthday.day;
  42. }
  43. }
  44. p2->next=NULL;
  45. return head;
  46. }
  47. void print(struct friends* head)  //输出链表
  48. {
  49. struct friends* p;
  50. p=head;
  51. cout<<setw(11)<<"name"<<setw(5)<<"sex"<<setw(12)<<"telNO."
  52. <<setw(16)<<"birthday"<<endl;
  53. while(p!=NULL)
  54. {
  55. cout<<setw(11)<<p->name<<setw(4)<<p->sex<<setw(16)<<p->tel<<"  ";
  56. cout<<setw(5)<<p->birthday.year;
  57.         cout<<setw(3)<<p->birthday.month<<"  ";
  58. cout<<setw(2)<<p->birthday.day<<endl;
  59. p=p->next;
  60. }
  61. }
  62. void main()
  63. {
  64. friends*s;
  65. s=create();
  66. print(s);
  67. }