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的長(zhǎng)度*/
#define N2 6 /*鏈表Lb的長(zhǎng)度*/
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ì)本站的支持!
相關(guān)文章
使用c++編程實(shí)現(xiàn)簡(jiǎn)單的打字小游戲
這篇文章主要為大家介紹了使用c++編程語言來實(shí)現(xiàn)一個(gè)非常簡(jiǎn)單的打字小游戲過程實(shí)現(xiàn)的示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
C++實(shí)現(xiàn)簡(jiǎn)易的彈球小游戲
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)易的彈球小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
C++類與對(duì)象的重點(diǎn)知識(shí)點(diǎn)詳細(xì)分析
類和對(duì)象是兩種以計(jì)算機(jī)為載體的計(jì)算機(jī)語言的合稱。對(duì)象是對(duì)客觀事物的抽象,類是對(duì)對(duì)象的抽象。類是一種抽象的數(shù)據(jù)類型;變量就是可以變化的量,存儲(chǔ)在內(nèi)存中—個(gè)可以擁有在某個(gè)范圍內(nèi)的可變存儲(chǔ)區(qū)域2023-02-02
VC通過托盤圖標(biāo)得到該所屬進(jìn)程的實(shí)現(xiàn)代碼
這篇文章主要介紹了VC通過托盤圖標(biāo)得到該所屬進(jìn)程的實(shí)現(xiàn)代碼,為了方便大家使用特將多個(gè)代碼分享給大家,需要的朋友可以參考下2021-10-10
opencv配置的完整步驟(win10+VS2015+OpenCV3.1.0)
OpenCV是計(jì)算機(jī)視覺中經(jīng)典的專用庫,其支持多語言、跨平臺(tái),功能強(qiáng)大,這篇文章主要給大家介紹了關(guān)于opencv配置(win10+VS2015+OpenCV3.1.0)的相關(guān)資料,需要的朋友可以參考下2021-06-06
C++中對(duì)象的賦值與復(fù)制操作詳細(xì)解析
對(duì)象之間的賦值也是通過賦值運(yùn)算符“=”進(jìn)行的。本來賦值運(yùn)算符“=”只能用來對(duì)單個(gè)的變量賦值,現(xiàn)在被擴(kuò)展為兩個(gè)同類對(duì)象之間的賦值,這是通過對(duì)賦值運(yùn)算符的重載實(shí)現(xiàn)的2013-10-10

