What is a linked list? What is the difference between a linked list and an array?

Source: Internet
Author: User

Related knowledge collation of linked list

What is a linked list

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

The difference between a linked list and an array

Recalling the concept of an array, the so-called array, is a set of elements of the same data type arranged in a certain order. According to the concept we can know that the array is contiguous in memory, the list is discontinuous, because different storage methods cause the array to statically allocate memory, the list dynamically allocates memory, the array element is in the stack area, the list element is in the heap area, because the array is contiguous in memory, we can use subscript to locate, time complexity is O (1), The list locates elements time complexity O (n), but because of the array's continuity array inserts or deletes elements of time complexity O (n), the time Complexity O (1) of the linked list. To summarize, the difference between an array and a linked list is as follows
1. Array statically allocates memory, the list dynamically allocates memory
2. The array is contiguous in memory, the list is discontinuous
3. Array elements in the stack area, linked list elements in the heap area
4. Array using subscript positioning, time complexity of O (1), linked list positioning element time complexity O (n);
5. The time complexity of an array to insert or delete elements O (n), the time Complexity O (1) of the linked list.

C # implements the basic operation of a linked list

In the case of a single linked list, we first define the data structure of the linked list node according to the definition of the list.

    public class node<t>    {        private T data;        Private node<t> Next;        The parameter constructor        //main use case instantiation needs to be processed with public node        (T item, node<t> next)        {            data = Item;            This.next = next;        }        No parameter constructor, use case instantiation node public nodes        ()        {            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 implement the operation of the list, construct a linked list, in the structure of the list we set a head node of the object, the head node is a very useful node, in the subsequent code can slowly realize

    public class mylinklist<t>    {public       node<t> Head {get; set;}        Constructor public          mylinklist ()        {            Head = null;        }    }

1. Find the length of the list, ideas: from the head node back to access, until the last nodes, 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 list, this is relatively simple, directly set the head node to null, the code is as follows

        public void Clear ()        {            Head = null;        }

3. To determine whether a linked list is empty also to use the head node

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

4. Add a new element at the end of the list, add a new element, you need to determine whether the linked list is empty, if it is empty we want to assign a value to the head node, if not empty need to modify the last node's next point, 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 the I node of the single-linked list, first need to find the insertion position, and then modify the adjacent node point, the code is as follows

        public void Insert (T item, int i)        {            if (IsEmpty () | | < 1 | | i > GetLength ())            {                return;            }            If you insert in the first position, you only need to point the node's next to 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 the specified node, first find the previous node to delete, and then modify that node's next point to the code slightly ....

· 7. Linked list also delete, Access, find and other operations, the basic ideas are the same, do not introduce

Linked list of related classic topics

1. Finding the number of nodes in a single linked list
2. Reverse the single-linked list
3. Finding the reciprocal K-node in a single-linked list (k > 0)
4. Find the middle node of a single linked list
5. Print a single linked list from the end of the head
6. It is known that two single-linked lists PHead1 and pHead2 are in order, merging them into one linked list is still orderly.
7. Determine if there is a ring in a single linked list
8. Determine if two single-linked lists Intersect
9. Find the first node where two single-linked lists Intersect
10. It is known that a ring exists in a single-linked list to enter the first node in the ring
11. Give a single-linked head pointer phead and a node pointer ptobedeleted,o (1) Time Complexity Delete node ptobedeleted

All right, I'm going to go here, the title is the title of the sword, you can answer the question, contact me

  

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.