数据结构教程 | 静态链表及代码实现

Code Implementation of Static link list


1 Construction of the static link list

1.1 Construct a static link list

Static link list is different from the link list discussed in the last passage(We call that a dynamic link list). The elements in the static link list stored as an array (like a sequential table), but their positions are random, the data connect with a cursor (like a link list).

Here we first construct the node in the static link list:

typedef struct {
    int data;		// Data field
    int cur;		// Cursor
}component;

1.2 Initialization

Standby list, the most unique component of the static link list, should be considered first. We can construct the list with the following code:

// Create the standby list
#define maxSize 6
void reserveArr(component *array){
    for (int i=0; i<maxSize; i++) {
        array[i].cur=i+1;			// Link each array component together
    }
    array[maxSize-1].cur=0;			// The cursor of the last mode equals 0
}
// Extract allocation space
int mallocArr(component * array){
    int i=array[0].cur;
    if (array[0].cur) {
        array[0].cur=array[i].cur;	// If the standby list not null, return the subscript
    }
    return i;
}
// Initialize the static link list
int initArr(component *array){
    reserveArr(array);
    int body=mallocArr(array);
    int tempBody=body;				// A pointer points to the last node(Because the link list is null, it is equal to the head node)
    for (int i=1; i<4; i++) {
        int j=mallocArr(array);		// Take the free component from the spare list
        array[tempBody].cur=j;		// Link the free component after the last node of the link list
        array[j].data=i;			// Initialize the newly applied component's data field
        tempBody=j;					// Move backwards (The pointer points to the last node of link list)
    }
    array[tempBody].cur=0;			// The pointer of the last node of the new link list equals 0
    return body;
}

1.3 Check and Display the code

void displayArr(component * array,int body){
    int tempBody=body;				// tempBody: set for traversal
    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);
}
int main() {
    component array[maxSize];
    int body=initArr(array);
    printf("The static link list is:\n");
    displayArr(array, body);
    return 0;
}

Output:
在这里插入图片描述

2 Basic operations of static link list

Notice: The standby list also need space, so we might define a function that will help us achieve it

// Function of standby list reclaim space. k: index of the array in which the node is not used
void freeArr(component * array,int k){
    array[k].cur=array[0].cur;
    array[0].cur=k;
}

We want to use more symbols to check the code, like type ‘char’. So we sightly change the display function above:

void displayArr(component * array,int body){
    int tempBody=body;
    while (array[tempBody].cur) {
        printf("%c,%d ",array[tempBody].data,array[tempBody].cur);
        tempBody=array[tempBody].cur;
    }
    printf("%c,%d\n",array[tempBody].data,array[tempBody].cur);
}

2.1 Insert elements

// body: the position of the link list's head node in the array, add: the position of the inserted element, a: inserted value
void insertArr(component * array,int body,int add,char a){
    int tempBody=body;						// tempBody: traversal of the structure array
    for (int i=1; i<add; i++) {				// Find the position of the last node of the inserted place
        tempBody=array[tempBody].cur;
    }
    int insert=mallocArr(array);			// Apply for space
    array[insert].data=a;
    array[insert].cur=array[tempBody].cur;	// The new node's cursor equals the cursor of it's direct precursor
    array[tempBody].cur=insert;				// The cursor of it's direct precursor equals the subscript in the array
}

2.2 Delete elements

void deletArr(component * array,int body,char a){
    int tempBody=body;
    while (array[tempBody].data!=a) {	// Find the position
        tempBody=array[tempBody].cur;
        if (tempBody==0) {			
            printf("The data is not exist in the link list");
            return;
        }
    }
    int del=tempBody;
    tempBody=body;
    while (array[tempBody].cur!=del) {	// Find the last node of the deleted node
        tempBody=array[tempBody].cur;
    }
    array[tempBody].cur=array[del].cur;	// Take the cursor from the deleted node to its last node
    freeArr(array, del);				// Reclaim the space
}

2.3 Find the element

int selectElem(component * array,int body,char elem){
    int tempBody=body;
    while (array[tempBody].cur!=0) {		// If the cursor equals 0,the link list is ended
        if (array[tempBody].data==elem) {
            return tempBody;
        }
        tempBody=array[tempBody].cur;
    }
    return -1;								// Failed to find the target
}

2.4 Alter the element

void amendElem(component * array,int body,char oldElem,char newElem){
    int add=selectElem(array, body, oldElem);
    if (add==-1) {
        printf("No such element");
        return;
    }
    array[add].data=newElem;
}

2.5 Check the code

int main() {
    component array[maxSize];
    int body=initArr(array);
    printf("The static link list:\n");
    displayArr(array, body);
  
    printf("Insert ‘e’on position 3:\n");
    insertArr(array, body, 3,'e');
    displayArr(array, body);
  
    printf("Delete the node with value ‘a:\n");
    deletArr(array, body, 'a');
    displayArr(array, body);
  
    printf("Find the node position with value ‘e’:\n");
    int selectAdd=selectElem(array,body ,'e');
    printf("%d\n",selectAdd);
    printf("Change the value ‘e’to‘h’:\n");
    amendElem(array, body, 'e', 'h');
    displayArr(array, body);
    return 0;
}

Output:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值