Wu Yuxiong - born natural data structures: static list and create

Static list, but also a linear memory structure, which take into account the order form and advantages in a list, the order form and can be seen as an upgraded version of the list.
Storing data using static lists, all the data stored in the array (and the same sequence table), but the storage location is random, the data between " one " logical relationship by an integer variable (referred to as a " cursor " , and the pointer function similar) maintain (and similar lists).
Create an array large enough, let's say about 6

Next direct successor array element at which, when the store data into an array, to each data element of plastic with a variable, this variable indicates the position of each element in the index

By " array cursor + " of stored data having a linear relationship between the storage structure is a static list.
Static data elements stored in the list need to custom data type, must contain at least the following two pieces of information:
Data field: a value for storing data elements;
Cursor: in fact, an array subscript, represents the position of the array elements where the direct successor;
typedef struct {
     int Data; // data field 
    int CUR; // cursor 
} component;
Static complete enough list, static list in addition to the data itself is composed of a cursor through the list, but also needs to have a chain connecting the respective idle position, referred to as a standby list.
Alternate action list is to use a previously unused or recycled through the array (currently unused) storage space, left later use. That is, the physical space using a static list array application, there two lists, a data connection, another connection array space unused.
Typically, the list header located at the standby array subscript 0 (A [ 0 ]) of the position, the data of the list header is located in array subscripting . 1 (A [ . 1 ]) position.
Set up an alternate static list of the list benefit is that you can clearly know whether there is an idle position in the array to use when adding a new data list. For example, if an array index for the static list 0 contains data on the position, the array is full proof.

Before uninitialized data list, all locations in the array in an idle state, and therefore should be a link on the standby list

The standby node list removal easiest way is removed A [ 0 ] is a direct successor nodes; Similarly, adding to the free nodes link list is also added as a spare A [ 0 ] New immediate successor node. Because the A [ 0 ] is the first node in the alternate list, we know its location, its immediate subsequent node operation is relatively easy, without traversing the spare list, it takes time complexity is O ( . 1 ).

 

 

#include <stdio.h>
#define maxSize 6
typedef struct {
    int data;
    int cur;
}component;
//将结构体数组中所有分量链接到备用链表中
void reserveArr(component *array);
//初始化静态链表
int initArr(component *array);
//输出函数
void displayArr(component * array,int body);
//从备用链表上摘下空闲节点的函数
int mallocArr(component * array);
int main() {
    component array[maxSize];
    int body=initArr(array);
    printf("静态链表为:\n");
    displayArr(array, body);
    return 0;
}
//创建备用链表
void reserveArr(component *array){
    for (int i=0; i<maxSize; i++) {
        array[i].cur=i+1;//将每个数组分量链接到一起
        array[i].data=-1;
    }
    array[maxSize-1].cur=0;//链表最后一个结点的游标值为0
}
//提取分配空间
int mallocArr(component * array){
    //若备用链表非空,则返回分配的结点下标,否则返回 0(当分配最后一个结点时,该结点的游标值为 0)
    int i=array[0].cur;
    if (array[0].cur) {
        array[0].cur=array[i].cur;
    }
    return i;
}
//初始化静态链表
int initArr(component *array){
    reserveArr(array);
    int body=mallocArr(array);
    //声明一个变量,把它当指针使,指向链表的最后的一个结点,因为链表为空,所以和头结点重合
    int tempBody=body;
    for (int i=1; i<4; i++) {
        int j=mallocArr(array);//从备用链表中拿出空闲的分量
        array[tempBody].cur=j;//将申请的空闲分量链接在链表的最后一个结点后面
        array[j].data=i;//给新申请的分量的数据域初始化
        tempBody=j;//将指向链表最后一个结点的指针后移
    }
    array[tempBody].cur=0;//新的链表最后一个结点的指针设置为0
    return body;
}
void displayArr(component * array,int body){
    int tempBody=body;//tempBody准备做遍历使用
    while (array[tempBody].cur) {
        printf("%d,%d ",array[tempBody].data,array[tempBody].cur);
        tempBody=array[tempBody].cur;
    }
    printf("%d,%d\n",array[tempBody].data,array[tempBody].cur);
}

 

Guess you like

Origin www.cnblogs.com/tszr/p/12232209.html