user-0 提问: 链表问题

回复:
user: user-0
content: 我理解错题的意思了

user: user-1
content: #include<stdio.h> #include<stdlib.h>  struct student {     int n;     struct student* next; };  int number;  struct   student  *create_link() //创建未知链表； {     struct student* p, * head,*q;     p = (struct student*)malloc(sizeof(struct student));     head = p;     scanf("%d", &p->n);      for (int i = 0;; i++)     {         q = (student*)malloc(sizeof(student));         p->next = q;         p = q;         scanf("%d", &p->n);          /*            if ((i == 0) && ((k = getchar()) == '\n'))            {         //p = NULL;         p->next = NULL;         break;         }         else if (i == 0)         {         p->n = (int)(k-'0');         q = (struct student*)malloc(sizeof(struct student));         p->next = q;         p = q;         }         else         {         //scanf_s("%d", &(p->n));         scanf("%d", &(p->n));         if ((k = getchar()) == '\n')         {         p->next = NULL;         break;         }         else         {         q = (student*)malloc(sizeof(student));         p->next = q;         p = q;         number++;         }         }         */         if (getchar() == '\n')             break;     }     p->next = NULL;     return head; }  void print_link(struct student *head) {     struct student *p = head;      while (p) {         printf("%d ", p->n);         p = p->next;     }     printf("\n"); }  int search(struct student *phead, int n) {     struct student *p = phead;      while (p) {         if (p->n == n)             return 1;         p = p->next;     }      return 0; }  struct student * intersection(struct student *la, struct student *lb) {     struct student *lc = NULL;     struct student *pa, *pb, *pc, *prev;      pa = la;     pb = lb;      while (pa && pb) {         if (pa->n == pb->n) {             if (search(lc, pa->n)) {                 pa = pa->next;                 pb = pb->next;                 continue;             }             pc = (struct student *)malloc(sizeof(struct student));             if (!pc)                 exit(0);             pc->n = pa->n;             if (!lc) {                 lc = pc;                 prev = pc;             } else {                 prev->next = pc;                 prev = pc;             }          }         if (pa->n > pb->n)             pb = pb->next;         if (pa->n < pb->n)             pa = pa->next;     }     prev->next = NULL;      return lc; }  int main() {     struct student *p, *q, *t;     //void print(struct student * p,struct student * q);//创建链表且为升序；     p = create_link();     //print_link(p);     q = create_link();     //print_link(q);     /*     if ((p == NULL) ||(q == NULL))         printf("NULL");     else         print(p, q);     */     t = intersection(p, q);      print_link(t);     return 0; }  #if 0 void print(struct student* p,struct student* q)  //打印两个链表的交集 {     struct  student* q1 = q, * p1 = p;     for (int i = 0;; i++)     {         if (p->next != NULL)         {             if ((p->n) == (p->next->n))                 p = p->next;         }         for (int j = 0;; j++)         {             if (q->next != NULL)             {                 if ((q->n) == (q->next->n))                     q = q->next;             }             if (p->n == q->n)             {                  printf("%d ", p->n);                 q = q1;                 break;             }             else             {                 if (q->next == NULL)                 {                     q = q1;                     break;                 }                 else                     q = q->next;              }          }         if (p->next == NULL)             break;         else             p = p->next;     } } #endif  供参考～

user: user-0
content: 求两个非降序单链表的交集，例如将单链表1->2->2->3 和 2->2->3->3->5->7 的交集产生新单链表 2->3，只能输出结果，不能修改两个单链表的数据。  【输入形式】  第一行为第一个单链表的各结点值，以单个空格分隔。  第二行为第二个单链表的各结点值，以单个空格分隔。  【输出形式】  交集的单链表，以升序排列(无重复值)，值与值之间以单个空格分隔。