Ⅰ 如何用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中取出一個數……,
不過這樣做的效率有點低。