Ⅰ 如何用c程序建立两个线性表并把他们合并成一个线性表
从键盘输入两个链表,通过程序对他们排序,之后按递增顺序合并链表
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#define NULL 0
struct List
{
int data;
struct List *next;
}List;
struct List * InitList()
{
struct List *head,*p,*q;
int d;
head=(struct List *)malloc(sizeof(struct List));
head->next=NULL;
p=head;
scanf("%d",&d);
while(d>0)
{
q=(struct List *)malloc(sizeof(struct List));
q->next=NULL;
q->data=d;
p->next=q;
p=p->next;
scanf("%d",&d);
}
return head;
}
void ListChooseSort(struct List *head)
{
struct List *p,*q;
int t;
if(head->next!=NULL)
{
for(p=head->next;p->next;p=p->next)
for(q=p->next;q;q=q->next)
if(q->data<p->data)
{
t=p->data;
p->data=q->data;
q->data=t;
}
}
}
struct List * UniteList(struct List *a,struct List *b)
{
struct List *U_head,*p,*q,*s;
U_head=(struct List *)malloc(sizeof(struct List));
U_head->next=NULL;
p=a->next;
q=b->next;
s=U_head;
while(p&&q)
{
if(p->data>q->data)
{
s->next=q;
q=q->next;
}
else
{
s->next=p;
p=p->next;
}
s=s->next;
}
if(p==NULL)s->next=q;
else s->next=p;
return U_head;
}
void ListPrint(struct List *head)
{
struct List *p;
p=head->next;
printf("\n\n");
while(p)
{
printf("%5d",p->data);
p=p->next;
}
printf("\n\n");
}
void main ()
{
struct List *a_head,*b_head,*Unite_head;
printf("\n创建链表a:\n\n");
a_head=InitList();
printf("\n链表a:");
ListPrint(a_head);
system("pause");
system("cls");
printf("\n创建链表b:\n\n");
b_head=InitList();
printf("\n链表b:");
ListPrint(b_head);
system("pause");
system("cls");
ListChooseSort(a_head);
printf("\n递增排序后的链表a:");
ListPrint(a_head);
ListChooseSort(b_head);
printf("递增排序后的链表b:");
ListPrint(b_head);
Unite_head=UniteList(a_head,b_head);
printf("合并后的链表为:");
ListPrint(Unite_head);
}
Ⅱ 求C语言程序代码,合并线性表
#include<stdio.h>
#include<stdlib.h>
#defineMAXSIZE110
typedefstruct
{
intelem[MAXSIZE];
intlen;
}sqlist;
voidmerge(sqlist*la,sqlist*lb,sqlist*lc)
{
inti,j,k;
i=j=k=0;
if(la->len+lb->len<=MAXSIZE)
{
while(i<la->len&&j<lb->len)
{
if(la->elem[i]<lb->elem[j])
{
lc->elem[k]=la->elem[i];
i++;
k++;
}
else
{
lc->elem[k]=lb->elem[j];
j++;
k++;
}
}
while(i<la->len)
{
lc->elem[k]=la->elem[i];
i++;
k++;
}
while(j<lb->len)
{
lc->elem[k]=lb->elem[j];
j++;
k++;
}
lc->len=la->len+lb->len;
}
elseprintf("outofbound");
}
voidprint(sqlist*l)
{
inti;
for(i=0;i<l->len;i++)
{
printf("%d",l->elem[i]);
}
printf(" ");
}
intmain(void)
{
inti;
sqlistla;
sqlistlb;
sqlistlc;
printf("输入线性表LA的元素个数:");
scanf("%d",&la.len);
printf("输入线性表LA的%d个元素:",la.len);
for(i=0;i<la.len;i++)
scanf("%d",&la.elem[i]);
printf("输入线性表LB的元素个数:");
scanf("%d",&lb.len);
printf("输入线性表LB的%d个元素:",lb.len);
for(i=0;i<lb.len;i++)
scanf("%d",&lb.elem[i]);
merge(&la,&lb,&lc);
print(&lc);
system("pause");
return(0);
}
Ⅲ c语言有序线性表合并数据
由于结果保存的表类型不同,
算法也有差别,
我选择结果保存为动态数组的算法,
其它算法可以自己搞.
1,
计算La的元素个数赋值给numOfLa;
计算Lb的元素个数赋值给numOfLb;
2,
分配能够容纳numOfLa+numOfLb个元素的内存空间,
首地址指针为pRes;
3,
设置变量counterLa并赋初值0,
设置变量counterLb并赋初值0,
设置变量counterRes并赋初值0
4,
当counterLa
<
numOfLa
并且
counterLb
<
numOfLb时做以下步:
4.1
如果La[counterLa]
等于Lb[counterLb]时,
pRes[couterRes]赋值为La[couterLa],
couterLa和counterLb
和counterRes三者均加一,
否则
4.2
如果La[counterLa]
小于Lb[counterLb]时,
pRes[couterRes]赋值为La[couterLa],
counterLa和couterRes两者均加一,
否则
4.3
pRes[couterRes]赋值为Lb[couterLb],
counterLb和couterRes两者均加一
5,
如果counterLa等于numOfLa,
则从i
=
couterLb
至i
<
numOfLb做以下:
5.1
pRes[counterRes]
赋值为
Lb[i]
5.2
counterRes和i两者增一;
6,
否则,
从i
=
couterLa
至i
<
numOfLa做以下:
6.1
pRes[counterRes]
赋值为
La[i]
6.2
counterRes和i两者增一;
7结束,
pRes中保存合并的线性表数据,
数据个数为counterRes;
在此基础上,
也可以写出链表的算法
Ⅳ 用C语言实现两个线性链表的归并
以前学数据结构做过一个“非递减的链表合并一一个非递增的链表”
程序如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}LinkList;
/* 建立链表 */
LinkList *create_link(int m)
{
LinkList *head,*s,*p;
int i;
head=(LinkList *)malloc(sizeof(LinkList));
head->next=NULL;
p=head;
for(i=0;i<m;i++){
s=(LinkList *)malloc(sizeof(LinkList));
if(s==NULL){
printf("failed.\n");
exit(0);
}
scanf("%d",&s->data);
s->next=NULL;
p->next=s;
p=s;
}
return head;
}
/* 2个非递减的链表合并一一个非递增的链表 */
LinkList *add_link(LinkList *head1,LinkList *head2,LinkList *head)
{
LinkList *p1,*p2,*q;
p1=head1->next;
p2=head2->next;
head=q=head1;
q->next=NULL;
while(p1&&p2){
if(p1->data<=p2->data){
q=p1;
p1=p1->next;
}
else{
q=p2;
p2=p2->next;
}
q->next=head1->next;
head1->next=q;
}
while(p1)
{q=p1;
p1=p1->next;
q->next=head1->next;
head1->next=q;}
while(p2)
{q=p2;
p2=p2->next;
q->next=head1->next;
head1->next=q;}
return head;
}
/* 打印链表 */
void print_link(LinkList *head)
{
LinkList *p;
p=head->next;
if(!p){
printf("Link is NULL.\n");
exit(0);
}
while(p){
printf("%d ",p->data);
p=p->next;
}
}
int main(void)
{
LinkList *head,*head1,*head2;
int m,n;
printf("input length of Link1:");
scanf("%d",&m);
head=create_link(m);
print_link(head);
printf("\n");
printf("input length of Link2:");
scanf("%d",&n);
head1=create_link(n);
print_link(head1);
printf("\n");
head2=add_link(head,head1,head2);
print_link(head2);
getchar();
getchar();
return 0;
}
Ⅳ 请用算法写出两个有序表合并成一个有序线性表(用C语言
#include "stdio.h" main() { int a,b,c,i,j,k,low,high,mid; int la[50]; int lb[50]; int lc[50]; printf("Please input the width of la,lb,lc:\n"); printf("Input here:"); scanf("%d,%d,%d",&a,&b,&c); printf("a=%d,b=%d,c=%d\n",a,b,c); if (a+b>c) printf("lc overflow,operation halt!\n"); else { printf("Input la:"); for(i=1;i<=a;++i) { scanf("%d",&la[i]); la[0]=la[i]; low=1; high=i; while(low<=high) { mid=(low+high)/2; if(la[0]=low;j--) la[j+1]=la[j]; la[low]=la[0]; } printf("\n"); printf("Input lb:"); for(i=1;i<=b;++i) { scanf("%d",&lb[i]); lb[0]=lb[i]; low=1; high=i; while(low<=high) { mid=(low+high)/2; if(lb[0]=low;j--) lb[j+1]=lb[j]; lb[low]=lb[0]; } printf("\n"); printf("la is:"); for (i=1;i<=a;i++) printf("%d ",la[i]); printf("\n"); printf("lb is:"); for (j=1;j<=b;j++) printf("%d ",lb[j]); i=1;j=1;k=0; while (i<=a && j<=b) if (la[i]>lb[j]) { lc[k]=lb[j];k++;j++;} else { lc[k]=la[i];k++;i++;} while (i<=a) {lc[k]=la[i];k++;i++;} while (j<=b) {lc[k]=lb[j];k++;j++;} printf("\n"); printf("lc is:"); for (i=0;i } getch(); } 把la,lb,lc分别换一下就OK了
希望采纳
Ⅵ 将两个有序线性表合并成一个有序线性表,用C语言 在线急等!!!!
代码写好了。
经测试VC下通过。
#include <stdio.h>
int merge(int *a,int n1,int *b,int n2,int *s)
{
int i=0,j=0,k=0;
while(i<n1&&j<n2)
{
if(a[i]>b[j]){
if(k&&s[k-1]==b[j])//有重复元素
j++;
else
s[k++]=b[j++];
}
else{
if(k&&s[k-1]==a[i])//有重复元素
i++;
else
s[k++]=a[i++];
}
}
while(i<n1){
if(k&&s[k-1]==a[i])
i++;
else
s[k++]=a[i++];
}
while(j<n2){
if(k&&s[k-1]==b[j])//有重复元素
j++;
else
s[k++]=b[j++];
}
return k;
}
int main()
{
int a[100],b[100],s[200],n1,n2,i,n3;
printf("输入第一个数组元素个数:");
scanf("%d",&n1);
printf("输入%d个升序元素(空格隔开):",n1);
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("输入第二个数组元素个数:");
scanf("%d",&n2);
printf("输入%d个升序元素(空格隔开):",n2);
for(i=0;i<n2;i++)
scanf("%d",&b[i]);
n3=merge(a,n1,b,n2,s); //a和b数组的元素合并到s数组里去(并消除重复元素),并返回合并后数组的元素个数
printf("合并后元素数组为:");
for(i=0;i<n3;i++)
printf("%d ",s[i]);
printf("\n");
return 0;
}
附测试数据一组:
5
3 50 50 100 100
3
8 50 80
输出为:3 8 50 80 100
Ⅶ 如何用c语言编合并两个顺序线性表的程序
1、一开始的思路:把A、B都丢进C里,然后对C排序。人们一开始想到的总是最懒的办法,往往是最没效率的。
改进:由于A、B是排好序的,先把A丢进C里,再拿B元素一个个往里查找插入。这么做要频繁移动元素,如果线性表不是链表的话,开销很大。
再改进:从A、B中各拿一个元素出来,比较后把小的放进C里,再从刚才拿出元素的那个表里再拿个元素出来,再比较,把小的放进C里,重复这样的操作,直到A、B其中一个中的元素拿完为止,再把还有剩的元素全丢进C里。
2、例程:
#include<stdlib.h>
/*顺序表存储空间长度的最小值*/
#defineLISTMINSIZE10
/*顺序表存储结构类型定义*/
typedefstruct
{
ListDT*base;/*顺序表空间基地址*/
intlistsize;/*顺序表空间尺寸*/
intlen;/*顺序表长度*/
}SeqList;
/*顺序表初始化*/
voidListInitialize(SeqList*pL,intsize)
{
if(size<LISTMINSIZE)
size=LISTMINSIZE;/*限定不能小于最小尺寸*/
pL->listsize=size;
pL->base=(ListDT*)malloc(pL->listsize*sizeof(ListDT));
if(!pL->base)
exit(EXIT_FAILURE);
pL->len=0;/*初始化空表*/
}
/*按给定的下标取顺序表元素值*/
BOOLListElem(SeqListL,intindex,ListDT*pelem)
{
BOOLflg=TRUE;
if(index<0||index>L.len-1)
flg=FALSE;/*参数越界*/
else
*pelem=L.base[index];
returnflg;
}
/*求顺序表长度*/
intListLen(SeqListL)
{
returnL.len;
}
/*在顺序表中指定序号位置插入元素*/
BOOLListInsert(SeqList*pL,intpos,ListDTd)
{
BOOLflg=TRUE;
inti;
if(pos<0||pL->len>=pL->listsize||pos>pL->len)
flg=FALSE;
else
{
for(i=pL->len-1;i>=pos;i--)/*移动数据*/
pL->base[i+1]=pL->base[i];
pL->base[pos]=d;/*写入数据*/
pL->len++;/*表长增1*/
}
returnflg;
}
/*把顺序表中指定序号的元素删除*/
BOOLListDel(SeqList*pL,intpos)
{
BOOLflg=TRUE;
inti;
if(pos<0||pos>=pL->len)
flg=FALSE;
else
{
for(i=pos+1;i<pL->len;i++)/*移动数据*/
pL->base[i-1]=pL->base[i];
pL->len--;/*表长增1*/
}
returnflg;
}
/*在顺序表中查找元素*/
intListLoc(SeqListL,ListDTd,BOOL(*equal)(ListDT,ListDT))
{
intpos=L.len-1;
while(pos>=0&&!(*equal)(L.base[pos],d))
pos--;
returnpos;
}
/*取前导元素序号位置*/
BOOLListPrior(SeqListL,intpos,int*ppriorpos)
{
BOOLflg=TRUE;
if(pos>0&&pos<L.len)
*ppriorpos=pos-1;
else
flg=FALSE;
returnflg;
}
/*取后继元素序号位置*/
BOOLListNext(SeqListL,intpos,int*pnextpos)
{
BOOLflg=TRUE;
if(pos>=0&&pos<L.len-1)
*pnextpos=pos+1;
else
flg=FALSE;
returnflg;
}
/*销毁顺序表*/
voidListDestroy(SeqListL)
{
free(L.base);
}
#endif
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*
建议性测试用程序
*/
typedefenum{TRUE=1,FALSE=0}BOOL;
typedefintListDT;
#include"seqlist.c"
voidprintSeqList(SeqListL)
{
inti;
ListDTx;
printf(" List: ");
for(i=0;i<ListLen(L);i++)
{
ListElem(L,i,&x);
printf("%3d",x);
}
}
BOOLdataequal(intx,inty)
{
return(x==y)?TRUE:FALSE;
}
#defineN5
voidmain()
{
inti,prior,next;
ListDTx,test[N]={10,20,30,40,50};
SeqListL;
/*初始化顺序表*/
ListInitialize(&L,N);
/*在表头插入N个元素*/
for(i=0;i<N;i++)
ListInsert(&L,0,test[i]);
printSeqList(L);
/*删除元素*/
ListDel(&L,N/2);
printSeqList(L);
printf(" inputakey: ");
scanf("%d",&x);
/*查找x在表中位置*/
i=ListLoc(L,x,dataequal);
/*求x的前导元素*/
if(ListPrior(L,i,&prior))
{
ListElem(L,prior,&x);
printf("Prior:%d ",x);
}
else
printf("noPrior. ");
/*求x的后继*/
if(ListNext(L,i,&next))
{
ListElem(L,next,&x);
printf("Next:%d ",x);
}
else
printf("noNext. ");
/*求表长*/
printf("Listlength=%d",ListLen(L));
/*销毁顺序表*/
ListDestroy(L);
}
Ⅷ 数据结构C语言版,顺序线性表的合并程序。最好有注释
//希望我的回答对你的学习有帮助
#include<stdlib.h>
/*顺序表存储空间长度的最小值*/
#defineLISTMINSIZE10
/*顺序表存储结构类型定义*/
typedefstruct
{
ListDT*base;/*顺序表空间基地址*/
intlistsize;/*顺序表空间尺寸*/
intlen;/*顺序表长度*/
}SeqList;
/*顺序表初始化*/
voidListInitialize(SeqList*pL,intsize)
{
if(size<LISTMINSIZE)
size=LISTMINSIZE;/*限定不能小于最小尺寸*/
pL->listsize=size;
pL->base=(ListDT*)malloc(pL->listsize*sizeof(ListDT));
if(!pL->base)
exit(EXIT_FAILURE);
pL->len=0;/*初始化空表*/
}
/*按给定的下标取顺序表元素值*/
BOOLListElem(SeqListL,intindex,ListDT*pelem)
{
BOOLflg=TRUE;
if(index<0||index>L.len-1)
flg=FALSE;/*参数越界*/
else
*pelem=L.base[index];
returnflg;
}
/*求顺序表长度*/
intListLen(SeqListL)
{
returnL.len;
}
/*在顺序表中指定序号位置插入元素*/
BOOLListInsert(SeqList*pL,intpos,ListDTd)
{
BOOLflg=TRUE;
inti;
if(pos<0||pL->len>=pL->listsize||pos>pL->len)
flg=FALSE;
else
{
for(i=pL->len-1;i>=pos;i--)/*移动数据*/
pL->base[i+1]=pL->base[i];
pL->base[pos]=d;/*写入数据*/
pL->len++;/*表长增1*/
}
returnflg;
}
/*把顺序表中指定序号的元素删除*/
BOOLListDel(SeqList*pL,intpos)
{
BOOLflg=TRUE;
inti;
if(pos<0||pos>=pL->len)
flg=FALSE;
else
{
for(i=pos+1;i<pL->len;i++)/*移动数据*/
pL->base[i-1]=pL->base[i];
pL->len--;/*表长增1*/
}
returnflg;
}
/*在顺序表中查找元素*/
intListLoc(SeqListL,ListDTd,BOOL(*equal)(ListDT,ListDT))
{
intpos=L.len-1;
while(pos>=0&&!(*equal)(L.base[pos],d))
pos--;
returnpos;
}
/*取前导元素序号位置*/
BOOLListPrior(SeqListL,intpos,int*ppriorpos)
{
BOOLflg=TRUE;
if(pos>0&&pos<L.len)
*ppriorpos=pos-1;
else
flg=FALSE;
returnflg;
}
/*取后继元素序号位置*/
BOOLListNext(SeqListL,intpos,int*pnextpos)
{
BOOLflg=TRUE;
if(pos>=0&&pos<L.len-1)
*pnextpos=pos+1;
else
flg=FALSE;
returnflg;
}
/*销毁顺序表*/
voidListDestroy(SeqListL)
{
free(L.base);
}
#endif
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*
建议性测试用程序
*/
typedefenum{TRUE=1,FALSE=0}BOOL;
typedefintListDT;
#include"seqlist.c"
voidprintSeqList(SeqListL)
{
inti;
ListDTx;
printf(" List: ");
for(i=0;i<ListLen(L);i++)
{
ListElem(L,i,&x);
printf("%3d",x);
}
}
BOOLdataequal(intx,inty)
{
return(x==y)?TRUE:FALSE;
}
#defineN5
voidmain()
{
inti,prior,next;
ListDTx,test[N]={10,20,30,40,50};
SeqListL;
/*初始化顺序表*/
ListInitialize(&L,N);
/*在表头插入N个元素*/
for(i=0;i<N;i++)
ListInsert(&L,0,test[i]);
printSeqList(L);
/*删除元素*/
ListDel(&L,N/2);
printSeqList(L);
printf(" inputakey: ");
scanf("%d",&x);
/*查找x在表中位置*/
i=ListLoc(L,x,dataequal);
/*求x的前导元素*/
if(ListPrior(L,i,&prior))
{
ListElem(L,prior,&x);
printf("Prior:%d ",x);
}
else
printf("noPrior. ");
/*求x的后继*/
if(ListNext(L,i,&next))
{
ListElem(L,next,&x);
printf("Next:%d ",x);
}
else
printf("noNext. ");
/*求表长*/
printf("Listlength=%d",ListLen(L));
/*销毁顺序表*/
ListDestroy(L);
}
Ⅸ 如何用c语言编合并两个线性表的程序
最简单的想法是从B中取出一个数,然后插入A中;再从B中取出一个数……,
不过这样做的效率有点低。