Difference between static linked list and dynamic linked list

The difference between static linked list and dynamic linked list:

Static linked list and dynamic linked list are two different representations of linear list linked storage structure.

1. The static linked list is implemented by a method similar to an array. It is a sequential storage structure, which is continuous in physical address and needs to be pre-allocated the size of the address space. Therefore , the initial length of a static linked list is generally fixed, and it is not necessary to move elements during insertion and deletion operations, but only to modify the pointer.

2. The dynamic linked list uses the memory application function (malloc/new) to dynamically apply for memory, so there is no limit to the length of the linked list. Because the dynamic linked list dynamically applies for memory, the physical addresses of each node are not continuous, and they must be accessed sequentially through pointers.

1. Static linked list

The members in a structure can be pointer variables of various types. When the base type of one or more members in a structure is the structure type, the structure is called a "structure that references itself". Such as:

struct node
{
  char ch;
    int num;
  struct node *p;
};

struct node a; 	//declare a structure variable

p is a pointer member that can point to a variable of type struct node. Therefore, ap = &a is a legal expression, and the resulting storage structure is shown in the following figure:

The reference program is as follows:

/*****************************************************
Copyright (C) 2017-2018 All rights reserved.
File name    : static_link.c
Version      : v1.0       
Author: Zhengqijun
Date : Tue 10 Oct 2017 15:12:30
Description  :
Function List :
*****************************************************/

#include <stdio.h>

/* static linked list */
struct node
{
    int num;
    struct node *next;
};

intmain()
{
    struct node stu[3];
    struct node *head, *p;

    stu[0].num = 10; //Assign the num member of the node
    stu[1].num = 20;
    stu[2].num = 30;

    head = &stu[0]; //The head pointer points to the first node stu[0]
    stu[0].next = &stu[1]; //Assign the address of the node stu[1] to the next member of the stu[0] node
    stu[1].next = &stu[2]; //Assign the address of the node stu[2] to the next member of the stu[1] node
    stu[2].next = NULL; //stu[2] is the last node, its next member does not store the address of any node, it is set to NULL

    // Traverse static linked list
    p = head; //make the p pointer also point to the first node
    
    do{
        printf("%d\n", p->num); //Output the data of the node pointed to by p
        p = p->next; //Then let p point to the next node
    } while (p != NULL); //until the next member of p is NULL, that is, the traversal is completed

    return 0;
}

The output is:

root@ubuntu:~/2017/1010$ ./static_link
10
20
30

2. Dynamic linked list

So far, whenever we encountered "batch" data, we have used arrays to store it. Defining an array must specify (explicitly or implicitly) the number of elements, thus limiting the amount of data stored in an array. In practical applications, the amount of data that a program needs to process each time it is run is usually not certain. If the definition of the array is small, there is not enough space to store the data, and if the definition is large, the storage space is wasted.
In this case, if the storage space can be opened up at any time as needed during the execution of the program, and released at any time when not needed, the storage space can be used reasonably. The dynamic memory allocation of the C language provides this possibility. The addresses of the storage units that are dynamically allocated each time are not necessarily continuous, and the batch data to be processed is often a whole, and there is a sequential relationship between the data. In each node of the linked list, in addition to the data field for storing the data itself, at least one pointer field is required, which is used to store the address of the next node element, so as to connect the nodes through these pointers. Since each storage unit of the linked list is obtained by dynamic storage allocation, such a linked list is called a "dynamic linked list".

The reference program is as follows:

/*****************************************************
Copyright (C) 2017-2018 All rights reserved.
File name    : dynamic_link.c
Version      : v1.0       
Author: Zhengqijun
Date : Tue 10 Oct 2017 15:31:59
Description  :
Function List :
*****************************************************/

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

/* The so-called dynamic linked list refers to the establishment of a linked list from scratch during the execution of the program, that is, to open up nodes one by one and input the data of each node, and establish a relationship between the front and back. */
struct Student
{
    int No; //student number
    struct Student *next;
};

intmain()
{
    struct Student *p1, *p2;
	struct Student *head, *p;

    int n = 0; //Number of nodes

    head = NULL;
    p1 = (struct Student *)malloc(sizeof(struct Student));
    printf("Please enter the first student number\n");
    scanf("%d", &p1->No);

    p2 = p1; //At the beginning, both p1 and p2 point to the first node
    while (p1->No != 0)
    {
        n++;
        if (n == 1)
        {
            head = p1;
        }
        else
        {
            p2->next = p1;
        }

        p2 = p1; //p2 is the last node
        printf("Please enter student number, enter 0 to terminate:\n");
        p1 = (struct Student *)malloc(sizeof(struct Student));
        scanf("%d", &p1->No);
    };

    p2->next = NULL;//After the input is completed, p2->next is NULL

    // Traverse the dynamic linked list
	p = head;
    printf("\nStudent number is:\n");

    while (p != NULL)
    {
        printf("%d\n", p->No);
        p = p->next;
    }

    return 0;
}

The output is:

 
 
root@ubuntu:~/2017/1010$ ./dynamic_link 
Please enter the first student number 1 please enter the student number, enter 0 to terminate: 2, please enter the student number, enter 0 to terminate: 3, please enter the student number, enter 0 to terminate: 4, please enter the student number, enter 0 to terminate: 0 student number For: 1234
 
 
 
 

Note: In the dynamic linked list, each node does not have its own name, and the relationship between nodes can only be maintained by pointers. Once the pointer of a node is "broken", subsequent nodes can no longer be found!


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325855841&siteId=291194637