List | static list - cursor achieve

table of Contents

I. Overview

1, the dynamic list

2, static list

Second, the specific implementation 

1, there must be a global array of structures

2, so that functional units instead of malloc and free CursorSpace array

 

Ⅰ.malloc mock implementation

Ⅱ.free mock implementation

Third, other operations

 


I. Overview

1, the dynamic list

Various list are previously learned by the implementation pointer , the allocation and deallocation of nodes in the list (i.e., release) are standard functions provided by the system malloc and free dynamic implementation of, so called dynamic list .

2, static list

However, some high-level languages, such as BASIC, FORTRAN, etc., does not provide a "pointer" This type of data , this time if you want to make use of the list storage structure can be achieved only through the array, and must use the "cursor" to the analog pointer , the program write "distribution nodes" and "recovery node" process members themselves.

This array is called a static list described in a linked list, this method is called the cursor implemented method is described.


note

There are two important features in a linked list of pointers to achieve:

1, the data stored in a set structure. Each structure contains a pointer and points to the next data structure.
2, a new structure can be obtained by calling malloc global memory from the system, and may be released by calling free.

Second, the specific implementation 

Vernier method must be able to imitate achieve these two characteristics :

1, there must be a global array of structures

For the time being named CursorSpace array:

Array as sequential storage elements, each element of a structure, there are two variables: data for data storage, next to achieve point to the next node position in the array, a linked list so as to realize a similar function.

  • The array is similar to the system memory, the application node, nodes are released in this array.
  • And the size of the array determines the maximum number of nodes linked list.
  • Any cell in the array, which array can be used to represent an address mark.
struct Node {
    int data;  //数据域
    int next; //模拟指针,即下一个位置的数组下标
} CursorSpace[SpaceSize];

 

2, let the unit instead of CursorSpace array of functions malloc and free

Through a simulated freelist table memory , we are also known as standby list . freelist table header is set to CursorSpace [0], then the header of the attached nodes are in the freelist table, i.e. still in memory to be used by the application node . Once this malloc a node from the "Memory", then the node is not in the freelist up.

  • The first array element CursorSpace [0] as a standby list head node, which cursor indicates the value 0 is next to null.
  • Initially initialized state: an array of all the nodes are in the freelist.

The following is a function initializes the freelist (spare list) of.

void initialize(int SpaceSize) {
    for(int i = 0;; i++) {
        CursorSpace[i]->data = 0;  //数据域置零
        //到了最后一个空间
        if(i + 1 == SpaceSize)  {
            CursorSpace[i].next = 0;  //相当于指向NULL
            break;
        }
        CursorSpace[i].next = i + 1;
    }
}

The initial situation of the standby list freelist as shown, slot its subscript, element data field (all initialized to zero), next to the index of the next node:

Familiar with the basic structure of the freelist then for simulation malloc and free to achieve it is easy to understand.

 

Ⅰ.malloc mock implementation

Note that we simulated environment: freelist (standby list) for our analog memory.

Operation is then malloc application and removed from the freelist a node , return its subscript, we default the first node after a request header node , code is implemented as follows:

int CursorAlloc() {
    int p;
    p = CursorSpace[0].next;  //获取头节点后第一个节点
    CursorSpace[0].next = CursorSpace[p].next;  //从freelist中删除标为p的节点
    return p;  //返回申请成功的节点下标
}

If you can not understand, you can realize several CursorAlloc operating manual. 

Ⅱ.free mock implementation

Note that we simulated environment: freelist (standby list) for our analog memory.

Operation is then malloc, free to be passed node index, to be recovered into the free node (re-insertion) in the freelist , we default after insertion head node , code is implemented as follows:

void CursorFree(int p) {
    CursorSpace[p].next = CursorSpace[0].next;
    CursorSpace[0].next = p;
}

If you can not understand, you can realize several CursorFree operating manual. 

 

Third, other operations

Cursor on the list of insert, delete, search and other operations

To be continued ...

 



end

No personal welcome attention to the public "programming chicken wings", here is a serious and well-behaved farmers a code designed to carefully write each article, usually aggregated into notes will push updates -

 

Published 11 original articles · won praise 5 · Views 200

Guess you like

Origin blog.csdn.net/weixin_43787043/article/details/103936825