当前位置:网站首页>About one-way linked list

About one-way linked list

2022-06-10 11:15:00 Learn Niuniu

1、 Single chain list

Single - linked list is a chain - access data structure , A set of storage units with arbitrary addresses are used to store the data elements in the linear table . The data in a linked list is represented by nodes , The composition of each node : Elements ( The image of the data element )+ The pointer ( Indicates where subsequent elements are stored ), An element is a data unit that stores data , A pointer is the address data that connects each node .

2、 Data structure of single linked list

head Is the head node , Don't store any data , Its function is to point to the first node in the linked list that actually stores data
among DATA For data fields , This memory stores data ;NEXT It's an address field , The data type is pointer , This memory stores the memory address of the next memory segment .

3、 Headlined list

A linked list with a header means that there is a header node when the list is initialized , And the header pointer always points to the header node , There is no data in the data field of this header node . As shown in the figure

 4、 Circular linked list

Circular linked list refers to the end node pointer of the linked list pointing to the head node address , So as to form a ring, as shown in the figure :


Source code is as follows :

#include <stdio.h>
	
struct link  // Structure 
{
    int data;  // Data fields 
    struct  link *next;  // Pointer to the domain 
};

int main()
{
    struct link n0, n1, n2;
    n0.data = 10; 
    n1.data = 20;
    n2.data = 30;
    n0.next = &n1;
    n1.next = &n2;
    n2.next = NULL;
    printf("%d\n", n0.data);
    printf("%d\n", n0.p->data);
    printf("%d\n", n1.p->data);
    return 0;
}

5、*“.” and “->” The difference between
C In language “.” What you give is an immediate address ,“->” A pointer is given ."." The left operand of is a value ,"->" The left operand of is a pointer ."." and "->" In fact, it can be merged into one operator , In many new languages, the members of values and pointers have been unified as "." 了 ,C It is only a continuation of tradition .
6、* Structure
The structure is in C Language belongs to the custom data type , We know that an array is a collection of the same data type , That structure can be seen as a collection of different data types , For example, we need to store the name of a game character 、 Grade 、 Blood volume value 、 Defense value 、 occupation 、 Skill level information , We will find that these attributes correspond to different data types , In this case , In order to better correspond to the role , We can encapsulate all their relevant information into one type , At this point, we will use the structure to perform the consent encapsulation , So the structure is also C A very common data type in language programming
Traverse 、 Head insertion 、 Tail inserted linked list

#include <stdio.h>
#include <stdlib.h>

struct links
{
    int data;
    struct  links *next;    
};

void showList(struct links *p)// List traversal 
{
    while(p)
    {
        printf("%d\n", p->data);
        p = p->next;
    }
}

void installTail(struct links **h, struct links *n)// The tail interpolation 
{
    n->next = *h;
    *h = n;
}

struct links *headInsert(struct links *h, struct links *n)// The first interpolation 
{
    n->next = h;
    return n;
}
int main()
{
    struct links *head = NULL;
 struct links *h1 = NULL;
    int i = 0;
    int data[10] = {1,2,3,4,5,6,7,8,9,10};
    while (i < (sizeof(data) / sizeof(int)))
    {
        h1 = malloc(sizeof(struct links));
        h1->data = data[i];
        h1->next = NULL;
        installTail(&head,h1);
        //head = headInsert(head,h1);
        i++;
    }
   showList(head);
    return 0;
}

*malloc Dynamic memory allocation , It is used to apply for a continuous memory block area of specified size to void* Type returns the address of the allocated memory area , When you can't know the exact location of memory , Want to bind real memory space , We need to use dynamic memory allocation , And the allocated size is the size required by the program .

At last, a poem by Mr. Qiu Jin is attached for your appreciation
sacred · Live in Beijing
Live in Beijing , It's the Mid Autumn Festival again . For the yellow flowers under the fence , Autumn looks like wiping .
Songs on all sides end up breaking Chu , Eight years of flavor, Tusi Zhejiang .
I will send Nong Qiang as a moth eyebrow , I don't care !
No body , Male column . The heart is better than , Man lie !
All my life , Because people are often hot . Who knows me ? The end of a hero is a struggle .
Where can I find a bosom friend ? Green shirt wet !

原网站

版权声明
本文为[Learn Niuniu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101107577086.html