Table of Contents
Related knowledge of linked lists
What is a linked list
The difference between linked lists and arrays
C# Implement the basic operations of a linked list
 3. Find the Kth node from the last in the singly linked list (k > 0)
Home Backend Development C#.Net Tutorial What is a linked list? What is the difference between linked list and array?

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

Jun 24, 2017 am 09:50 AM
tidy Related Knowledge

Related knowledge of linked lists

What is a linked list

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

The difference between linked lists and arrays

Recall the concept of arrays. The so-called array is a collection of elements of the same data type arranged in a certain order. According to the concept, we can know that arrays are continuous in memory and linked lists are not continuous; due to different storage methods, arrays statically allocate memory and linked lists dynamically allocate memory. Array elements are in the stack area and linked list elements are in the heap area. Since arrays are continuous in memory, We can use subscripts to locate, the time complexity is O(1), and the time complexity of locating elements in the linked list is O(n); however, due to the continuity of the array, the time complexity of inserting or deleting elements from the array is O(n), and the time complexity of the linked list is O(n). Complexity O(1). To summarize, the difference between arrays and linked lists is as follows
1. Arrays allocate memory statically, linked lists dynamically allocate memory
2. Arrays are continuous in memory, linked lists are discontinuous
3. Array elements are in the stack area, and linked list elements are in the stack area In the heap area
 4. The array is positioned using subscripts, and the time complexity is O(1). The time complexity of locating elements in the linked list is O(n);
 5. The time complexity of inserting or deleting elements from the array is O( n), the time complexity of the linked list is O(1).

C# Implement the basic operations of a linked list

Taking a singly linked list as an example, according to the definition of a linked list, we first define the data structure of the linked list node

    public class Node<T>
    {
        private T data;
        private Node<T> next;

        //有参构造函数
        //主要用例实例化需要处理的节点用
        public Node(T item, Node<T> next)
        {
            data = item;
            this.next = next;
        }

        //无参构造函数,用例实例化Node节点
        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; }
        }
    }
Copy after login

Next, let's implement the operation of the linked list and construct a linked list. In the constructed linked list, we define an object of the head node. The head node is a very useful node. You can slowly realize it in the subsequent code

    public class MyLinkList<T>
    {
       public Node<T> Head { get; set; }

        //构造器  
        public MyLinkList()
        {
            Head = null;
        }
    }
Copy after login

1. Find the length of the linked list, idea: visit backward from the beginning node until the last node, the code is as follows

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

2. Clear the linked list, this is It is relatively simple. Just set the head node to null. The code is as follows

        public void Clear()
        {
            Head = null;
        }
Copy after login

3. In the same way, the head node is also used to determine whether the linked list is empty

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

4. Add a new element at the end of the linked list. To add a new element, you need to first determine whether the linked list is empty. If it is empty, we need to assign a value to the head node. If it is not empty, you need to modify the last node. The next point of a node points to the following code. Just point to the adjacent 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);
        }
Copy after login

6. To delete the specified node, first find the previous node to be deleted, and then modify the next point of the node. Code slightly. . . .

· 7. Linked lists also have operations such as deletion, acquisition, and search. The basic ideas are the same, so I will not introduce them one by one.

Classic topics related to linked lists

1. Find the number of nodes in the singly linked list

 2. Reverse the singly linked list

 3. Find the Kth node from the last in the singly linked list (k > 0)

 4. Find the single linked list The intermediate node of the linked list

 5. Print the singly linked list from end to head
 6. It is known that the two singly linked lists pHead1 and pHead2 are each in order, and merging them into one linked list will still be in order
 7. Determine a Whether there is a cycle in a singly linked list
8. Determine whether two singly linked lists intersect
9. Find the first node where two singly linked lists intersect
10. It is known that there is a cycle in a singly linked list, find the entry The first node in the ring
11. Given a single linked list head pointer pHead and a node pointer pToBeDeleted, O(1) time complexity is required to delete the node pToBeDeleted



Okay That’s it for now. The questions are from the Jianzhi offer. You can answer them. If you have any questions, please contact me

 

The above is the detailed content of What is a linked list? What is the difference between linked list and array?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Let you learn about the shocking win10x system knowledge Let you learn about the shocking win10x system knowledge Jul 14, 2023 am 11:29 AM

Recently, the latest image download of win10X system has been leaked on the Internet. Different from the common ISO, this image is in .ffu format and can currently only be used for Surface Pro7 experience. Although many friends can’t experience it, you can still read the relevant content of the evaluation and enjoy it. Let’s take a look at the latest evaluation of the win10x system! The latest evaluation of the win10x system 1. The biggest difference between Win10X and Win10 first appears after booting up. Buttons are placed in the center of the taskbar. In addition to pinned applications, the taskbar can also display recently launched applications, similar to Android and iOS phones. 2. Another thing is that the “Start” menu of the new system does not support file

Tips for organizing desktop icons in Win10 Tips for organizing desktop icons in Win10 Dec 27, 2023 pm 05:00 PM

Friends who use computers all want their desktop to be arranged cleanly and neatly, but they don’t know how to operate it in the win10 system. Today I will bring you a win10 method to organize desktop icons, let’s take a look. How to organize desktop icons in Windows 10: 1. Right-click a blank space on the desktop and click "View" at the top. 2. In the window on the right, you can see functions such as "Automatically arrange icons". 3. Do not check "Automatically arrange icons" so that you can place the icons according to your needs. 4. Moreover, all these options can be selected, but this way you will not be able to express your own personality.

Step-by-step guide for creating folders on Realme mobile phone Step-by-step guide for creating folders on Realme mobile phone Mar 23, 2024 pm 03:51 PM

Step-by-Step Guide to Creating Folders on Realme Mobile Phones In our daily lives, we use our phones to store a variety of files, including photos, videos, documents, and more. However, as time goes by, the number of files on your phone may increase, causing insufficient memory on your phone or making it difficult to manage files. In order to better organize files and improve the efficiency of mobile phone use, the establishment of mobile phone folders has become particularly important. Realme Mobile is a popular smartphone brand with a powerful system and diverse features. Creating folders on Realme phones can help users better organize files

How to defragment the disk in win10 system How to defragment the disk in win10 system Jun 29, 2023 pm 07:41 PM

How to defragment the disk in win10 system? As the usage of computers is getting higher and higher, we may sometimes encounter the setting of disk defragmentation in win10 system. If we need to set up the disk defragmentation in win10 system, how to deal with the defragmentation of disk in win10 system? Many friends don’t know how to operate it. The editor below has compiled the detailed steps for defragmenting the disk in win10 system. If you are interested, follow the editor to read below! Detailed steps for defragmenting disks in win10 system: 1. What kind of disks need to be defragmented? We need to know that not all disks need to be defragmented, such as very common solid-state hard drives and removable disk devices.​

How does a chatbot answer questions through a knowledge graph? How does a chatbot answer questions through a knowledge graph? Apr 17, 2023 am 09:13 AM

Preface In 1950, Turing published the landmark paper "Computing Machinery and Intelligence" (Computing Machinery and Intelligence), proposing a famous judgment principle about robots - the Turing test, also known as the Turing judgment, which states that if the first If the three cannot distinguish the difference between the responses of humans and AI machines, it can be concluded that the machine has artificial intelligence. In 2008, the AI ​​butler Jarvis in Marvel's "Iron Man" let people know how AI can accurately help humans (Tony) solve various matters thrown at them... Figure 1: AI butler Jarvis ( Picture source: Internet) In early 2023, Chat, a free chat robot that broke out in the technology world in a 2C way, became popular.

Understanding Golang: essential knowledge for developers Understanding Golang: essential knowledge for developers Feb 23, 2024 am 10:51 AM

Golang, also known as Go language, is an open source programming language developed by Google. Since its release in 2007, Golang has gradually emerged in the field of software development and has been favored by more and more developers. As a statically typed, compiled language, Golang has many advantages, such as efficient concurrent processing capabilities, concise syntax, and powerful tool support, making it have broad application prospects in cloud computing, big data processing, network programming, etc. . This article will introduce the basic concepts of Golang,

Let's talk about knowledge extraction. Have you learned it? Let's talk about knowledge extraction. Have you learned it? Nov 13, 2023 pm 08:13 PM

1. Introduction Knowledge extraction usually refers to mining structured information from unstructured text, such as tags and phrases containing rich semantic information. This is widely used in the industry in scenarios such as content understanding and product understanding. By extracting valuable tags from user-generated text information and applying them to content or products, knowledge extraction is usually accompanied by the classification of the extracted tags or phrases. , is usually modeled as a named entity recognition task. The general named entity recognition task is to identify named entity components and classify the components into place names, person names, organization names and other types; domain-related tag word extraction identifies and divides tag words into Field-defined categories, such as series (Air Force One, Sonic 9), brand (Nike, Li Ning), type (shoes, clothing, digital), style (

Understanding Linux Server Security: Essential Knowledge and Skills Understanding Linux Server Security: Essential Knowledge and Skills Sep 09, 2023 pm 02:55 PM

Understanding Linux Server Security: Essential Knowledge and Skills With the continuous development of the Internet, Linux servers are increasingly used in various fields. However, since servers store a large amount of sensitive data, their security issues have also become the focus of attention. This article will introduce some essential Linux server security knowledge and skills to help you protect your server from attacks. Updating and Maintaining Operating Systems and Software Timely updating of operating systems and software is an important part of keeping your server secure. Because every operating system and software

See all articles