The difference between static linked list and dynamic linked list

Source: Internet
Author: User
Tags array definition

The difference between a static list and a dynamic list:

Static linked lists and dynamic lists are two different representations of the linear chain-type storage structure.

1. Static linked lists are implemented in an array-like manner, are sequential storage structures, are contiguous on physical addresses, and need to be allocated in advance to the size of the address space. Therefore, the initial length of the static linked list is usually fixed, do not need to move elements in the INSERT and delete operations, only need to modify the pointer.

2, dynamic list is using the memory application function (malloc/new) dynamic application of memory, so there is no limit on the length of the linked list. Dynamic list because the dynamic application of memory, so the physical address of each node is discontinuous, to be accessed sequentially through the pointer.

a static linked list

Members of a struct can be of various types of pointer variables, and when a struct has one or more members of the base type that is the type of the struct, the struct is called "reference to its own structure". Such as:

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

struct Node A;	Declare a struct variable

P is a pointer member that can point to a struct node type variable. Therefore, A.P = &a is a valid expression, and the resulting storage structure is shown in the following illustration:

The reference program looks like this:

/***************************************************** Copyright (C) 2017-2018 All rights reserved. 
File name:static_link.c version:v1.0 Author:zhengqijun date:2017 year October 10 Tuesday 15:12 30 seconds  Description:funcion list: *****************************************************/#include <stdio.h>/* static list
    * * struct node {int num;
struct node *next;

};
    int main () {struct node stu[3];

    struct node *head, *p;		Stu[0].num = 10;
    Assigning the NUM member of a node to Stu[1].num = 20;

    Stu[2].num = 30;		Head = &stu[0];	The head pointer points to the 1th 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 whose next member does not hold the address of any node and is set to NULL//traverse the static chain table P = head;		Causes the P pointer to also point to the 1th node do{printf ("%d\n", p->num);//output P points to the node's data P = p->next;	Then let P point to the next node} while (P!= NULL);
Until the next member of P is null, the traversal return 0 is completed; }

The output results are:

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

second, dynamic linked list

So far, when it comes to dealing with "batch" data, we use arrays to store it. Defining an array must (explicitly or implicitly) indicate the number of elements, and thus limit the amount of data stored in an array. In practice, the number of data to be processed by a program at each run is usually uncertain. If the array definition is small, there is not enough space to hold the data, the definition of large and waste of storage space.
For this situation, if you can in the process of execution, the need to open the storage space at any time, do not need to release at any time, you can more reasonable use of storage space. The dynamic storage allocation of the C language provides this possibility. Each dynamically allocated storage unit, its address is not necessarily continuous, and the required processing of the batch data is often a whole, there is a connection between the data. In each node of a list, in addition to having the data outside the data itself, at least one pointer field is needed to hold the address of the next node element to connect the nodes through these pointers. Because each storage unit of the linked list is obtained by dynamic storage allocation, the linked list is called "Dynamic List".

The reference program looks like this:

/***************************************************** Copyright (C) 2017-2018 All rights reserved. File name:dynamic_link.c version:v1.0 Author:zhengqijun date:2017 year October 10 Tuesday 15:31 59 SEC description:funcion List: *****************************************************/#include <stdio.h> #includ E <stdlib.h>/* The so-called dynamic linked list, refers to the implementation of the process from scratch to establish a linked list, that is, one by one to open the node and input the data of each node, and establish the relationship between the front and back phase chain.		* * struct Student {int No;
School number struct Student *next;

};
	int main () {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 a 1th school number \ n");

    scanf ("%d", &p1->no); P2 = p1;
        At the beginning, both P1 and P2 point to the 1th 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 the school number, enter 0 terminate: \ n");P1 = (struct Student *) malloc (sizeof (struct Student));
    scanf ("%d", &p1->no);

    };
    P2->next = null;//input complete, p2->next is NULL//traversal dynamic chain table P = head;

    printf ("\ n: \ n");
        while (P!= NULL) {printf ("%d\n", p->no);
    p = p->next;
return 0; }

The output results are:

Please input the 1th School Number 1 Please enter the school number, enter 0 to terminate: 2 Please enter the study number, enter 0 to terminate: 3 Please enter the study number, enter 0 to terminate: 4 Please enter the school number, enter 0 to terminate: 0 The school number is: 1234

Note: In a dynamic list, each node does not have its own name and can only rely on the pointer to maintain the relationship between nodes. Once a node's pointer is "disconnected", subsequent nodes are no longer available for searching.


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.