[Data Structure] Linked List Details

Let's not talk nonsense and get straight to the point.

introduce

What is a linked list?

Let's see what Baidu said:

A linked list is a non-sequential and non-sequential storage structure on a physical storage unit, and the logical order of data elements is realized through the link order of pointers in the linked list. The linked list is composed of a series of nodes (each element in the linked list is called a node), and the nodes can be dynamically generated at runtime. Each node consists of two parts: one is a data field that stores data elements, and the other is a pointer field that stores the address of the next node. Compared with the linear list sequential structure, the operation is more complicated. Since it does not have to be stored in order, the linked list can reach the complexity of O(1) when inserting, which is much faster than another linear list sequence table, but it takes O(n) to find a node or access a specific numbered node time, and the corresponding time complexities of linear table and sequential table are O(logn) and O(1) respectively.

It can be found:

Each node consists of two parts: one is a data field that stores data elements, and the other is a pointer field that stores the address of the next node .

The linked list can reach the complexity of O(1) when inserting , which is much faster than another linear list sequence table, but it takes O(n) time to find a node or access a specific numbered node, while the linear list and The corresponding time complexities of sequence table are O(logn) and O(1) respectively.

These are the characteristics of linked lists.

This is a conceptual diagram of a linked list. The three strings of Blue, Yellow, and Red are stored as data in the data field of the linked list. Each data has a "pointer", which points to the memory address of the next data, which is called a pointer field .


After understanding the concept, let's take a look at the basic operation .

1. Create

See above:

Each node consists of two parts: one is a data field that stores data elements, and the other is a pointer field that stores the address of the next node.

A node can be stored in a structure (represented as Lnode here).

The data field is very simple, and we usually use another structure to store it.

And what about pointers?

In the creation of the linked list, we need to add a pointer to the next node , which stores the address of the next node .

So what is the type of the pointer? Of course it is Lnode, because the pointer points to the next node.

Here is a paragraph to help understand:

Assigning a variable to a pointer is actually assigning the address of the variable to the pointer, or conversely, the memory address of the variable is stored in the pointer, pointing to the variable, and the variable can be found through the pointer.

Then, it can be realized.

struct Lnode{
    ElemType data;//数据域
    struct Node *next;//指针域
};

2. Initialization

It is said to be initialized, but it is actually creating a node. Then, just open up a space of type Lnode .

How to open up space?

We need a new .

What is new ?

new is actually the computer opening up a new space, but unlike general declarations, the space opened up by new is on the heap, while the variables declared in general are stored on the stack. Generally speaking, when a new space is created in a local function, this space can still be used after the local function call ends, and can be used to pass parameters to the main function. In addition, it should be noted that in the format of new, what new comes out is the first address of a space. Therefore, it is generally necessary to use a pointer to store this address.
————————————————
Copyright statement: This content is the original
text Link to the original text: https://blog.csdn.net/m0_72542983/article/details/ 128977214

Forget it, stop talking nonsense, search for yourself! Then, come on.

real initialization

However, there are no other nodes behind this node, so the pointer field should be empty.

Here, we use NULL to represent.

What is NULL?

Null is a value reserved in computations to indicate that a pointer does not refer to a valid object.

This sentence clearly points out.

In C++, NULL can be directly assigned to a pointer. So it's easy!

Code

void InitList(Lnode* &L)//初始化 
{
    L=new Lnode;
    L->next=NULL;
}

3. Determine whether it is an empty linked list

First of all, we need to know, what is an empty linked list?

Empty linked list: There are no elements in the linked list. (head pointer and head node are still there)

That is to say, the pointer field of the head node points to empty

Well, it's very simple.

bool check(Lnode* L)//判断是否为空 
{
    if(L->next==NULL) return 0;//空 
    return 1;
}

4. Empty the linked list (do not leave the head node)

Use an L to represent the current head node, and use p to represent the current node.

Each time, p is transferred to the next one of L, and then L is released, and L is changed to p. This loop continues until p is empty.

code show as below:

void xiaohui(linklist &L)//全部删掉 (不留头结点)
{
    Lnode *p;//linklist L
    while(p)
    {
        L=p;
        p=p->next;
        free(L);
    }
} 

5. Become an empty table (keep the head node)

There is only one difference between this and the above. The title is written very clearly: keep the head node .

Then we only use q instead of L to help p find the way. Then, when p is NULL, we can also know the pointer of the original head node—that is, L

Ok, here is the code:

void qk(linklist &L)//变成空表(留头结点) 
{
    Lnode *p,*q;//linklist L
    q=L->next;
    while(p)
    {
        p=q;
        q=q->next;
        free(p);
    }
    L->next=NULL; 
}

5. Search and access

Compared with the array, the data of the linked list is scattered and stored in the random position of the array.

Because the data is stored scattered, if you want to access the data, you can only start from the first data and follow the pointers to access one by one (this is sequential access ). For example, if you want to find the data of Red, you have to start accessing from Blue.

After that, we have to go through Yellow before we can find Red.

Also relatively basic, on the code!

int cz(linklist L,char mz[])//查找 
{
    Lnode *p=L->next;
    while(p)
    {
        if(strcmp(mz,p->data.name)==0) break;
        p=p->next;
    }
    if(!p)
    {
        return 0;
    }
    return 1;
}


6. Add

如果想要添加数据,只需要改变添加位置前后 的指针指向就可以,非常简单。比如,在Blue 和Yellow之间添加Green。

将 Blue 的指针指向的位置变成 Green,然后再 把Green的指针指向Yellow,数据的添加就大功告成了。

代码如下:

int cr(linklist &L,int x,ElemType e)//插入
{
    int i=0;
    Lnode *p=L;
    while(p&&i<x-1)
    {
        p=p->next;
        i++;
    }
    Lnode *s=new Lnode;
    s->data=e;
    s->next=p->next;
    p->next=s;
    return 1;
}

七.删除

数据的删除也一样,只要改变指针的指向就可 以,比如删除Yellow。

这时,只需要把 Green 指针指向的位置从 Yellow 变成 Red,删除就完成了。但是, Yellow 本身还存储在 内存中,需要把它释放掉(可以用free数组)

看代码:

int cr(linklist &L,int x,ElemType &e)//删除第x个节点,并返回删除值 
{
    int i=1;
    Lnode *p=L->next,*t;
    while(p&&i<x-1)
    {
        p=p->next;
        i++;
    }
    t=p->next;
    p->next=p->next->next;
    free(t);
    return 1;
}

基本操作汇总

#include<bits/stdc++.h>
using namespace std;
typedef struct student{
    char name[8];
    int num;
    int cj;
}ElemType;
typedef struct Lnode{
    ElemType data;
    struct Lnode *next;
}Lnode,*linklist;
void InitList(linklist &L)//初始化 
{
    L=new Lnode;
    L->next=NULL;
}
bool check(linklist L)//判断是否为空 
{
    if(L->next==NULL) return 0;//空 
    return 1;
}
void xiaohui(linklist &L)//全部删掉 
{
    Lnode *p;//linklist L
    while(p)
    {
        L=p;
        p=p->next;
        free(L);
    }
}
void qk(linklist &L)//变成空表(留头结点) 
{
    Lnode *p,*q;//linklist L
    q=L->next;
    while(p)
    {
        p=q;
        q=q->next;
        free(p);
    }
    L->next=NULL; 
}
int bc(linklist &L)//链表的长度
{
    int s=0;
    Lnode *p;
    p=L->next;
    while(p)
    {
        s++;
        p=p->next;
    }
    return s;
}
int qz(linklist &L,int x,ElemType &a)//取第x个的值
{
    int i=1; 
    Lnode *p=L->next;
    while(p&&i<x)
    {
        p=p->next;
        i++;
    }
    if(!p)
    {
        return 0;
    }
    a=p->data;
    return 1;
}
int cz(linklist L,char mz[])//查找 
{
    Lnode *p=L->next;
    while(p)
    {
        if(strcmp(mz,p->data.name)==0) break;
        p=p->next;
    }
    if(!p)
    {
        return 0;
    }
    return 1;
}
int cr(linklist &L,int x,ElemType e)//插入
{
    int i=0;
    Lnode *p=L;
    while(p&&i<x-1)
    {
        p=p->next;
        i++;
    }
    Lnode *s=new Lnode;
    s->data=e;
    s->next=p->next;
    p->next=s;
    return 1;
}
int cr(linklist &L,int x,ElemType &e)//删除第x个节点,并返回删除值 
{
    int i=1;
    Lnode *p=L->next,*t;
    while(p&&i<x-1)
    {
        p=p->next;
        i++;
    }
    t=p->next;
    p->next=p->next->next;
    free(t);
    return 1;
}
int tc(int n,linklist &L)//建立头插法
{
    Lnode *p;
    for(int i=1;i<=n;i++)
    {
        p=new Lnode;
        //cin>>p->data;
        p->next=L->next;
        L->next=p;
    }
}
int tc(int n,linklist &L)//建立尾插法
{
    Lnode *p,*r=L;
    for(int i=1;i<=n;i++)
    {
        p=new Lnode;
        p->next=NULL;
        //cin>>p->data;
        r->next=p;
        r=p;
    }
} 
int main()
{
    
}

最后

因为太懒了,就拖了很久,今天开网,终于发布了,麻烦来个三连,谢谢!

Guess you like

Origin blog.csdn.net/aliyonghang/article/details/129341980