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

文章/文档

开发平台:

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."<<setw(16)<<"birthday"<<endl;
  52. while(p!=NULL)
  53. {
  54. cout<<setw(11)<<p->name<<setw(4)<<p->sex<<setw(16)<<p->tel<<"  ";
  55. cout<<setw(5)<<p->birthday.year;
  56.         cout<<setw(3)<<p->birthday.month<<"  ";
  57. cout<<setw(2)<<p->birthday.day<<endl;
  58. p=p->next;
  59. }
  60. }
  61. struct friends*DELETE(struct friends*head,char name[])
  62. {
  63. struct friends*p1,*p2;
  64. if(head==NULL)
  65. {
  66. cout<<"list null!DO NOT DELETE!";
  67. return(head);
  68. }
  69. p1=head;
  70. while(strcmp(p1->name,name)!=0 && p1->next!=NULL)
  71. {
  72. p2=p1;
  73. p1=p1->next;
  74. }
  75. if(strcmp(p1->name,name)==0)
  76. {
  77. if(p1==head) head=p1->next;
  78. else p2->next=p1->next;
  79. delete p1;
  80. cout<<"delete:"<<name;
  81. }
  82. else
  83. cout<<name<<"has not been found!"<<endl;
  84. return head;
  85. }
  86. struct friends*insert(struct friends*head,struct friends*frd)
  87. {
  88. struct friends*p0,*p1,*p2;
  89. p1=head;
  90. p0=frd;
  91. if(head==NULL)
  92. {
  93. head=p0; p0->next=NULL;
  94. }
  95. else  
  96. while(strcmp(p0->name,p1->name)>0 && p1->next!=NULL)
  97. {
  98. p2=p1;p1=p1->next;
  99. }
  100. if (strcmp(p0->name,p1->name)<=0 )
  101. {
  102. if(head==p1)
  103. {
  104. head=p0;
  105. p0->next=p1;
  106. }
  107. else
  108. {
  109. p2->next=p0;
  110. p0->next=p1;
  111. }
  112. }
  113. else
  114. {
  115. p1->next=p0;
  116. p0->next=NULL;
  117. }
  118. n++;
  119. return head;
  120. }
  121. void main()
  122. {
  123. char del[10];
  124. friends*s,*t;
  125. s=create();
  126. print(s);
  127. cout<<"Input the name you want deleted:";
  128. cin>>del;
  129. s=DELETE(s,del);
  130. cout<<endl;
  131. print(s);
  132. cout<<"Input the name you want inserted:";
  133. t=new friends;
  134. cin>>t->name>>t->sex>>t->tel>>t->birthday.year>>t->birthday.month>>
  135. t->birthday.day;
  136. s=insert(s,t);
  137. print(s);
  138. delete t;
  139. }