#2
林月儿2015-09-23 12:49
程序代码: #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; }node; node* init(node *head) { head=(node *)malloc(sizeof(node)); head->next=NULL; return head; } void input(node *h) { node *p,*q; q=h; printf("输入数据的个数 n : "); int n; scanf("%d",&n); printf("请输入 %d 个有序递增数据:\n",n); for (int i=0;i<n;i++) { printf("第 %d 个: ",i+1); p=(node *)malloc(sizeof(node)); scanf("%d",&p->data); p->next=NULL; q->next=p; q=p; } q=NULL; } void output(node* h) { node* p; p=h->next; printf("输出数据\n"); while(p!=NULL) { printf("%d\t",p->data); p=p->next; } printf("\n"); } void sort(node *head) { node *h=head->next,*last=head; while(last->next!=NULL) { last=last->next; } for(node *r=h;r->next!=NULL;r=r->next) { node *loc=head; while(loc!=last) { loc=loc->next; } last=loc; for(node *c=h;c->next!=NULL&&c!=loc;c=c->next) { if(c->data<c->next->data) { c->data=c->data+c->next->data; c->next->data=c->data-c->next->data; c->data=c->data-c->next->data; } } } } node* combine(node *a,node *b) { node *c=(node *)malloc(sizeof(node)); sort(a); sort(b); node *p=a->next,*q=b->next,*t=c; while(p!=NULL&&q!=NULL) { if(p->data>q->data) { t->next=p; t=t->next; p=p->next; } else { t->next=q; t=t->next; q=q->next; } } while(p!=NULL){t->next=p;t=t->next;p=p->next;} while(q!=NULL){t->next=q;t=t->next;q=q->next;} t=NULL; return c; } int main() { node *a,*b,*c; a=init(a); b=init(b); printf("\n输入链表A :\n"); input(a); printf("\n输入链表B :\n"); input(b); output(a); output(b); printf("输出合并后的链表:\n"); c=combine(a,b); output(c); } [ 本帖最后由 林月儿 于 2015-9-23 12:53 编辑 ] |
刚学C语言,搞不懂链表。。。
假设有两个按元素值递增有序排列的线性表a和b,均以单链表作为存储结构,请编程实现将表a和表b归并成一个按元素值递减有序排列的线性表c(注意:非严格递减,也就是说本题中的数据有可能相等),并要求利用原表的结点空间构造c表。第一行先输入两个小于100的正整数m,n,第二行从小到大的输入m个整数,第三行从小到大的输入n个整数归并这两个序列为一个递减的序列c,用链表存储,之后输出按顺序输出链表c的值,每个数占一行。
输入样例4 3
2 6 6 10
3 10 50
输出样例
50
10
10
6
6
3
2
同学教我,教完我就出去了。。。但我发现输入与输出格式不对,只能分别输入两个链表的内容,不好意思再麻烦他,各位前辈,请问如何将下面的程序改成该题样式?
typedef struct node
{
int data;
struct node *next;
}node,*list;
void init(list &head)
{
head=(list)malloc(sizeof(node));
head->next=NULL;
}
void input(list &h)
{
list p,q;
q=h;
//printf("输入数据的个数 n : ");
int n;
scanf("%d",&n);
//printf("请输入 %d 个有序递增数据:\n",n);
for (int i=0;i<n;i++)
{
// printf("第 %d 个: ",i+1);
p=(list)malloc(sizeof(node));
scanf("%d",&p->data);
p->next=q->next;
q->next=p;
q=p;
}
}
void output(list h)
{
list p;
p=h->next;
//printf("输出数据\n");
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
printf("\n");
}
void combine(list &a,list &b,list &c)
{
list p,q,t;
p=a->next;
q=b->next;
free(b);
b=q;
c=a;
a=p;
c->next=NULL;
while(p&&q)
{
if (p->data<=q->data)
{
a=a->next;
p->next=c->next;
c->next=p;
p=a;
}
else
{
b=q->next;
q->next=c->next;
c->next=q;
q=b;
}
}
if (p!=NULL)
{
while(p)
{
a=a->next;
p->next=c->next;
c->next=p;
p=a;
}
}
if (q!=NULL)
{
while(q)
{
b=q->next;
q->next=c->next;
c->next=q;
q=b;
}
}
}
int main()
{
list a,b,c;
init(a);init(b);
//printf("\n输入链表A :\n");
input(a);
//printf("\n输入链表B :\n");
input(b);
//printf("输出合并后的链表:\n");
combine(a,b,c);
output(c);
}
[ 本帖最后由 天空Sky 于 2015-9-23 10:52 编辑 ]