编写一个函数,用于合并两个链表,输入的链表按照其元素顺序从小到大, 输出的链表元素也按大小进行顺序排序


#include<iostream>
#include<stdio.h>


typedef struct Node
{//定义一个结点
int data;
Node *next;
Node(const int& dataVal,Node* nextVal=NULL):data(dataVal),next(nextVal) {}
Node(Node* nextVal=NULL):next(nextVal){}
}LNode,*LinkList;


class LList
{
public: 
LList(int size);
~LList();


void clear();
void print(LNode* head) ;
void insert(const int& it);
void append(const int& it);
LNode* createLinkList(int n);
LNode* bubbleSortLinkList(LNode* headNode);
int length();
private:
Node* head;
Node* tail;
Node* curr;
int linkListCnt;
};


LList::LList(int size)
{
curr = head = tail = new Node();
linkListCnt = 0;
}
LList::~LList()
{
while (head!=NULL)
{
curr = head;
head = head->next;
delete curr;
}
}
void LList::clear()
{
while (head != NULL)
{
curr = head;
head = head->next;
delete curr;
}


curr = head = tail = new Node();
linkListCnt = 0;
}


void LList::insert(const int& it)
{
curr->next = new LNode(it, curr->next);
if (tail == curr)
tail = curr->next;
linkListCnt++;
}


void LList::append(const int& it)
{
tail->next = new Node(it, NULL);
tail = tail->next;
linkListCnt++;
}


int LList::length() { return linkListCnt; }


void LList::print(LNode* head) 
{
LNode* p = head->next;
while (p)
{
std::cout << p->data << " " ;
p = p->next;
}
std::cout << std::endl;
}


LNode* LList::createLinkList(int n)
{
int value;
linkListCnt = 0;
for (int i = 0; i < n; ++i)
{
printf("请输入链表中的第%d个数:", i);
scanf_s("%d", &value);
tail->next = new Node(value, NULL);
tail = tail->next;
linkListCnt++;
}
return head;//返回链表
}


LNode* LList::bubbleSortLinkList(LNode* headNode)
{
LNode* tempNode1 = NULL;
LNode* tempNode2 = NULL;


if (headNode == tail)
return NULL;
for (tempNode1= headNode->next; tempNode1 != NULL; tempNode1 = tempNode1->next)
{
for (tempNode2 = headNode->next; tempNode2 != NULL; tempNode2 = tempNode2->next)
{
if (tempNode1->data < tempNode2->data)
{
int temp = tempNode1->data;
tempNode1->data = tempNode2->data;
tempNode2->data = temp;
}
}
}
return headNode;
}


LNode* mergeLinkList(LNode* list1Node, LNode* list2Node)
{
LNode* tempNode = list1Node;
for (; tempNode->next != NULL; tempNode = tempNode->next) { ; }


tempNode->next = list2Node->next;
list2Node->next = NULL;//避免被第二次析构
return list1Node;

}


#include "stdafx.h"
#include"List.h"


int main()
{
LList list1(10),list2(10);
LNode* list1Node = list1.createLinkList(5);
LNode* list2Node = list2.createLinkList(5);
std::cout << "两个链表连接后的链表:" << std::endl;
LNode* tempNode = mergeLinkList(list1Node, list2Node);
list1.bubbleSortLinkList(tempNode);
list1.print(tempNode);
    return 0;

}

由于对数据结构较为陌生,编写的代码水平有限,望得到指点改进。进行过简单的测试,可行,也许在某些特殊情况条件下还得对代码进行适当的修正!

好的,下面是一个基于插入排序链表排序的示例代码,使用C语言实现: ```c #include <stdio.h> #include <stdlib.h> typedef struct ListNode { int val; struct ListNode* next; } ListNode; ListNode* insertionSortList(ListNode* head) { // 如果链表为空或只有一个节点,直接返回 if (head == NULL || head->next == NULL) { return head; } ListNode* dummy = (ListNode*)malloc(sizeof(ListNode)); // 创建一个虚拟头节点 dummy->next = head; ListNode* lastSorted = head; // 表示已经有序的链表尾节点 ListNode* cur = lastSorted->next; // 表示待排序的节点 while (cur) { if (lastSorted->val <= cur->val) { // 如果已排序节点的值小于等于待排序节点的值,则直接更新lastSorted指针 lastSorted = lastSorted->next; } else { // 否则需要在已排序链表中找到待排序节点的插入位置 ListNode* prev = dummy; while (prev->next->val <= cur->val) { // 寻找待排序节点的插入位置 prev = prev->next; } lastSorted->next = cur->next; // 先将待排序节点从原位置删除 cur->next = prev->next; // 将待排序节点插入到prev和prev->next之间 prev->next = cur; } cur = lastSorted->next; } return dummy->next; } // 创建一个链表 ListNode* createList(int* arr, int n) { if (n == 0) { // 如果数组为空,返回NULL return NULL; } ListNode* head = (ListNode*)malloc(sizeof(ListNode)); head->val = arr[0]; head->next = NULL; ListNode* cur = head; for (int i = 1; i < n; i++) { ListNode* newNode = (ListNode*)malloc(sizeof(ListNode)); newNode->val = arr[i]; newNode->next = NULL; cur->next = newNode; cur = cur->next; } return head; } // 打印链表 void printList(ListNode* head) { ListNode* cur = head; while (cur) { printf("%d ", cur->val); cur = cur->next; } printf("\n"); } int main() { int arr[] = {4, 2, 1, 3}; int n = sizeof(arr) / sizeof(int); ListNode* head = createList(arr, n); printf("排序前:"); printList(head); ListNode* sortedHead = insertionSortList(head); printf("排序后:"); printList(sortedHead); return 0; } ``` 在这个示例代码中,我们使用了插入排序的思想,每次将一个排序节点插入到已排序链表的合适位置。时间复杂度为 $O(n^2)$。 希望这个示例代码能够帮助你实现链表排序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值