c语言有序链表合并的实验报告,两个有序链表序列的合并 -C语言实现

#include

#include

typedef int ElementType;

typedef struct Node *PtrToNode;

struct Node {

ElementType Data;

PtrToNode Next;

};

typedef PtrToNode List;

List Read(int a[],int n); /* 细节在此不表 */

void Print( List L ); /* 细节在此不表;空链表将输出NULL */

List Merge( List L1, List L2 );

int main()

{

List L1, L2, L;

int l1[5] = {2,4,6,8,10};

int l2[5] = {2,4,6,8,10};

L1 = Read(l1,5);

L2 = Read(l2,5);

L = Merge(L1, L2);

Print(L);

Print(L1);

Print(L2);

return 0;

}

void Print( List L )

{

List p = L->Next;

if(!p){

printf("NULL\n");

}

else{

while (p) {

printf("%d ",p->Data);

p = p->Next;

}

printf("\n");

}

}

List Read(int a[],int n)

{

List head = (List)malloc(sizeof(List));

head->Data = n;

head->Next = NULL;

int i;

List p,last;

last = head;

for (i = 0; i < n; i++) {

p = (List)malloc(sizeof(List));

p->Data = a[i];

p->Next = NULL;

last->Next = p;

last = p;

}

return head;

}

List Merge( List L1, List L2 )

{

List head,tail;

head = (List)malloc(sizeof(struct Node));

tail = head;

head->Next = NULL;

head->Data = L1->Data + L2->Data;

List p,q;

p = L1->Next;

q = L2->Next;

while (p && q)

{

if(p->Data > q->Data)

{

tail->Next = q;

tail = q;

q = q->Next;

}

else if(p->Data < q->Data)

{

tail->Next = p;

tail = p;

p = p->Next;

}

else

{

tail->Next = p;

tail = p;

p = p->Next;

tail->Next = q;

tail = q;

q = q->Next;

}

}

while (p)

{

tail->Next = p;

tail = p;

p = p->Next;

}

while (q)

{

tail->Next = q;

tail = q;

q = q->Next;

}

L1->Next = NULL;

L2->Next = NULL;

return head;

}

以下是使用C语言实现合并两个有序链表序列的代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表结构体 typedef struct Node { int value; // 值 struct Node *next; // 指向下一个节点的指针 } Node; // 合并两个有序链表 Node *mergeLists(Node *list1, Node *list2) { Node *head = NULL; // 合并链表的头节点 Node *tail = NULL; // 合并链表的尾节点 // 如果有一个链表为空,则直接返回另一个链表 if (list1 == NULL) { return list2; } else if (list2 == NULL) { return list1; } // 选择头节点 if (list1->value < list2->value) { head = list1; list1 = list1->next; } else { head = list2; list2 = list2->next; } tail = head; // 循环合并两个链表 while (list1 != NULL && list2 != NULL) { if (list1->value < list2->value) { tail->next = list1; list1 = list1->next; } else { tail->next = list2; list2 = list2->next; } tail = tail->next; } // 将剩余的节点添加到尾部 if (list1 != NULL) { tail->next = list1; } else { tail->next = list2; } return head; } // 打印链表 void printList(Node *list) { while (list != NULL) { printf("%d ", list->value); list = list->next; } printf("\n"); } int main() { // 创建两个有序链表 Node *list1 = (Node *) malloc(sizeof(Node)); list1->value = 1; list1->next = (Node *) malloc(sizeof(Node)); list1->next->value = 3; list1->next->next = (Node *) malloc(sizeof(Node)); list1->next->next->value = 5; list1->next->next->next = NULL; Node *list2 = (Node *) malloc(sizeof(Node)); list2->value = 2; list2->next = (Node *) malloc(sizeof(Node)); list2->next->value = 4; list2->next->next = (Node *) malloc(sizeof(Node)); list2->next->next->value = 6; list2->next->next->next = NULL; // 合并两个有序链表 Node *mergedList = mergeLists(list1, list2); // 打印合并后的链表 printList(mergedList); return 0; } ``` 输出结果为: ``` 1 2 3 4 5 6 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值