#include<stdio.h>
#include<stdlib.h>
typedef struct _List{
int data;
struct _List* next;
}*Node,List;
void SoloDelete(Node a);//删除链表
void Insert_1(Node a,int n);//在a后插入一个新节点,数据域为n
Node CreatList();//创建一个链表
void PrintList(Node a);//输出链表储存的值
void Combine(Node a,Node b);//将b中不属于a的节点插入a
int main(){
List *ha = CreatList();
List *hb = CreatList();
printf("creat\n");
Combine( ha, hb);
PrintList(ha);
SoloDelete(ha);
SoloDelete(hb);
int i,j;
printf("你想创建多长的链表?请输入:\n");//创建
scanf("%d",&j);
for( i = 0; i <3 ;++i){
printf("请输入储存在此节点的数据\n");
scanf("%d",&j);
}
printf("你想创建多长的链表?请输入:\n");//创建
scanf("%d",&i);
for( i = 0; i <3 ;++i){
printf("请输入储存在此节点的数据\n");
scanf("%d",&j);
}
printf("合并后的ha为:1 6 5 4 2 3\n");
return 0;
}
void Insert_1(Node a,int n){//在a后插入一个新节点,数据域为n
Node c;
c = (Node)malloc(sizeof(List));
if(a->next){
Node b;
b = (Node)malloc(sizeof(List));
b = a->next;
a->next = c;
c->next = b;
b->next = NULL;
}
else {
a->next = c;
c->next = NULL;
c->data = n;
}
}
void SoloDelete(Node a){//删除一个节点
while(a->next){
Node c = a->next;
free(a);
a = c;
}
}
Node CreatList(){
int i = 0;
int j = 0;
int n = 0;
List* ha;
ha = (Node)malloc(sizeof(List));
printf("你想创建多长的链表?请输入:\n");//创建
scanf("%d",&i);
for( j = 0 ; j<i ; ++j){
printf("请输入储存在此节点的数据\n");
scanf("%d",&n);
Insert_1(ha,n);
}
return ha;
}
void PrintList(Node a){
printf("print\n");
List* b;
b = a;
while ( b ) {
b = b->next;
printf( "%d ", b->data);
}
}
void Combine(Node a,Node b){
//printf("combine\n");
List* c;
List* d;
c = b->next;
d = a->next;
int status = 0;
while(d->next){//将hb中不属于ha的节点插入ha
//printf("while\n");
status = 0;
while(c->next){
printf("ehilr\n");
if(d->data==c->data){
status = 1;
break;
}
c = c->next;
}
if(status==0){
Insert_1(a,c->data);
}
d = d->next;
}
//printf("dfsfs\n");
}
#include<stdio.h>
#include<stdlib.h>
typedef struct _List{
int data;
struct _List* next;
}*Node,List;
void SoloDelete(Node a);//删除链表
void Insert_1(Node a,int n);//在a后插入一个新节点,数据域为n
Node CreatList();//创建一个链表
void PrintList(Node a);//输出链表储存的值
void Combine(Node a,Node b);//将b中不属于a的节点插入a
int main(){
List *ha = CreatList();
List *hb = CreatList();
printf("creat\n");
Combine( ha, hb);
PrintList(ha);
SoloDelete(ha);
SoloDelete(hb);
return 0;
}
void Insert_1(Node a,int n){//在a后插入一个新节点,数据域为n
Node c;
c = (Node)malloc(sizeof(List));
if(a->next){
c->data=n;
c->next = a->next;
a->next=c;
}
else {
a->next = c;
c->next = NULL;
c->data = n;
}
}
void SoloDelete(Node a){//删除一个节点
while(a->next){
Node c = a->next;
free(a);
a = c;
}
}
Node CreatList(){
int i = 0;
int j = 0;
int n = 0;
List* ha;
ha = (Node)malloc(sizeof(List));
printf("你想创建多长的链表?请输入:\n");//创建
scanf("%d",&i);
for( j = 0 ; j<i ; ++j){
printf("请输入储存在此节点的数据\n");
scanf("%d",&n);
Insert_1(ha,n);
}
return ha;
}
void PrintList(Node a){
printf("print\n");
List* b;
b = a->next;
while ( b) {
printf( "%d ", b->data);
b=b->next;
}
}
void Combine(Node a,Node b){
//printf("combine\n");
List* c;
List* d;
int f[100],e[100];
int k=0,m=0,t=0,j=0;
c=b->next;
while (c)
{
f[m]=c->data;
m++;
c=c->next;
}
for (k=0;k<m;k++)
{
d=a->next;
while (d)
{
if (f[k]==d->data)
break;
d=d->next;
}
if (d==NULL)
{
e[j]=f[k];
j++;
}
}
/*
int status = 0;
while(d){//将hb中不属于ha的节点插入ha
c=b->next;
printf("ehilr\n");
while(c){
status=0;
if(d->data==c->data){
status = 1;
}
if(status==0){
for (k=0;k<m;k++)
{
if(f[k]==c->data)
break;
}
if(k==m)
{
f[k]=c->data;
m++;
}
for (k=0;k<m;k++)
{
if (f[k]==d->data)
{
for (t=k;t<m-1;t++)
f[t]=f[t+1];
m--;
k--;
}
}
}
c = c->next;
}
d = d->next;
}*/
for(k=0;k<j;k++)
printf("%d ",e[k]);
printf("\n");
for (k=0;k<j;k++)
Insert_1(a,e[k]);
//printf("dfsfs\n");
}
不知道我的这个你满意么
可以利用vc++这个软件的调试功能,你可以下载一个
printf("你想创建多长的链表?请输入:\n");//创建
scanf("%d",&j);
for( i = 0; i <3 ;++i){
printf("请输入储存在此节点的数据\n");
scanf("%d",&j);
}
printf("你想创建多长的链表?请输入:\n");//创建
scanf("%d",&i);
for( i = 0; i <3 ;++i){
printf("请输入储存在此节点的数据\n");
scanf("%d",&j);
}
这里重复了吧?