前言
本节是对静态链表的实现,还是有一些点需要注意,跟动态链表还是有些区别。定义的next和used都是int型,但是开辟空间还是要用malloc库函数。
1.代码实现
#include <stdio.h>
#include <malloc.h>
#define DEFAULT_SIZE 5
//定义结点
typedef struct StaticLinkedNode {
char data;
int next;
}*NodePtr;
//定义链表结点
typedef struct StaticLinkedList {
NodePtr nodes;
int* used;
}*ListPtr;
ListPtr initLinkedList()
{
ListPtr tempPtr;//定义头指针
tempPtr = (ListPtr)malloc(sizeof(struct StaticLinkedList));
tempPtr->nodes = (NodePtr)malloc(sizeof(struct StaticLinkedNode) * DEFAULT_SIZE);//给头指针的nodes域开辟空间
tempPtr->used = (int*)malloc(sizeof(int) * DEFAULT_SIZE);//给used域开辟空间
tempPtr->nodes[0].data = '\0';//头指针的data和next域都置空
tempPtr->nodes[0].next = -1;
tempPtr->used[0] = 1;//第一个结点标记为1
for (int i = 1; i < DEFAULT_SIZE; i++) {
tempPtr->used[i] = 0;//遍历定义的最大空间,给每个used都设置为0
}
return tempPtr;//返回头指针的地址
}
void printList(ListPtr paraListPtr)
{
int p = 0;
while (p != -1) {
printf("%c", paraListPtr->nodes[p].data);
p = paraListPtr->nodes[p].next;//遍历输出所有的data,如果used值为-1,说明遍历到表尾了,退出循环.
}
printf("\r\n");
}
void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition)
{
int p, q, i;//p为表中元素的相对位置,q用来赋值i,i就是局部变量
p = 0;
for (i = 0; i < paraPosition; i++) {
p = paraListPtr->nodes[p].next;
if (p == -1) {//先遍历到paraPosition所指的位置,但是还是要排除表被遍历结束还找不到paraPosition.
printf("The position %d is beyong the scope of the list.\r\n", paraPosition);
return;
}
}
for (i = 1; i < DEFAULT_SIZE; i++) {//进行到这,就开始给新插入的元素找表中空的位置./遍历寻找used为0的位置
if (paraListPtr->used[i] == 0) {
printf("Space at %d allocated.\r\n", i);
paraListPtr->used[i] = 1;
q = i;//q的作用:i赋值给q.
break;
}
}
if (i == DEFAULT_SIZE) {
printf("No space.\r\n");
return;//空间达到最大,不能进行后续元素插入了.
}
//这几步就是把原本空间的数据改成传入的数据,然后再把表前后连接.
paraListPtr->nodes[q].data = paraChar;
printf("linking\r\n");
paraListPtr->nodes[q].next = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = q;
}
void deleteElement(ListPtr paraListPtr, char paraChar)
{
int p, q;//q来表示元素位置,q来占位删除元素的后一个元素.
p = 0;
//遍历:两个条件,p->next不能为空,p->data不能为传入的元素./因为我们要让p指向删除的元素的前一个位置.
while ((paraListPtr->nodes[p].next != -1) && (paraListPtr->nodes[paraListPtr->nodes[p].next].data != paraChar)) {
p = paraListPtr->nodes[p].next;
}
//排除p->next==NULL的情况.
if (paraListPtr->nodes[p].next == -1) {
printf("can not delete %c\r\n", paraChar);
return;
}
//接下来几步就是把删除元素的前后元素相对位置连接起来.
q = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = paraListPtr->nodes[paraListPtr->nodes[p].next].next;
//别看这个长,其实[]里面就是p->next,然后外围又是一个next,所以就是p->next->next.
paraListPtr->used[q] = 0;
}
void appendinsertdeleteTest()
{
ListPtr tempList = initLinkedList();
printList(tempList);
insertElement(tempList, 'H', 0);
insertElement(tempList, 'e', 1);
insertElement(tempList, 'l', 2);
insertElement(tempList, 'l', 3);
insertElement(tempList, 'o', 4);
printList(tempList);
printf("Deleting 'e'.\r\n");
deleteElement(tempList, 'e');
printf("Deleting 'a'.\r\n");
deleteElement(tempList, 'a');
printf("Deleting 'o'.\r\n");
deleteElement(tempList, 'o');
printList(tempList);
insertElement(tempList, 'x', 1);
printList(tempList);
}
void main()
{
//invoking function.
appendinsertdeleteTest();
}
2.输出结果
Space at 1 allocated.
linking
Space at 2 allocated.
linking
Space at 3 allocated.
linking
Space at 4 allocated.
linking
No space.
Hell
Deleting 'e'.
Deleting 'a'.
can not delete a
Deleting 'o'.
can not delete o
Hll
Space at 2 allocated.
linking
Hxll