當前位置:首頁 » 編程語言 » c語言合並鏈表

c語言合並鏈表

發布時間: 2022-05-15 09:22:43

『壹』 c語言中鏈表合並怎麼弄詳解

鏈表是一種物理存儲單元上非連續、非順序的存儲結構,數據元素的邏輯順序是通過鏈表中的指針鏈接次序實現的。鏈表由一系列結點(鏈表中每一個元素稱為結點)組成,結點可以在運行時動態生成。每個結點包括兩個部分:一個是存儲數據元素的數據域,另一個是存儲下一個結點地址的指針域。相比於線性表順序結構,操作復雜。

使用鏈表結構可以克服數組鏈表需要預先知道數據大小的缺點,鏈表結構可以充分利用計算機內存空間,實現靈活的內存動態管理。但是鏈表失去了數組隨機讀取的優點,同時鏈表由於增加了結點的指針域,空間開銷比較大。在計算機科學中,鏈表作為一種基礎的數據結構可以用來生成其它類型的數據結構。鏈表通常由一連串節點組成,每個節點包含任意的實例數據(datafields)和一或兩個用來指向上一個/或下一個節點的位置的鏈接("links")。鏈表最明顯的好處就是,常規數組排列關聯項目的方式可能不同於這些數據項目在記憶體或磁碟上順序,數據的存取往往要在不同的排列順序中轉換。而鏈表是一種自我指示數據類型,因為它包含指向另一個相同類型的數據的指針(鏈接)。鏈表允許插入和移除表上任意位置上的節點,但是不允許隨機存取。鏈表有很多種不同的類型:單向鏈表,雙向鏈表以及循環鏈表。


以上是對鏈表的一個概述,說的其實很全面了。我們應用鏈表就是為了克服順序表(數組)必須在內存中連續分配相鄰地址的束縛,更好的應用內存空間(很多破碎不連貫空間)。

你可以把鏈表類比成貨運火車,火車的每一節車皮就是鏈表的每一個結點(一般用link表示),每個結點實際上有兩個部分,一個部分是裝貨的空間就是鏈表的數據存儲部分(一般用link—>data表示),另一部分就是與下一節車廂的連接部分就是鏈表的指針部分(用link—>next表示,指向下一個結點)。那麼我們平時怎樣管理火車呢?記住火車的第一節車皮即可,順著第一節就能找到找到所有的車皮。鏈表也是如此,有了頭結點(一般用head表示)就能找到所有的結點。這里缺點就來了,比如一共100節車皮,我讓你找49節車皮,那你就必須從第一節車皮開始找,否則你沒辦法確定哪個是第49節。鏈表也是如此,必須從頭結點開始找起,這也就是為什麼要記住頭結點的原因之一。相比數組直接按照序號訪問,鏈表的訪問要麻煩很多。同樣我們也需要記住尾結點,就好像我們在一列長火車的尾部插一面小紅旗,那麼列車工人就能方便找到車尾,把需要的車皮掛載到這列火車上;鏈表同樣如此,我們用tail來標記尾結點,直接把需要增加的結點載入到鏈表上即可,否則沒有尾結點,那我們就要從頭開始找到尾,很麻煩啊。

鏈表合並其實很簡單,只要是兩個結點數據類型相同(不同也可以),把其中一個的結點的頭結點連接到另一個的尾結點就可以了。就是讓其中一個的尾結點的指針tail->next=head(另一個結點的頭結點)當然這是無序鏈表。如果是有序鏈表,比如結點數據時按照從大到小排列的,那首先就需要找到插入位置,讀取每一個結點的數據,然後比較。找到插入位置之後按照下圖進行的方式即可:

『貳』 有關c語言兩個順序鏈表的合並

typedefintElementType;
typedefstructNode*PtrToNode;//定義PtrToNode為指向Node的結構體指針
structNode{
ElementTypeData;
PtrToNodeNext;
};
typedefPtrToNodeList;//定義list為指向node結構體的指針

ListMerge(ListL1,ListL2){
Listpa=L1,pb=L2,head;
Listhead=pc=(structNode*)malloc(sizzeof(structNode));
pc=(pa->Data<=pb->Data)?pa:pb;//pc指向Data較小的節點
while(pa&&pb){//循環比較L1與L2兩條鏈表的Data
if(pa->Data<pb->Data){
pc->Next=pa;
pc=pc->Next;
pa=pa->Next;
}
else{
pc->Next=pb;
pc=pc->Next;
pb=pb->Next;
}
}
while(pa){pc->Next=pa;pc=pc->Next;pa=pa->Next;}//將剩餘的鏈表接到pc後面
while(pb){pc->Next=pb;pc=pc->Next;pb=pb->Next;}
printf("ListMergeisavailable!");
returnhead;
}

『叄』 C語言合並鏈表

#include<stdio.h>
#include<malloc.h>

structstudenta{
intnum;
floatscore;
structstudenta*next;
};

structstudentb{
intnum;
floatscore;
structstudentb*next;
};

structstudenta*add(structstudenta*heada,structstudenta*p0)
{
structstudenta*p1,*p2;
p1=heada;
while(/*p1->next!=NULL*/p1!=NULL&&p1->num<p0->num){
p2=p1;
p1=p1->next;
}
if(p1==heada){
heada=p0;
p0->next=p1;
}else{
if(p2->next!=NULL){
//p2->next=p0;
//p0->next=p1;/*順序不要搞錯*/
p0->next=p1;
p2->next=p0;
}else{
p2->next=p0;
p0->next=NULL;
}
}
returnheada;
}

intmain()
{
structstudentaboya[3],*heada,*p,*p0,*pa;
structstudentbboyb[3],*headb,*pb;
structstudenta*add(structstudenta*,structstudenta*);
heada=boya;
headb=boyb;
boya[0].num=101;
boya[0].score=100.0;
boya[1].num=103;
boya[1].score=98.0;
boya[2].num=105;
boya[2].score=55.0;
boya[0].next=&boya[1];
boya[1].next=&boya[2];
boya[2].next=NULL;

boyb[0].num=102;
boyb[0].score=35.0;
boyb[1].num=104;
boyb[1].score=77.0;
boyb[2].num=106;
boyb[2].score=59.0;
boyb[0].next=&boyb[1];
boyb[1].next=&boyb[2];
boyb[2].next=NULL;

printf("A鏈表: ");
pa=heada;
do{
printf("num=%d score=%5.1f ",pa->num,pa->score);
pa=pa->next;
}while(pa);//用heada遍歷,循環結束heada已經不再指向頭結點

printf("B鏈表: ");
pb=headb;
do{
printf("num=%d score=%5.1f ",pb->num,pb->score);
pb=pb->next;
}while(pb);//headb遍歷,循環結束headb已經不再指向頭結點

printf("合並鏈表: ");
do{
p0=(structstudenta*)malloc(sizeof(structstudenta));
p0->num=headb->num;
p0->score=headb->score;
p=add(heada,p0);
headb=headb->next;
}while(headb);

do{
printf("num=%d score=%5.1f ",p->num,p->score);
p=p->next;
}while(p);

return0;
}

『肆』 C語言鏈表的合並代碼

#include<stdio.h>
#include<stdlib.h>

typedefstructstudent
{
int num;
floatscore;
structstudent*next;
}STUD;

STUD*creat() //建立鏈表函數
{
STUD*h,*p,*q;
inti;

h=NULL;
printf("Pleaseinputthedata: ");
for(i=1;i<=3;i++)
{
p=(STUD*)malloc(sizeof(STUD));//申請空間
scanf("%d,%f",&p->num,&p->score);//此處用逗號分隔

if(h==NULL) //將鏈表的節點連接起來
h=p;
else
q->next=p;

q=p;
}
q->next=NULL;

return(h);
}

voidprint(STUD*h) //輸出鏈表函數
{
printf(" ");
while(h!=NULL)
{
printf("%-5d%-5f ",h->num,h->score);
h=h->next;
}
}

STUD*connect(STUD*h1,STUD*h2) //連接鏈表
{ //以下代碼大修改
STUD*head;

head=h1;
while(h1->next!=NULL) //先讓第一個鏈表循環到最都一個節點
{
h1=h1->next;
}

h1->next=h2; //將最後一個節點連接第二個鏈表

returnhead; //返回連接好的鏈表
}

intmain(void) //voidmain()你自己上網搜搜是對的還是錯的!
{
STUD*head1,*head2;

head1=creat(); //創建第一個鏈表

print(head1); //輸出第一個鏈表

head2=creat(); //創建第二個鏈表

print(head2); //輸出第二個鏈表

head1=connect(head1,head2); //連接鏈表

print(head1); //輸出連接後的鏈表

return0;
}

『伍』 c語言如何實現兩鏈表合並

只要讓第一個鏈表的尾部元素 的 next 指針,指向第二個鏈表的第一個元素就可以了
如果是雙向鏈表,則除上面操作外,再讓第二個鏈表的 prev指針指向第一個鏈表最後一個元素

『陸』 有序鏈表的合並,c語言

  • #include<stdio.h>

  • #include<malloc.h>

  • typedefstructlist{

  • intdata;

  • structlist*next;//下一個節點地址

  • }list;

  • //第一條鏈表

  • structlist*L=NULL;//頭

  • structlist*head=NULL;//首

  • structlist*p=NULL;

  • //第二條鏈表

  • structlist*L1=NULL;//頭

  • structlist*head1=NULL;//首

  • structlist*p1=NULL;

  • //代理鏈表

  • structlist*L2=NULL;//頭

  • structlist*q=NULL;//L2備用地址

  • structlist*q1=NULL;//備用地址

  • intmain(){

  • inti=0,length;

  • printf("請輸入鏈表的長度 ");

  • scanf("%d",&length);

  • head=(structlist*)malloc(sizeof(structlist));

  • L=head;

  • printf("請依次輸入鏈表的內容 ");

  • for(i;i<length;i++){

  • p=(structlist*)malloc(sizeof(structlist));

  • scanf("%d",&p->data);

  • p->next=NULL;

  • head->next=p;

  • head=p;

  • }

  • inti1=0,length1;

  • printf("請輸入鏈表的長度 ");

  • scanf("%d",&length1);

  • head1=(structlist*)malloc(sizeof(structlist));

  • L1=head1;

  • printf("請依次輸入鏈表的內容 ");

  • for(i1;i1<length1;i1++){

  • p1=(structlist*)malloc(sizeof(structlist));

  • scanf("%d",&p1->data);

  • p1->next=NULL;

  • head1->next=p1;

  • head1=p1;

  • }

  • L2=(structlist*)malloc(sizeof(structlist));

  • q=L2;//備用合並鏈表起始地址

  • p=L->next;

  • p1=L1->next;

  • while(p&&p1){

  • if(p->data<p1->data){

  • L2->next=p;

  • L2=p;

  • p=p->next;

  • }elseif(p->data==p1->data){

  • L2->next=p;

  • L2=p;

  • p=p->next;

  • q1=p1->next;//備用相同元素的下一個地址指向

  • free(p1);

  • p1=q1;

  • }elseif(p->data>p1->data){

  • L2->next=p1;

  • L2=p1;

  • p1=p1->next;

  • }

  • }

  • L2->next=p?p:p1;

  • free(L1);

  • printf("合並後鏈表的內容 ");

  • p=q->next;

  • while(p){

  • printf("%d",p->data);

  • p=p->next;

  • }

  • }

『柒』 C語言單鏈表合並

#include<stdio.h>#include<stdlib.h>structlist{intdata;structlist*next;};//兩個鏈表融合,插入排序函數voidsort(structlist*l1,structlist*l2);//輸出鏈表voidoutput(structlist*head);//輸入鏈表voidinput(structlist*head,intnum);intmain(){intn;list*h1,*h2;//兩個鏈表的頭,下面四行初始化鏈表h1=(structlist*)malloc(sizeof(structlist));h2=(structlist*)malloc(sizeof(structlist));h1->next=NULL;h2->next=NULL;//兩個鏈表輸入printf("請輸入第一個鏈表節點數: ");scanf("%d",&n);input(h1,n);printf("請輸入第二個鏈表節點數: ");scanf("%d",&n);input(h2,n);//合並鏈表並排序sort(h1,h2);//輸出合並後的鏈表output(h1);}voidinput(structlist*head,intnum){structlist*tmp;structlist*end;end=head;printf("請輸入鏈表節點: ");for(inti=0;i!=num;i++){tmp=(structlist*)malloc(sizeof(structlist));scanf("%d",&tmp->data);end->next=tmp;tmp->next=NULL;end=tmp;}}voidsort(structlist*l1,structlist*l2){structlist*p1,*p2,*tmp;p1=l1;p2=l2->next;while(p1->next&&p2){if(p1->next->data>p2->data){tmp=p2->next;p2->next=p1->next;p1->next=p2;p2=tmp;}elsep1=p1->next;}if(p2)p1->next=p2;}voidoutput(structlist*head){while(head->next){printf("%d",head->next->data);head=head->next;}}

熱點內容
javatoolsfor 發布:2024-03-29 18:17:55 瀏覽:900
linuxi2c驅動 發布:2024-03-29 18:09:56 瀏覽:672
junit源碼下載 發布:2024-03-29 18:00:10 瀏覽:526
本田雅閣壓縮機不工作 發布:2024-03-29 17:59:13 瀏覽:601
溯源碼可以偽造嗎 發布:2024-03-29 17:54:45 瀏覽:57
北京編程傳 發布:2024-03-29 17:54:44 瀏覽:436
編程畫曲線 發布:2024-03-29 17:48:59 瀏覽:60
簡單存儲服務s3 發布:2024-03-29 17:48:46 瀏覽:337
安卓手機的usb功能在哪裡設置 發布:2024-03-29 17:46:27 瀏覽:759
配置文件ini如何寫 發布:2024-03-29 17:31:05 瀏覽:998