合並鏈表c語言
1. c語言鏈表合並函數的問題
創建鏈表的函數有些問題吧
student *p;
student *q=I;
for (j=0;j<i;j++)
{
p=(student*)malloc(sizeof(student));
p->next=Null;
q->next=p;
q=p;
}
2. C語言 把兩個有序鏈表合並為一個有序鏈表(遞增)
設鏈表結點結構為Node(int data, Node *next),typedef Node List,鏈表均帶表頭結點。
思路是:把list1中的元素看成是集合1,把list2中的元素看成是集合2,把list1頭結點(即list1結點)從集合1中脫離下來看成是目標集合的頭結點,目標集合開始時是空集,並用last指針始終指向該集合的尾部,然後每次從集合1和集合2中取各自的第一個元素進行比較,較小者從相應集合里脫離,插入到目標集合list1的尾部即last的末尾,並將剛插入的元素作為目標集合list1的新的last,直到集合1為空或集合2為空時結束,最後將未空的集合中的剩餘元素鏈接到last後面即可。
3. 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;}}4. C語言 鏈表合並並排序
/**********start**********//以下是偽代碼,有些變數需要定義
if(heada->num
<=
headb->num)//判斷第一個節點是在a還是b上,小的作為新鏈表的頭結點,然後設置指示指針pa,pb,p分別在a鏈表,b鏈表,新鏈表上向後移動
{head=heada;
pa=heada->next;
pb=headb;}
else
{head=headb;
pa=heada;
pb=headb->next;}
p=head;
while(pa
&&
pb)//當a,b鏈表都沒到達尾部時,比較a,b鏈表上當前指示指針pa,pb所指節點的大小,並把新鏈表的p指向較小的(pa或pb所指的),然後向後移動一下
{
if
(pa->num<=pb->num)
{p->next=pa;p=p->next;pa=pa->next}
else
{p->next=pb;p=p->next;pb=pb->next}
}
if(pa)//說明a鏈表沒到尾部,b鏈表已結束,直接把a鏈表剩下的部分接在新鏈表最後
{
p->next=pa;
tail=taila;
}
if(pb)//同上b鏈表後面的部分接在新鏈表後
{
p->next=pb;
tail=tailb;
}
tail->next=NULL;
/***********end***********/
5. c語言如何實現兩鏈表合並
只要讓第一個鏈表的尾部元素 的 next 指針,指向第二個鏈表的第一個元素就可以了
如果是雙向鏈表,則除上面操作外,再讓第二個鏈表的 prev指針指向第一個鏈表最後一個元素
6. c語言編程,合並兩個循環鏈表
首先要以head1為開始點,找到鏈表一中的最後一個節點;
然後以最後一個節點為開始點,指向head2;
最後將鏈表二中的最後一個節點指向鏈表一的開始節點;
這樣就將兩個循環鏈表合並成一個循環鏈表了。
7. 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;
}
8. 有序鏈表的合並,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;
}
}
9. 用C語言編一個程序:兩個遞增有序鏈表合並成一個遞減鏈表,
#include<stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node,*list;
void init(list &head)
{
head=(list)malloc(sizeof(node));
head->next=NULL;
}
void input(list &h)
{
list p,q;
q=h;
printf("輸入數據的個數 n : ");
int n;
scanf("%d",&n);
printf("請輸入 %d 個有序遞增數據:\n",n);
for (int i=0;i<n;i++)
{
// printf("第 %d 個: ",i+1);
p=(list)malloc(sizeof(node));
scanf("%d",&p->data);
p->next=q->next;
q->next=p;
q=p;
}
}
void output(list h)
{
list p;
p=h->next;
printf("輸出數據\n");
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void combine(list &a,list &b,list &c)
{
list p,q,t;
p=a->next;
q=b->next;
free(b);
b=q;
c=a;
a=p;
c->next=NULL;
while(p&&q)
{
if (p->data<=q->data)
{
a=a->next;
p->next=c->next;
c->next=p;
p=a;
}
else
{
b=q->next;
q->next=c->next;
c->next=q;
q=b;
}
}
if (p!=NULL)
{
while(p)
{
a=a->next;
p->next=c->next;
c->next=p;
p=a;
}
}
if (q!=NULL)
{
while(q)
{
b=q->next;
q->next=c->next;
c->next=q;
q=b;
}
}
}
void main()
{
list a,b,c;
init(a);init(b);
printf("\n輸入鏈表A :\n");
input(a);
printf("\n輸入鏈表B :\n");
input(b);
printf("輸出合並後的鏈表:\n");
combine(a,b,c);
output(c);
}
結果:
輸入鏈表A :
輸入數據的個數 n : 3
請輸入 3 個有序遞增數據:
4 5 9
輸入鏈表B :
輸入數據的個數 n : 4
請輸入 4 個有序遞增數據:
2 3 7 10
輸出合並後的鏈表:
輸出數據
10 9 7 5 4 3 2
Press any key to continue
10. 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;
}