Sorting of linked list related knowledge and linked list related knowledge

Source: Internet
Author: User

Sorting of linked list related knowledge and linked list related knowledge
What is linked list?

A linked list is a non-sequential storage structure on a physical storage unit. The logical sequence of data elements is achieved through the pointer links in the linked list. A linked list consists of a series of nodes (each element in a linked list is called a node), which can be dynamically generated at runtime. Each node consists of two parts: one is the data domain that stores data elements, and the other is the pointer domain that stores the next node address.

Differences between linked lists and Arrays

Recall the concept of an array. An array is a set of elements of the same data type in a certain order. According to the concept, we can know that the array is continuous in the memory, and the linked list is not continuous. Due to different storage methods, the array static memory is allocated, and the linked list dynamically allocates memory. The array elements are in the stack area, the linked list elements are in the heap area. Because the array is continuous in the memory, we can locate the elements by subscript. the time complexity is O (1), and the time complexity is O (n ); however, due to the time complexity O (n) of inserting or deleting elements in the continuous array, the time complexity O (1) of the linked list ). To sum up, the differences between arrays and linked lists are as follows:
1. Static Array Memory Allocation and dynamic memory allocation through linked lists
2. The array is continuous in the memory and the linked list is not continuous.
3. array elements are in the stack area, and linked list elements are in the heap area.
4. The array is located by subscript. the time complexity is O (1), and the time complexity O (n) of the element located in the linked list );
5. the time complexity O (n) of inserting or deleting elements in the array, and the time complexity O (1) of the linked list ).

C # basic operations on linked lists

Take a single-chain table as an example. Based on the definition of the linked list, we first define the data structure of the linked list node.

Public class Node <T> {private T data; private Node <T> next; // constructor with parameters // use the public Node (T item, node <T> next) {data = item; this. next = next;} // No parameter constructor. Use Cases to instantiate the public Node () {data = default (T); next = null ;} public Node <T> Next {get {return next;} set {this. next = value ;}} public T Data {get {return data;} set {this. data = value ;}}}

Next we will implement the Linked List Operation to construct a linked list. In the construction of the linked list, we will set a head Node object. The head node is a useful node, in the subsequent code, you can gradually understand

Public class MyLinkList <T> {public Node <T> Head {get; set;} // constructor public MyLinkList () {Head = null ;}}

1. Find the length of the linked list. The Code is as follows:

       public int Length()        {            var p = Head;            int len = 0;            while (p != null)            {                ++len;                p = p.Next;            }            return len;        }

2. Clear the linked list. This is relatively simple. You can directly set the header node to null. The Code is as follows:

        public void Clear()        {            Head = null;        }

3. Similarly, determine whether the linked list is empty or not.

        public bool IsEmpty()        {            if (Head == null)            {                return true;            }            else            {                return false;            }        }

4. add new elements to the end of the linked list and add new elements. First, you need to judge whether the linked list is empty. If it is empty, we need to assign a value to the header node, if it is not empty, you need to modify the next point of the last node. The Code is as follows:

       public void Append(T item)        {            if (Head == null)            {                Head = new Node<T>(item, null);                return;            }            var p = new Node<T>();            p = Head;            while (p.Next != null)            {                p = p.Next;            }            p.Next = new Node<T>(item, null);        }

5. Insert a specified node before the position of node I in a single-chain table. First, locate the Insertion Location and modify the point of the adjacent node. The Code is as follows:

Public void Insert (T item, int I) {if (IsEmpty () | I <1 | I> GetLength () {return ;} // if you insert data at the first position, you only need to direct the next of the Node to the head. if (I = 1) {var first = new Node <T> (item, null); first. next = Head; Head = first; return;} var p = new Node <T> (); p = Head; var left = new Node <T> (); var right = new Node <T> (); int j = 1; while (p. next! = Null & j <I) {left = p; right = p. next; ++ j;} var q = new Node <T> (item, null); left. next = q; q. next = right ;}

6. delete a specified node. First, locate the previous node to be deleted, and then modify the next point of the node to get the Code omitted ....

· 7. There are also operations such as delete, retrieve, and search on the linked list. The basic idea is the same, so I will not discuss them one by one.

Linked List-related classic questions

1. Calculate the number of nodes in a single-chain table.
2. Reverse a single-chain table
3. Find the last K node in a single-chain table (k> 0)
4. Find the intermediate node of a single-chain table
5. Print a single-chain table from the end to the end
6. Two single-chain table pHead1 and pHead2 are known to be in order, and they are merged into a linked list.
7. check whether a single-chain table has loops.
8. Determine whether two single-chain tables are intersecting
9. Find the first node of the intersection of two single-chain tables
10. If a single-chain table is known to have a ring, find the first node in the ring.
11. Given a single-chain header pointer pHead and a node pointer pToBeDeleted, O (1) time complexity delete node pToBeDeleted

 

Now, let's just click here. The question "sword" refers to the question in the offer. You can answer this question and contact me if you have any questions.

  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.