C語言中數(shù)據(jù)結(jié)構(gòu)之鏈表歸并排序?qū)嵗a
C語言中數(shù)據(jù)結(jié)構(gòu)之鏈表歸并排序?qū)嵗a
問題
設(shè)有兩個(gè)無頭結(jié)點(diǎn)的單鏈表,頭指針分別為ha,hb,鏈中有數(shù)據(jù)域data,鏈域next,兩鏈表的數(shù)據(jù)都按遞增排序存放,現(xiàn)要求將hb表歸到ha表中,且歸并后ha仍遞增序,歸并中ha表中已有的數(shù)據(jù)若hb中也有,則hb中的數(shù)據(jù)不歸并到ha中,hb的鏈表在算法中不允許破壞。
源程序
#include <stdio.h>
#include<stdlib.h>
#define N1 6 /*鏈表La的長度*/
#define N2 6 /*鏈表Lb的長度*/
struct listnode
{
int data;
struct listnode *next;
};
void createlist(struct listnode * *,int);
void listinsert(struct listnode * *,struct listnode * *);
void readlist(struct listnode *);
int main()
{
struct listnode *ha=NULL,*hb=NULL;
printf("請(qǐng)按照升序序列輸入以下數(shù)字以建立鏈表La\n");
printf("Please Input %d numbers:",N1);
createlist(&ha,N1);
printf("請(qǐng)按照升序序列輸入以下數(shù)字以建立鏈表Lb\n");
printf("Please Input %d numbers:",N2);
createlist(&hb,N2);
listinsert(&ha,&hb);
readlist(ha);
printf("\n");
}
void createlist(struct listnode * *p,int n)
{ /*尾插法建立鏈表*/
struct listnode *t,*q;
int i;
t=(struct listnode *)malloc(sizeof(struct listnode));
scanf("%d",&t->data);
*p=t;
q=t;
for(i=n-1;i>0;i--)
{
t=(struct listnode *)malloc(sizeof(struct listnode));
scanf("%d",&t->data);
q->next=t;
q=t;
}
q->next=NULL;
}
void listinsert(struct listnode * *a,struct listnode * *b)
{ /*將兩個(gè)鏈表按遞增序列排序*/
struct listnode *pa,*pb,*other,*la,*pre;
la=(struct listnode *)malloc(sizeof(struct listnode));
la->next=*a;
pa=*a;
pb=*b;
pre=la;
while(pa&&pb)
{
if(pa->data<pb->data)
{
pre=pre->next;
pa=pa->next;
}
else if (pa->data>pb->data)
{
other=(struct listnode *)malloc(sizeof(struct listnode));
other->data=pb->data;
other->next=pa;
pre->next=other;
pre=other;
pb=pb->next;
}
else
{
pre=pre->next;
pa=pa->next;
pb=pb->next;
}
}
if(!pa)
{
while(pb)
{
other=(struct listnode *)malloc(sizeof(struct listnode));
other->data=pb->data;
pre->next=other;
pre=pre->next;
pb=pb->next;
}
pre->next=NULL;
}
*a=la->next;
free(la);
}
void readlist(struct listnode *p)
{ /*遍歷鏈表并輸出最終結(jié)果*/
struct listnode *q;
q=p;
printf("鏈表的排序結(jié)果為:\n");
while(q!=NULL)
{
printf("%3d",q->data);
q=q->next;
}
printf("\n");
}
運(yùn)行結(jié)果
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
上一篇:C語言中形參和實(shí)參詳解及實(shí)例代碼
欄 目:C語言
本文標(biāo)題:C語言中數(shù)據(jù)結(jié)構(gòu)之鏈表歸并排序?qū)嵗a
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1547.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery


