


JavaScript implements linked list insertion sort and linked list merge sort
This article introduces in detail the JavaScript implementation of linked list insertion sort and linked list merge sort. Linked list merge sort is to merge and sort each part and then merge them together.
1. Linked list
1.1 Storage representation of linked list
//链表的存储表示 typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *next; }LNode, *LinkList;
1.2 Basic operation
Create linked list:
/* * 创建链表。 * 形参num为链表的长度,函数返回链表的头指针。 */ LinkList CreatLink(int num) { int i, data; //p指向当前链表中最后一个结点,q指向准备插入的结点。 LinkList head = NULL, p = NULL, q; for (i = 0; i < num; i++) { scanf("%d", &data); q = (LinkList)malloc(sizeof(LNode)); q->data = data; q->next = NULL; if (i == 0) { head = q; } else { p->next = q; } p = q; } return head; }
Output linked list:
/* * 输出链表结点值。 */ int PrintLink(LinkList head) { LinkList p; for (p = head; p ;p = p->next) { printf("%-3d ", p->data); } return 0; }
2. Linked list insertion sort
Basic idea: Assume that the first n-1 nodes are in order, insert the nth node Go to the appropriate position of the previous node and order these n nodes.
Implementation method:
Remove the first node on the linked list to become a linked list containing one node (head1), and the remaining nodes will naturally become another A linked list (head2), at this time head1 is an ordered linked list containing
one node;
Remove the first node from the linked list head2 and insert it into the linked list head1 At the appropriate position, head1 is still in order. At this time, head1 becomes an ordered linked list containing two nodes;
removes one node from the linked list head2 in turn and inserts it into the linked list head1. Until the linked list head2 is an empty linked list. Finally, the linked list head1 contains all nodes, and the nodes are in order.
Insertion sort code:
/* * 链表插入排序(由小到大)。 * 输入:链表的头指针, * 输出:排序后链表的头指针。 * 实现方法:将原链表拆成两部分:链表1仍以head为头指针,链表结点有序。链表2以head2为头指针,链表结点无序。 * 将链表2中的结点依次插入到链表1中,并保持链表1有序。 * 最后链表1中包含所有结点,且有序。 */ LinkList LinkInsertSort(LinkList head) { //current指向当前待插入的结点。 LinkList head2, current, p, q; if (head == NULL) return head; //第一次拆分。 head2 = head->next; head->next = NULL; while (head2) { current = head2; head2 = head2->next; //寻找插入位置,插入位置为结点p和q中间。 for (p = NULL, q = head; q && q->data <= current->data; p = q, q = q->next); if (q == head) { //将current插入最前面。 head = current; } else { p->next = current; } current->next = q; } return head; }
Complete source code:
/* * 链表插入排序,由小到大 */ #define _CRT_SECURE_NO_WARNINGS #include#include #define TOTAL 10 //链表长度 //链表的存储表示 typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *next; }LNode, *LinkList; LinkList CreatLink(int num); LinkList LinkInsertSort(LinkList head); int PrintLink(LinkList head); /* * 创建链表。 * 形参num为链表的长度,函数返回链表的头指针。 */ LinkList CreatLink(int num) { int i, data; //p指向当前链表中最后一个结点,q指向准备插入的结点。 LinkList head = NULL, p = NULL, q; for (i = 0; i < num; i++) { scanf("%d", &data); q = (LinkList)malloc(sizeof(LNode)); q->data = data; q->next = NULL; if (i == 0) { head = q; } else { p->next = q; } p = q; } return head; } /* * 链表插入排序(由小到大)。 * 输入:链表的头指针, * 输出:排序后链表的头指针。 * 实现方法:将原链表拆成两部分:链表1仍以head为头指针,链表结点有序。链表2以head2为头指针,链表结点无序。 * 将链表2中的结点依次插入到链表1中,并保持链表1有序。 * 最后链表1中包含所有结点,且有序。 */ LinkList LinkInsertSort(LinkList head) { //current指向当前待插入的结点。 LinkList head2, current, p, q; if (head == NULL) return head; //第一次拆分。 head2 = head->next; head->next = NULL; while (head2) { current = head2; head2 = head2->next; //寻找插入位置,插入位置为结点p和q中间。 for (p = NULL, q = head; q && q->data <= current->data; p = q, q = q->next); if (q == head) { //将current插入最前面。 head = current; } else { p->next = current; } current->next = q; } return head; } /* * 输出链表结点值。 */ int PrintLink(LinkList head) { LinkList p; for (p = head; p ;p = p->next) { printf("%-3d ", p->data); } return 0; } int main() { LinkList head; printf("输入Total个数以创建链表:\n"); head = CreatLink(TOTAL); head = LinkInsertSort(head); printf("排序后:\n"); PrintLink(head); putchar('\n'); return 0; }
3. Linked list merge sort
Basic idea: If the linked list If it is empty or contains a node, the linked list is naturally ordered. Otherwise, split the linked list into two parts, merge sort each part separately, and then merge the two sorted linked lists together.
Merge sort code:
/* * 链表归并排序(由小到大)。 * 输入:链表的头指针, * 输出:排序后链表的头指针。 * 递归实现方法:将链表head分为两部分,分别进行归并排序,再将排序后的两部分归并在一起。 * 递归结束条件:进行递归排序的链表为空或者只有一个结点。 */ LinkList LinkMergeSort(LinkList head) { LinkList head1, head2; if (head == NULL || head->next == NULL) return head; LinkSplit(head, &head1, &head2); head1 = LinkMergeSort(head1); head2 = LinkMergeSort(head2); head = LinkMerge(head1, head2); return head; }
The linked list split function is as follows. The basic idea is to use slow/fast pointers. See the comments for the specific implementation method.
/* * 链表分割函数。 * 将链表head均分为两部分head1和head2,若链表长度为奇数,多出的结点从属于第一部分。 * 实现方法:首先使指针slow/fast指向链首, * 然后使fast指针向前移动两个结点的同时,slow指针向前移动一个结点, * 循环移动,直至fast指针指向链尾。结束时,slow指向链表head1的链尾。 */ int LinkSplit(LinkList head, LinkList *head1, LinkList *head2) { LinkList slow, fast; if (head == NULL || head->next == NULL) { *head1 = head; *head2 = NULL; return 0; } slow = head; fast = head->next; while (fast) { fast = fast->next; if (fast) { fast = fast->next; slow = slow->next; } } *head1 = head; *head2 = slow->next; //注意:一定要将链表head1的链尾置空。 slow->next = NULL; return 0; }
The linked list merge function has two methods: recursive implementation and non-recursive implementation:
Non-recursive implementation:
/* * 链表归并。 * 将两个有序的链表归并在一起,使总链表有序。 * 输入:链表head1和链表head2 * 输出:归并后的链表 * 实现方法:将链表head2中的结点依次插入到链表head1中的适当位置,使head1仍为有序链表。 */ LinkList LinkMerge(LinkList head1, LinkList head2) { LinkList p, q, t; if (!head1) return head2; if (!head2) return head1; //循环变量的初始化,q指向链表head1中的当前结点,p为q的前驱。 p = NULL; q = head1; while (head2) { //t为待插入结点。 t = head2; head2 = head2->next; //寻找插入位置,插入位置为p和q之间。 for (;q && q->data <= t->data; p = q, q = q->next); if (p == NULL) head1 = t; else p->next = t; t->next = q; //将结点t插入到p和q之间后,使p重新指向q的前驱。 p = t; } return head1; }
Recursive implementation:
LinkList LinkMerge2(LinkList head1, LinkList head2) { LinkList result; if (!head1) return head2; if (!head2) return head1; if (head1->data <= head2->data) { result = head1; result->next = LinkMerge(head1->next, head2); } else { result = head2; result->next = LinkMerge(head1, head2->next); } return result; }
Complete source code:
/* * 链表归并排序,由小到大。 */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #define TOTAL 10 //链表长度 //链表的存储表示 typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *next; }LNode, *LinkList; LinkList CreatLink(int num); LinkList LinkMergeSort(LinkList head); LinkList LinkMerge(LinkList head1, LinkList head2); LinkList LinkMerge2(LinkList head1, LinkList head2); int LinkSplit(LinkList head, LinkList *head1, LinkList *head2); int PrintLink(LinkList head); /* * 创建链表。 * 形参num为链表的长度,函数返回链表的头指针。 */ LinkList CreatLink(int num) { int i, data; //p指向当前链表中最后一个结点,q指向准备插入的结点。 LinkList head = NULL, p = NULL, q; for (i = 0; i < num; i++) { scanf("%d", &data); q = (LinkList)malloc(sizeof(LNode)); q->data = data; q->next = NULL; if (i == 0) { head = q; } else { p->next = q; } p = q; } return head; } /* * 输出链表结点值。 */ int PrintLink(LinkList head) { LinkList p; for (p = head; p; p = p->next) { printf("%-3d ", p->data); } return 0; } int main() { LinkList head; printf("输入Total个数以创建链表:\n"); head = CreatLink(TOTAL); head = LinkMergeSort(head); printf("排序后:\n"); PrintLink(head); putchar('\n'); return 0; } /* * 链表归并排序(由小到大)。 * 输入:链表的头指针, * 输出:排序后链表的头指针。 * 递归实现方法:将链表head分为两部分,分别进行归并排序,再将排序后的两部分归并在一起。 * 递归结束条件:进行递归排序的链表为空或者只有一个结点。 */ LinkList LinkMergeSort(LinkList head) { LinkList head1, head2; if (head == NULL || head->next == NULL) return head; LinkSplit(head, &head1, &head2); head1 = LinkMergeSort(head1); head2 = LinkMergeSort(head2); head = LinkMerge(head1, head2); //非递归实现 //head = LinkMerge2(head1, head2); //递归实现 return head; } /* * 链表归并。 * 将两个有序的链表归并在一起,使总链表有序。 * 输入:链表head1和链表head2 * 输出:归并后的链表 * 实现方法:将链表head2中的结点依次插入到链表head1中的适当位置,使head1仍为有序链表。 */ LinkList LinkMerge(LinkList head1, LinkList head2) { LinkList p, q, t; if (!head1) return head2; if (!head2) return head1; //循环变量的初始化,q指向链表head1中的当前结点,p为q的前驱。 p = NULL; q = head1; while (head2) { //t为待插入结点。 t = head2; head2 = head2->next; //寻找插入位置,插入位置为p和q之间。 for (;q && q->data <= t->data; p = q, q = q->next); if (p == NULL) head1 = t; else p->next = t; t->next = q; //将结点t插入到p和q之间后,使p重新指向q的前驱。 p = t; } return head1; } LinkList LinkMerge2(LinkList head1, LinkList head2) { LinkList result; if (!head1) return head2; if (!head2) return head1; if (head1->data <= head2->data) { result = head1; result->next = LinkMerge(head1->next, head2); } else { result = head2; result->next = LinkMerge(head1, head2->next); } return result; } /* * 链表分割函数。 * 将链表head均分为两部分head1和head2,若链表长度为奇数,多出的结点从属于第一部分。 * 实现方法:首先使指针slow/fast指向链首, * 然后使fast指针向前移动两个结点的同时,slow指针向前移动一个结点, * 循环移动,直至fast指针指向链尾。结束时,slow指向链表head1的链尾。 */ int LinkSplit(LinkList head, LinkList *head1, LinkList *head2) { LinkList slow, fast; if (head == NULL || head->next == NULL) { *head1 = head; *head2 = NULL; return 0; } slow = head; fast = head->next; while (fast) { fast = fast->next; if (fast) { fast = fast->next; slow = slow->next; } } *head1 = head; *head2 = slow->next; //注意:一定要将链表head1的链尾置空。 slow->next = NULL; return 0; }
The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.
For more JavaScript implementation of linked list insertion sort and linked list merge sort related articles, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.
