1. Knowledge points involved
1. Definition of non-ascending insertion
A one-way linked list is a data structure in which elements are connected in a linear manner, each pointing to the next element. Non-ascending insertion means that elements are not inserted into the linked list in ascending order (from small to large). Instead, elements can be inserted into the linked list in any order, depending on the needs of the application.
2. Non-ascending insertion is not a random insertion
In C# unidirectional linked lists, non-ascending order insertion does not mean random insertion. It means inserting a new node at the appropriate location in the linked list based on the size of the node data, rather than simply inserting it to the end of the linked list.
When using non-ascending inserts, the new node is compared with the existing node based on its data size and then inserted into the correct position in the linked list. This means that the insertion location of a new node depends on its data size and the data size of the existing nodes in the linked list.
Here is how the InsertUnAscending method works:
- If the linked list is empty, the new node will be inserted as the head node of the linked list.
- If the linked list is not empty, a temporary node (temp) will be used to traverse the linked list from the beginning node.
- During the traversal process, the data of the new node is compared with the data size of the next node of the current node (temp). If the data of the next node of the current node is smaller than the data of the new node, it will be moved to the next node.
- When the appropriate location is found, insert the new node before the next node of the current node (temp), and then set the next node of the new node to the next node of the current node.
- Finally, set the next node of the current node (temp) as the new node.
This method ensures that the node data in the linked list remains in non-ascending order.
method
The AddLast method is a method of the LinkedList<T> class in C#, which is used to add an element at the end of the linked list. This method can be used when you want to add elements to the linked list in any order.
The following is the syntax of the AddLast method:
public void AddLast(T value);
Where, T is a type parameter, indicating the type of elements in the linked list.
2. Examples
1. Custom unidirectional linked list and non-ascending insertion method
// One-way linked list implementation: method of inserting data in non-ascending ordernamespace _131_7 { public class Node { public int Data { get; set; } public Node? Next { get; set; } } public class LinkedList { private static Node? _head; public LinkedList() { _head = null; } /// <summary> /// Method for inserting node data in non-ascending order /// Non-ascending order insert means that elements are not inserted into the linked list in ascending order /// Instead, elements can be inserted into the linked list in any order /// The specific implementation is determined by the requirements of the program /// </summary> public static void AddNonAscending(int data) { Node newNode = new() { Data = data }; if (_head == null) { _head = newNode; } else { Node? temp = _head; while ( != null && < data) { temp = ; } = ; = newNode; } } // traverse the linked list and print the data public static void PrintList() { Node? temp = _head; while (temp != null) { ( + " "); temp = ; } (); } } class Program { static void Main(string[] args) { (args); // Insert elements, the order is not ascending (11); (5); (3); (4); (2); (1); // traverse the linked list and print the elements (); } } } //Run result:/* 11 1 2 3 4 5 */
2. Use the LinkedList<int> linked list class AddLast method to implement non-ascending order insertion
// Use the LinkedList<int> linked list class AddLast method to implement non-ascending order insertionnamespace _131_8 { class Program { static void Main(string[] args) { (args); LinkedList<int>? list = new(); // Insert elements, the order is not ascending (5); (3); (1); (4); (2); // traverse the linked list and print the elements LinkedListNode<int>? node = ; while (node != null) { (); node = ; } } } } //Run result:/* 5 3 1 4 2 */
As you can see from the example above, the elements are not inserted into the linked list in ascending order.
This is the article about the examples of C# unidirectional linked list implementing non-ascending insertion methods. For more related contents of C# unidirectional linked lists, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!