解题思路:
注意事项:
参考代码:
#include <stdio.h>
#include <stdlib.h>
#define len sizeof(num)
typedef struct s{
int data;
struct s *next;
}num;
num *creat(int n){ //创建链表
num *p,*h,*t;
t=h=NULL;
for (int i=0;i<n;i++){
p=(num *)malloc(len);
scanf ("%d",&p->data);
p->next=NULL;
if (h==NULL){
h=p;
}else{
t->next=p;
}
t=p;
}
return h;
}
int find(num *h,int n){ //查找是否有相同的元素
num *i;
for (i=h;i;i=i->next){
if (i->data==n){
return 1;
}
}
return 0;
}
num *insert(num *h,int n){ //链表尾部插入
num *i;
num *p;
p=(num *)malloc(len);
p->data=n;
p->next=NULL;
for (i=h;i->next;i=i->next){
;
}
i->next=p;
return h;
}
void print(num *h){ //打印链表
num *i;
for (i=h;i;i=i->next){
printf ("%d ",i->data);
}
printf ("\n");
}
int main (){
num *i;
num *h,*t;
int n,m;
while (scanf ("%d",&n)!=EOF){
h=creat(n);
print(h);
scanf ("%d",&m);
t=creat(m);
print(t);
while (t){
if (find(h,t->data)==0){
h=insert(h,t->data);
}
t=t->next;
print(h);
}
printf ("\n");
}
}
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题10.4 (C语言代码)浏览:675 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:377 |
WU-整数平均值 (C++代码)浏览:1244 |
C语言程序设计教程(第三版)课后习题10.4 (C语言代码)浏览:886 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:529 |
A+B for Input-Output Practice (V) (C语言代码)浏览:468 |
1048题解(读入回车问题)浏览:565 |
复数求和 (C语言代码)浏览:927 |
简单的a+b (C语言代码)浏览:541 |
简单的a+b (C语言代码)浏览:512 |