c語言將兩個鏈表合並為一個

形參不應該是指針類型嗎?
取地址是個啥?版
void Merge(TxILink *T,TxILink *L)
{
struct TxILink* p = T;
while(p->next)
{ p = p->next ; }
p->next = L;
}
這樣就可以權了。

㈡ C語言實現的合並兩個單鏈表的程序,高手幫忙看下錯在哪兒了呢

/*錯的地方還不少,主要有以下幾點
1.使用malloc函數沒有包含其頭文件。
2.s_union最後缺個「}」。
3. printf_s函數裡面printf("%c",p->ch;p=p->next;);應改為printf("%c",p->ch);p=p->next;
4.某些地方s對象的成員x寫成了其他字元串。
以上是語法錯誤,還存在編譯不報錯的邏輯錯誤,如下
5.s_union合並演算法錯誤。
6. s_create採用鏈表前插法,這樣得到的字元序列與輸入相反。
此外還有一些其他編寫不太好的地方,一並修正,得到如下結果
*/
#include<stdio.h>
#include<malloc.h>

typedef struct node
{char x;node* next;}s;

s* s_create(int* length)
{
s *p,*head,*tail;
char ch;
head=tail=(s*)malloc(sizeof(s));
head->next=NULL;
scanf("%c",&ch);
(*length)++;
while(ch!='\n')
{
p=(s*)malloc(sizeof(s));
p->x=ch;
p->next=NULL;
tail=tail->next=p;
(*length)++;
scanf("%c",&ch);
}
return head;
}

void s_union(s* head1,s* head2)
{
s *p=head1->next,*q=head2->next,*temp1,*temp2;
while(q!=NULL)
{
temp1=p->next;
temp2=q->next;
p->next=q;
p=q->next=temp1;
q=temp2;
}
}

void printf_s(s* head)
{
s *p=head->next;
while(p){printf("%c",p->x);p=p->next;}
printf("\n");
}

int main()
{
s *head1,*head2;
int length1=0,length2=0;
head1=s_create(&length1);
head2=s_create(&length2);
printf_s(head1);
printf_s(head2);
if(length1>length2)s_union(head1,head2);
else s_union(head2,head1);
printf_s(head1);
return 0;
}

㈢ 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;

  • 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;

  • }

  • p=L->next;//得到首原節點

  • p1=L1->next;//得到首原節點

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

  • L2=L;//指向已有鏈表空間

  • L2->next=NULL;

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

  • //循環里主要是頭插法原理

  • while(p||p1){

  • if(!p){

  • q=p1;

  • p1=p1->next;

  • }

  • elseif(!p1){

  • q=p;

  • p=p->next;

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

  • q=p;

  • p=p->next;

  • }else{

  • q=p1;

  • p1=p1->next;

  • }

  • q->next=L2->next;

  • L2->next=q;

  • }

  • free(L1);

  • p=L2->next;

  • while(p){

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

  • p=p->next;

  • }

  • }

  • #include<stdio.h>
    #include<malloc.h>
    typedef struct list {
    int data;
    struct list * next; //下一個節點地址
    }list;
    //第一條鏈表
    struct list * L=NULL;//頭
    struct list * head=NULL;//首
    struct list * p=NULL;
    //第二條鏈表
    struct list * L1=NULL;//頭
    struct list * head1=NULL;//首
    struct list * p1=NULL;
    //代理鏈表
    struct list * L2=NULL;//頭
    struct list * q=NULL;
    int main(){
    int i=0,length;
    printf("請輸入鏈表的長度 ");
    scanf("%d",&length);
    head=(struct list *)malloc(sizeof(struct list));
    L=head;
    printf("請依次輸入鏈表的內容 ");
    for(i;i<length;i++){
    p = (struct list *)malloc(sizeof(struct list));
    scanf("%d",&p->data);
    p->next=NULL;
    head->next=p;
    head=p;
    }
    int i1=0,length1;
    printf("請輸入鏈表的長度 ");
    scanf("%d",&length1);

    head1=(struct list *)malloc(sizeof(struct list));
    L1=head1;
    printf("請依次輸入鏈表的內容 ");
    for(i1;i1<length1;i1++){
    p1= (struct list *)malloc(sizeof(struct list));
    scanf("%d",&p1->data);
    p1->next=NULL;
    head1->next=p1;
    head1=p1;
    }
    p=L->next;//得到首原節點
    p1=L1->next;//得到首原節點
    L2=(struct list *)malloc(sizeof(struct list));
    L2=L;//指向已有鏈表空間
    L2->next=NULL;
    q=(struct list *)malloc(sizeof(struct list));
    //循環里主要是 頭插法原理
    while(p||p1){
    if(!p){
    q=p1;
    p1=p1->next;
    }
    else if(!p1){
    q=p;
    p=p->next;
    }else if(p->data<=p1->data){
    q=p;
    p=p->next;
    }else{
    q=p1;
    p1=p1->next;
    }
    q->next = L2->next;
    L2->next=q;
    }
    free(L1);
    p=L2->next;
    while(p){
    printf("%d ",p->data);
    p=p->next;
    }
    }

㈣ C語言程序題:兩個有序單鏈表的合並 合並之後仍然有序。 如第一個鏈表13579 第二個鏈表

ListNode*ReNewCombineList(ListNode*p1,ListNode*p2)//合並兩個鏈表,,生成第三個鏈表遞歸

{

ListNode*pNewList=NULL;

//ListNode*p3=NULL;

if(p1==NULL)

return p2;

if(p2==NULL)

return p1;

if(p1-&gt;data&lt;p2-&gt;data)

{

pNewList=p1;

pNewList-&gt;next=ReNewCombineList(p1-&gt;next,p2);

}

else

{

pNewList=p2;

pNewList-&gt;next=ReNewCombineList(p1,p2-&gt;next);

}

return pNewList;

}

(4)兩個單鏈表合並c語言擴展閱讀:

return

C++的關鍵字,它提供了終止函數執行的一種方式。當return語句提供了一個值時,這個值就成為函數的返回值.

說到return,有必要提及主函數的定義,下面是從網路上找到的資料,好好消化吧,對了解主函數中返回值的理解有很大的幫助.

很多人甚至市面上的一些書籍,都使用了void main(),其實這是錯誤的。C/C++中從來沒有定義過void main()。

C++之父Bjarne Stroustrup在他的主頁上的FAQ中明確地寫著The definition void main(){/*...*/}is not and never has been C++,

nor has it even been C.(void main()從來就不存在於C++或者C)。下面我分別說一下C和C++標准中對main函數的定義。

1.C

在C89中,main()是可以接受的。Brian W.Kernighan和Dennis M.Ritchie的經典巨著The C programming Language 2e(《C程序設計語言第二版》)用的就是main()。不過在最新的C99標准中,只有以下兩種定義方式是正確的:

int main(void)

int main(int argc,char*argv[])

(參考資料:ISO/IEC 9899:1999(E)Programming languages—C 5.1.2.2.1 Program startup)

當然,我們也可以做一點小小的改動。例如:char*argv[]可以寫成char**argv;argv和argc可以改成別的變數名(如intval和charval),不過一定要符合變數的命名規則。

如果不需要從命令行中獲取參數,請用int main(void);否則請用int main(int argc,char*argv[])。

main函數的返回值類型必須是int,這樣返回值才能傳遞給程序的激活者(如操作系統)。

如果main函數的最後沒有寫return語句的話,C99規定編譯器要自動在生成的目標文件中(如exe文件)加入return 0;,表示程序正常退出。不過,我還是建議你最好在main函數的最後加上return語句,雖然沒有這個必要,但這是一個好的習慣。

注意,vc6不會在目標文件中加入return 0;,大概是因為vc6是98年的產品,所以才不支持這個特性。現在明白我為什麼建議你最好加上return語句了吧!不過,gcc3.2(Linux下的C編譯器)會在生成的目標文件中加入return 0;。

㈤ C語言 將兩個非遞減的單鏈表合成一個

#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedefstructLNode
{intdata;
structLNode*next;
}LNode,*LinkList;
voidCreatlist(LinkList*L)
{LinkListp,q;
intnum;
*L=(LinkList)malloc(sizeof(LNode));
q=*L;
printf("輸入若干整數,輸入-1表示結束 ");
while(scanf("%d",&num),num!=-1)
{p=(LinkList)malloc(sizeof(LNode));
p->data=num;
q->next=p;
q=q->next;
}
p->next=NULL;
}
voidPrintList(LinkListL)
{LinkListp;
p=L->next;
while(p)
{printf("%d",p->data);
p=p->next;
}
printf(" ");
}
voidMergeList(LinkListla,LinkListlb,LinkList*lc)
{LinkListpa,pb,pc;
pa=la->next;
pb=lb->next;
*lc=pc=la;
while(pa&&pb)
{if(pa->data<=pb->data)
{pc->next=pa;
pc=pa;
pa=pa->next;
}
else
{pc->next=pb;
pc=pb;
pb=pb->next;
}
}
pc->next=pa?pa:pb;
free(lb);
}
intmain(void)
{LinkListLA,LB,LC;
Creatlist(&LA);
printf("LA:");PrintList(LA);
Creatlist(&LB);
printf("LB:");PrintList(LB);
MergeList(LA,LB,&LC);
printf("LC:");PrintList(LC);
return0;
}

#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedefstructLNode
{
intdata;
structLNode*next;
}LNode,*LinkList;
voidCreatlist(LinkList*L)
{
LinkListp,q;
intnum;
*L=(LinkList)malloc(sizeof(LNode));
q=*L;
printf("輸入若干整數,輸入-1表示結束 ");
while(scanf("%d",&num),num!=-1)
{
p=(LinkList)malloc(sizeof(LNode));
p->data=num;
q->next=p;
q=q->next;
}
p->next=NULL;
}
voidPrintList(LinkListL)
{
LinkListp;
p=L->next;
while(p)
{
printf("%d",p->data);
p=p->next;
}
printf(" ");
}
voidMergeList(LinkListla,LinkListlb,LinkList*lc)
{
LinkListpa,pb,pc,pt;
pa=la->next;
pb=lb->next;
*lc=pc=la;
pc->next=NULL;
free(lb);
while(pa&&pb)
{
if(pa->data<=pb->data)
{
pt=pa->next;
pa->next=pc->next;
pc->next=pa;
pa=pt;
}
else
{
pt=pb->next;
pb->next=pc->next;
pc->next=pb;
pb=pt;
}
}
if(!pa)pa=pb;
while(pa)
{
pt=pa->next;
pa->next=pc->next;
pc->next=pa;
pa=pt;
}
}
intmain(void)
{
LinkListLA,LB,LC;
Creatlist(&LA);
printf("LA:");
PrintList(LA);
Creatlist(&LB);
printf("LB:");
PrintList(LB);
MergeList(LA,LB,&LC);
printf("LC:");
PrintList(LC);
return0;
}

㈥ c語言,如何使兩個鏈表合並,求解答

void test(LNodeL *head1,Node *head2)//將表頭為head2的鏈表合並到head1
{
LNode *p1 = head1;
while(p1->next != NULL)
{
p1 = p1->next;
}
p->next = head2;
}
這樣就可以。

㈦ c語言如何實現兩鏈表合並

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

㈧ C語言程序,兩個單鏈表合並問題,跪求大神解答~~在線等

#include <stdio.h>#include <stdlib.h>typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
ElementType Data;
PtrToNode Next;
};
typedef PtrToNode List;List Read(); /* 鏈表讀取 */void Print( List L ); /*鏈表輸出;空鏈表將輸出NULL */List Merge( List L1, List L2 ); /*鏈表合並*/int main()
{
List L1, L2, L;
L1 = Read(); //讀取鏈表1 L2 = Read(); //讀取鏈表2 L = Merge(L1, L2);
Print(L);
Print(L1);
Print(L2);
return 0;
}List Merge( List L1, List L2 )
{
List L, p1, p2, r ;
p1 = L1->Next;
p2 = L2->Next;
L = (List)malloc(sizeof(struct Node));
L->Next = NULL;
r = L;
while (p1 && p2){
if(p1->Data < p2->Data){
r->Next = p1;
r = p1;
p1 = p1->Next;
}
else{
r->Next = p2;
r = p2;
p2 = p2->Next;
}
}
if(p1)
r->Next = p1;
else r->Next = p2;
L1->Next = NULL;
L2->Next = NULL;
return L;
}List Read()
{
int n, i;
List L, p, s;
scanf("%d",&n);
L = (List)malloc(sizeof(struct Node));
L->Next = NULL;
p = L;
for(i = 0;i < n; ++i){
s = (List)malloc(sizeof(struct Node));
scanf("%d",&s->Data);
s->Next = p->Next;
p->Next = s;
p = s;
}
return L;
}void Print(List L)
{
List p;
p = L->Next;
if (L->Next==NULL)
{
printf("NULL");
}
while(p)
{
printf("%d ", p->Data);
p = p->Next;
}
printf("\n");
}

㈨ C語言鏈表合並:將兩個有序單向鏈表合並為一個單向有序鏈表,要求分別用兩種方式實現~急~求大神幫忙

小意思!有個前提,兩個鏈表的數據類型都是一樣的哦
第一種:先新建一專個鏈表,然後屬遍歷第一鏈表,同時把它的值都賦給新建的鏈表,然後,開始第二個鏈表,也是同樣的辦法。加上第二個的時候,先找到新建鏈表的表尾,再表尾處開始添加第二個
第二種:首先遍歷第一個鏈表,找到表尾,然後去掉第二個鏈表的表頭,把第二個鏈表的頭部賦給第一個鏈表的尾部 //當然,如果沒有表頭什麼的就直接把第一個節點賦給第一個就行了。

第二種方法之後,兩個鏈表就合成一個了。

㈩ 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;}}