


C language implements the merge of two ordered linked lists (code example)
This article introduces the method of merging two ordered linked lists using c language through code examples. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Tutorial recommendation: "c language tutorial video"
C language implements two Merging of ordered linked lists
There are two ordered singly linked lists, and the code can be used to merge the two singly linked lists into a new ordered list, which requires the use of the space of the old table. Memory cannot be newly allocated
#include <stdio.h>#include <stdlib.h>typedef struct List{ int a; struct List *next;}list;void newList(list *l){ //初始化头节点 l->next = NULL;}void setList(list * l){ //建立链表 int i = 1; int j; while (i) { scanf_s("%d", &j); if (j == -1) { i = 0; } else { list *l1 = (list *)malloc(sizeof(list));//为新的结点分派内存 l1->a = j;//储存数据 /* 将最后结点的next区域指向新结点 将新结点的next区域指向设置为空 */ l->next = l1; l1->next = NULL; l = l->next; } }}void printfList(list *l){ printf("该链表内容为:\n"); while (l->next) { printf("%d\t", l->next->a); l = l->next; } printf("\n");}list *add(list *LA, list *LB){ //记录两个链表的头结点 list *la=LA; list *l = LA; list *lb = LB; //移动指针 LA = LA->next; LB = LB->next; la->next = NULL; while (LA!=NULL&&LB!=NULL) { /* 将两个结点的数据进行比较,数据较小的结点接在头结点后面, */ if (LA->a < LB->a) { la->next = LA; la = LA; LA = LA->next; } else { la->next = LB; la = LB; LB = LB->next; } } //若其中一个链表的结点已经全接在新表中则将另一个链表的剩余结点接在新表的后面 if (LA) { la->next = LA; } if(LB) { la->next = LB; } free(lb); return l;}int main(){ //为结点分配内存 list *LA = (list *)malloc(sizeof(list)); list *LB = (list *)malloc(sizeof(list)); //初始化结点 newList(LA); newList(LB); //建立链表 setList(LA); setList(LB); //输出链表的内容 printf("LA的数据:\n"); printfList(LA); printf("LB的数据:\n"); printfList(LB); list *LC = add(LA, LB); //输出合并后的新表 printfList(LC); system("pause"); return 0;}
For more programming-related knowledge, please visit:Introduction to Programming! !
The above is the detailed content of C language implements the merge of two ordered linked lists (code example). For more information, please follow other related articles on the PHP Chinese website!

C# is not always tied to .NET. 1) C# can run in the Mono runtime environment and is suitable for Linux and macOS. 2) In the Unity game engine, C# is used for scripting and does not rely on the .NET framework. 3) C# can also be used for embedded system development, such as .NETMicroFramework.

C# plays a core role in the .NET ecosystem and is the preferred language for developers. 1) C# provides efficient and easy-to-use programming methods, combining the advantages of C, C and Java. 2) Execute through .NET runtime (CLR) to ensure efficient cross-platform operation. 3) C# supports basic to advanced usage, such as LINQ and asynchronous programming. 4) Optimization and best practices include using StringBuilder and asynchronous programming to improve performance and maintainability.

C# is a programming language released by Microsoft in 2000, aiming to combine the power of C and the simplicity of Java. 1.C# is a type-safe, object-oriented programming language that supports encapsulation, inheritance and polymorphism. 2. The compilation process of C# converts the code into an intermediate language (IL), and then compiles it into machine code execution in the .NET runtime environment (CLR). 3. The basic usage of C# includes variable declarations, control flows and function definitions, while advanced usages cover asynchronous programming, LINQ and delegates, etc. 4. Common errors include type mismatch and null reference exceptions, which can be debugged through debugger, exception handling and logging. 5. Performance optimization suggestions include the use of LINQ, asynchronous programming, and improving code readability.

C# is a programming language, while .NET is a software framework. 1.C# is developed by Microsoft and is suitable for multi-platform development. 2..NET provides class libraries and runtime environments, and supports multilingual. The two work together to build modern applications.

C#.NET is a powerful development platform that combines the advantages of the C# language and .NET framework. 1) It is widely used in enterprise applications, web development, game development and mobile application development. 2) C# code is compiled into an intermediate language and is executed by the .NET runtime environment, supporting garbage collection, type safety and LINQ queries. 3) Examples of usage include basic console output and advanced LINQ queries. 4) Common errors such as empty references and type conversion errors can be solved through debuggers and logging. 5) Performance optimization suggestions include asynchronous programming and optimization of LINQ queries. 6) Despite the competition, C#.NET maintains its important position through continuous innovation.

The future trends of C#.NET are mainly focused on three aspects: cloud computing, microservices, AI and machine learning integration, and cross-platform development. 1) Cloud computing and microservices: C#.NET optimizes cloud environment performance through the Azure platform and supports the construction of an efficient microservice architecture. 2) Integration of AI and machine learning: With the help of the ML.NET library, C# developers can embed machine learning models in their applications to promote the development of intelligent applications. 3) Cross-platform development: Through .NETCore and .NET5, C# applications can run on Windows, Linux and macOS, expanding the deployment scope.

The latest developments and best practices in C#.NET development include: 1. Asynchronous programming improves application responsiveness, and simplifies non-blocking code using async and await keywords; 2. LINQ provides powerful query functions, efficiently manipulating data through delayed execution and expression trees; 3. Performance optimization suggestions include using asynchronous programming, optimizing LINQ queries, rationally managing memory, improving code readability and maintenance, and writing unit tests.

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.


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

Atom editor mac version download
The most popular open source editor

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
