C-language static linked list and dynamic linked list _c language

Source: Internet
Author: User
Tags array definition

1. 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 link
{
char ch;
struct link *p;
A

P is a pointer member that can point to a struct link type variable. Therefore, A.P = &a is a valid expression, and the resulting storage structure is shown in Figure 1.

Figure 1 referencing the structure of the body

Example of 11 simple linked lists

#include <stdio.h>

struct node
{
  int data;
  struct node *next;
typedef struct node NODETYPE;

int main ()
{
  //a is a header node, B is an intermediary, C is a tail node
  //h is a pointer to a base type of NodeType, a
  pointer to a header node//p is a cursor with a base type of NodeType, used to traverse
  a list NODETYPE A, B, C, *h, *p;
  
  The data assignment to the variable
  a.data = ten;
  B.data =;
  C.data =;
  
  Connect the nodes
  h = &a;
  A.next = &b;
  B.next = &c;
  C.next = ' n ';
  
  Move p so that it points to a, B, and C, outputting the value
  p = h in their data;
  while (p)
  {
    printf ("%d\t", p->data);
    p = p->next;  P-Order after
  }
  printf ("\ n");
  return 0;
}

Struct_list

Struct_list

The structure body type defined in the above program has a total of two members: member data is an integer, member next is a pointer type, and its base type is NODETYPE type NODETYPE.

A, B, and C are NODETYPE struct type variables, and h and p are pointer variables that point to the type of NODETYPE struct body. After executing the program, form the storage structure as shown in Figure 2: The address of the variable A in pointer h, the address of variable A in the member A.next of variable a ..., the member of the last variable C c.next is set to ' Yes ' (NULL). This will be the same type of structure variables a, B, C "link" together to form the so-called "linked list", variable A, B, C called linked list node.

In this example, each node that is linked together (struct variables A, b, c) is defined by a system that creates a fixed, not necessarily contiguous, storage unit in memory. In the process of program execution, it is impossible to generate new storage units artificially, nor can it be considered that the storage units that have been opened have disappeared. This list becomes a "static list".

Fig. 2 schematic diagram of the chain table storage structure

2. The concept of 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 the 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 so that the nodes can be connected by these pointers (Figure 3). Because each storage unit of the linked list is obtained by dynamic storage allocation, the linked list is called "Dynamic List".

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

Fig. 3 One-way linked list with head nodes

Each list uses a "head pointer" variable to point to the beginning of the list, as in Figure 3. In other words, the address of the first node in the list is stored in the head. In this list, we set up a "header node", which does not hold data in the data field (as needed or without a header node). The pointer field of the last node in the list does not hold the address, and is set to ' value ' (NULL) to mark the end of the list. Each node in the list above has only one pointer field, and each pointer field holds the address of the next node. Therefore, the linked list can only find the successor node from the current node, so it is called "one-way linked list".

Related Article

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.