有两个链表a和b,设结点中包含学号、姓名。从a链表中删去与b链表中有相同学号的那些结点。

用C++编一个程序

我写过类似的, 你稍微改一下!这个是输入两个整数链表,然后相同的删除,比如一个输入1,2,3,4,0是结束,第二个输入1,2,0那么结果就是3,4!
#include <iostream>
#include <malloc.h>
#include<stdlib.h>

using namespace std;

typedef int elemtype; //定义数据域的类型
typedef struct linknode //定义节点类型
{
elemtype data;
struct linknode *next;
} nodetype;

nodetype *create() //建立单链表,由用户输入各节点data域的值,以0表示输入结束
{
elemtype d;
nodetype *h=NULL,*s,*t;
int i=1;
cout<<"建立一个单链表"<<endl;
while(1)
{
cout<<"输入第"<<i<<"节点的data域值";
cin>>d;
if(d==0)break;
if(i==1)
{
h=(nodetype *)malloc(sizeof(nodetype));
h->data=d;
h->next=NULL;
t=h;
}
else
{
s=(nodetype *)malloc(sizeof(nodetype));
s->data=d;
s->next=NULL;
t->next=s;
t=s;//t一直指向最后一个节点
}
i++;
}
return h;
}
void disp(nodetype *h)
{
nodetype *p=h;
cout<<"输出一个单链表:"<<endl<<" ";
if(p==NULL) cout<<"空表";
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}

nodetype *sub(nodetype *ha,nodetype *hb)
{
nodetype *r,*head,*s,*p,*q;
head =(nodetype *)malloc(sizeof(nodetype));
head->next=NULL;
r = head;
p = ha;
while(p!=NULL)
{
q=hb;
while(q!=NULL&&p->data!=q->data) 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;
}
void main()
{
nodetype *ha,*hb,*s;
ha=create();
disp(ha);
hb=create();
disp(hb);
s=sub(ha,hb);
disp(s);
}
温馨提示:答案为网友推荐,仅供参考
相似回答