不开辟新空间的并集和交集
并集如下
void unionlink(NODE A,NODE B)
{
NODE q,d;
int c = 0;
NODE p1 = A->next;
NODE p2 = B->next;
A->next = NULL;//不开辟新链表 直接将A改成新链表
while(p2!=NULL)//先将p2 插入一个链表中; 然后将p2与p1不同的再插入
{
d = p2;
p2 = p2->next;
d->next= A->next;
A->next = d;
}
while(p1 != NULL)
{
p2 = A->next;//重置p2位置
//因为头插入A链表的时候p2在进入循环之前就在A链表的最后一个节点了
//所以不能 p2 = B->next; 因为B链表后面的节点让拿走了完 但是B的下一个节点还是p2 所以这样的赋值没有意义
while(p2!= NULL)
{
if(p1->data == p2->data)
{
c = 1;
}
p2 = p2->next;
}
q = p1;//因为q的节点和p1的节点是一个地址
p1 = p1->next;//所以如果这一步不跳到下一个节点 q的操作会影响 p1节点
if( c == 0)
{
q->next = A->next;
A->next = q;
}
c = 0; //重新赋值为0
}
print(A);
}
交集如下
void interlink(NODE A,NODE B)
{
int c = 0;
NODE q;
NODE p1 = A->next;
A->next = NULL;
while(p1 != NULL)
{
NODE p2 = B->next;//重置p2位置
while(p2 != NULL)
{
if(p1->data == p2->data)
{
c = 1;
}
p2 = p2->next;
}
q = p1;
p1 = p1->next;
if( c==1)
{
q->next = A->next;
A->next = q;
}
c = 0;
}
print(A);
}
开辟新空间的交集和并集
并集如下
void kunionlink(NODE A,NODE B)
{
NODE q;
int c = 0;
NODE p1 = A->next;
NODE p2 = B->next;
NODE C = createlist();
NODE M = C;
while( p2 != NULL)
{
M->next = (NODE)malloc(sizeof(lnode));
M = M->next;
M->data = p2->data;
p2 = p2->next;
}
while(p1 != NULL)
{
p2= B->next;
while(p2 != NULL)
{
if( p1->data == p2->data)
{
c = 1;
}
p2 = p2->next;
}
if(c == 0)
{
M->next = (NODE)malloc(sizeof(lnode));
M = M->next;
M->data = p1->data;
}
c = 0;
p1 = p1->next;
}
M->next = NULL;
print(C);
}
交集如下
void kinterlink(NODE A,NODE B)
{
NODE q;
int c = 0;
NODE p1 = A->next;
NODE p2 = B->next;
NODE C = createlist();//如果想要定义一个新的链表需要申请一个新的内存空间
NODE M = C;//定义新链表的移动指针
//新链表的话不会影响原链表
while(p1 != NULL)
{
p2 = B->next;//重置
while(p2 != NULL)
{
if(p1->data == p2->data)
{
c = 1;
}
p2 = p2->next;
}
if(c == 1)
{
M->next = (NODE)malloc(sizeof(lnode));
M = M->next;
M->data = p1->data;
}
c = 0;
p1 = p1->next;
}
M->next = NULL;
print(C);
}
完整代码
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
struct node *next;
int data;
}lnode,*NODE;
void print(NODE L)
{
NODE pmove = L->next;
printf("\n");
while(pmove != NULL)
{
printf("%d\n",pmove->data);
pmove = pmove->next;
}
}
NODE createlist()
{
NODE headnode = (NODE)malloc(sizeof(lnode));
headnode->next = NULL;
return headnode;
}
void backinsert(NODE L)
{
NODE p = L;
int x;
scanf("%d",&x);
while(x != 0)
{
p->next = (NODE)malloc(sizeof(lnode));
p = p->next;
p->data = x;
scanf("%d",&x);
}
p->next = NULL;
}
void unionlink(NODE A,NODE B)
void interlink(NODE A,NODE B)
void kunionlink(NODE A,NODE B)
void kinterlink(NODE A,NODE B)
int main()
{
NODE A = createlist();
NODE B = createlist();
printf("请输入A的数据\n");
backinsert(A);
printf("\n请输入B的数据\n");
backinsert(B);
printf("\n");
printf("1-不开辟\n2-开辟");
int x,c;
printf("\n请输入操作");scanf("%d",&x);
switch(x)
{
case 1:
printf("\n1-并集\n2-交集");
printf("\n请输入操作");scanf("%d",&c);
switch(c)
{
case 1:unionlink(A,B);break;
case 2:interlink(A,B);break;
} break;
case 2: printf("\n1-并集\n2-交集");
printf("\n请输入操作");scanf("%d",&c);
switch(c)
{
case 1:kunionlink(A,B);break;
case 2:kinterlink(A,B);break;
} break;