3. 线性表 - 静态链表
3.1 静态链表概念
静态链表 即定义一个结构体数组,结构体包含以下 2 部分信息:
数据域 :用于存储数据元素的值;游标 :储存直接后继元素所在数组中的位置,其实就是数组下标 - 静态链表实际上包含2条链表
备用链表 :还未使用的链表数据链表 :已经存储数据的链表
3.2 静态链表的基本操作方法
#include<stdio.h>
#include<stdint.h>
#include <stdlib.h>
#define maxSize 6
typedef struct {
uint8_t data;
uint8_t position;
}component;
void printArr(component* array) {
printf("*******************\n");
for (uint8_t i = 0; i < maxSize; i++) {
printf("%x --> [ %x, %d ]\n",i, array[i].data, array[i].position);
}
printf("*******************\n");
}
void reserveArr(component* array) {
for (uint8_t i = 0; i < maxSize; i++) {
array[i].position = i + 1;
array[i].data = 0xff;
}
array[maxSize - 1].position = 0;
}
uint8_t mallocArr(component* array) {
uint8_t i = array[0].position;
if (array[0].position) {
array[0].position = array[i].position;
}
return i;
}
uint8_t initArr(component* array) {
reserveArr(array);
uint8_t body = mallocArr(array);
array[body].data = 1;
array[body].position = 0;
uint8_t tempBody = body;
for (uint8_t i = 2; i < 4; i++) {
uint8_t j = mallocArr(array);
array[tempBody].position = j;
array[j].data = i;
tempBody = j;
}
array[tempBody].position = 0;
return body;
}
void displayArr(component* array, uint8_t body) {
uint8_t tempBody = body;
while (array[tempBody].position) {
printf("%x ", array[tempBody].data);
tempBody = array[tempBody].position;
}
printf("%x\n", array[tempBody].data);
}
void insertArr(component* array, uint8_t body, uint8_t add, uint8_t num) {
uint8_t tempBody = body;
for (uint8_t i = 1; i < add - 1; i++) {
tempBody = array[tempBody].position;
}
int insert = mallocArr(array);
if (insert == 0) {
printf("链表存储空间已满,操作失败!!!\n");
return;
}
array[insert].data = num;
array[insert].position = array[tempBody].position;
array[tempBody].position = insert;
}
void freeArr(component* array, uint8_t k) {
array[k].position = array[0].position;
array[k].data = 0xff;
array[0].position = k;
}
void deletArr(component* array, uint8_t body, uint8_t num) {
uint8_t tempBody = body;
while (array[tempBody].data != num) {
tempBody = array[tempBody].position;
if (tempBody == 0) {
printf("链表中没有此数据");
return;
}
}
uint8_t del = tempBody;
tempBody = body;
while (array[tempBody].position != del) {
tempBody = array[tempBody].position;
}
array[tempBody].position = array[del].position;
freeArr(array, del);
}
uint8_t selectElem(component* array, int body, char elem) {
uint8_t tempBody = body;
while (array[tempBody].position != 0) {
if (array[tempBody].data == elem) {
return tempBody;
}
tempBody = array[tempBody].position;
}
if (array[tempBody].data == elem) {
return tempBody;
}
return 0;
}
void amendElem(component* array, uint8_t body, uint8_t oldElem, uint8_t newElem) {
int add = selectElem(array, body, oldElem);
if (add == 0) {
printf("无更改元素!!!");
return;
}
array[add].data = newElem;
}
int main() {
component array[maxSize];
uint8_t body = initArr(array);
printArr(array);
printf("静态链表为 :");
displayArr(array, body);
insertArr(array, body, 2, 4);
printArr(array);
printf("在静态链表的第2位置上插入数据4 :");
displayArr(array, body);
deletArr(array, body, 2);
printArr(array);
printf("删除静态链表数据2 :");
displayArr(array, body);
uint8_t position = selectElem(array, body, 3);
printf("静态链表中数据 3 所在位置为 :%d", position);
amendElem(array, body, 4, 2);
printArr(array);
printf("将静态链表数据 4 替换成 2 :");
displayArr(array, body);
return 0;
}
感谢阅读 若有错误 敬请见谅!!!
|