#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct linknode { int num; char name[20]; struct linknode *next; }node; node *creat() { node *h = NULL,*s,*t; int d; int i = 1; char name1[20]; while(1) { printf("输入第%d结点的学号,姓名\n",i); scanf("%d%s",&d,&name1); if(d == 0) break; if(i == 1) { h = (node *)malloc(sizeof(node)); h->num = d; strcpy(h->name,name1); h->next = NULL; t = h; } else { s = (node *)malloc(sizeof(node)); s->num = d; strcpy(s->name,name1); s->next = NULL; t->next = s; t = s; } i++; } return h; } void print(node *h) { node *p = h; if(p == NULL) printf("空表\n"); while(p != NULL) { printf("%d %s\n",p->num,p->name); p = p->next; } printf("\n"); } node *link(node *ha,node *hb) { node *r,*head,*s,*p,*q; head =(node *)malloc(sizeof(node)); head->next = NULL; r = head; p = ha; while(p!=NULL) { q = hb; while((q != NULL) && (p->num != q->num)) q = q->next; if(q != NULL) { r->next = p->next; free(p); p = r->next; } else { r->next = p; r = p; p = p->next; r->next = NULL; } } s = head; head = head->next; free(s); return head; } int main() { node *ha,*hb,*s; printf("\n请输入链表a的信息,学号等于零时结束输入:格式(学号 姓名)\n"); ha = creat(); printf("\n链表a的信息为:\n"); print(ha); printf("\n请输入链表b的信息,学号等于零时结束输入:格式(学号 姓名)\n"); hb = creat(); printf("\n链表b的信息为:\n"); print(hb); s = link(ha,hb); printf("\n删除后的链表信息为:\n"); print(s); return 0; }
有两个链表a,b,设结点包括学号,姓名。从a链表中删去与b链表中有相同学号的那些结点。
原文:http://baixu.blog.51cto.com/10798719/1743332