插入时就开始比较

    尾插法

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include <stdio.h>
    3. #include<stdlib.h>
    4. #include<math.h>
    5. #include<string.h>
    6. #include<stdbool.h>
    7. typedef struct _student {
    8. char name[10];
    9. char stuNum[20];
    10. struct _student* pnext;
    11. //struct _student* pre=NULL;
    12. } NODE,*pNode;
    13. pNode CreateList(int n) { // 尾插法
    14. int i;
    15. int flag = 1;
    16. pNode last = NULL, pnew = NULL,phead=NULL;
    17. for (i = 0; i < n; i++) {
    18. pnew = (pNode)malloc(sizeof(NODE));
    19. scanf("%s", pnew->name);
    20. scanf("%s", pnew->stuNum);
    21. if (flag) { // 创建头结点
    22. phead = pnew;
    23. last = pnew;
    24. last->pnext = NULL;
    25. flag = 0;
    26. }
    27. else
    28. {
    29. if (pnew != NULL) {
    30. pnew->pnext = NULL;
    31. last->pnext = pnew;
    32. last = pnew;
    33. }
    34. }
    35. }
    36. return phead;
    37. }
    38. pNode duplicate(pNode p) {
    39. //pNode head = p;
    40. pNode tmp = p;
    41. while(tmp!=NULL){
    42. while(p->pnext!=NULL) {
    43. if (strcmp(tmp->stuNum, p->pnext->stuNum) == 0) {
    44. p->pnext = p->pnext->pnext;
    45. }
    46. else
    47. p = p->pnext;
    48. }
    49. tmp = tmp->pnext;
    50. }
    51. return p;
    52. }
    53. void PrintList(pNode p, int pos) {
    54. int i = 0;
    55. //for (i = 0; i < pos; i++)p = p->pnext;
    56. while (p!=NULL)
    57. {
    58. printf("%s %s\n", p->name, p->stuNum);
    59. p = p->pnext;
    60. }
    61. }
    62. int main() {
    63. int n;
    64. scanf("%d", &n);
    65. pNode head = CreateList(n);
    66. duplicate(head);
    67. PrintList(head, n);
    68. }