一、静态链表的意义
静态链表,就是用数组描述的链表。静态链表在初始化时要确定数组的长度,所以需要预先分配空间,其空间大小一般是静态的,故名静态链表
二、创建静态链表
代码演示:
typedef struct StaticLinkedNode{
char data;
int next; //游标
}*NodePtr;
typedef struct StaticLinkedList{
NodePtr nodes;
int *used;
}*ListPtr;
*1、next代表游标
2、用used指针判断空间是否已经分配 *
三、初始化静态链表

注意:
初始化要让头结点数据为空,且使其游标为-1,当然也要标注该空间已经分分配,以及标注后面的空间未分配
代码演示
ListPtr initLinkedList(){
ListPtr tempPtr = (ListPtr)malloc(sizeof (struct StaticLinkedList));
tempPtr->nodes=(NodePtr)malloc(sizeof(struct StaticLinkedNode)*DEFAULT_SIZE);
tempPtr->used=(int*)malloc(sizeof(int)*DEFAULT_SIZE);
tempPtr->nodes[0].data='\0' ;
tempPtr->nodes[0].next=-1;
tempPtr->used[0]=1;
for(int i=0;i<DEFAULT_SIZE;i++){
tempPtr->used[i]=0;
}
return tempPtr;
}
注:这里要注意分配的空间需要乘最大结点数
四、打印
代码演示:
void printList(ListPtr paraListPtr){
int p=0;
while(p!=-1){
printf("%c",paraListPtr->nodes[p].data);
p=paraListPtr->nodes[p].next;
}
printf("\r\n");
}
注:需要理解 p=paraListPtr->nodes[p].next ,非常重要!!!
五、插入
void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition){
int p,q,i;
p=0;
for(i=0 ;i<paraPosition;i++){
p=paraListPtr->nodes[p].next;
if(p==-1){
printf("The position %d is beyond the scope of the list.\r\n",paraPosition);
return;
}
}
for(i=1;i<DEFAULT_SIZE;i++){
if(paraListPtr->used[i]==0){
printf("Space at %d allocated.\r\n", i);
paraListPtr->used[i]=1;
q=i;
break;
}
}
if(i==DEFAULT_SIZE){
printf("No space\r\n");
return;
}
paraListPtr->nodes[q].data=paraChar;
paraListPtr->nodes[q].next=paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next=q;
}
注:
1、首先需要找到插入的位置,这点和之前一样
2、然后需要找到未分配的空间,从之前的used[0]开始,used[0]已经分配,之后每分配一个空间都需标注为已经分配
3、注意游标的变化!(如下图,看两次图的变化)

六、删除
代码演示:
void deleteElement(ListPtr paraListPtr,char paraChar){
int p , q;
p=0;
while((paraListPtr->nodes[p].next!=-1) && (paraListPtr->nodes[paraListPtr->nodes[p].next].data!=paraChar)){
p=paraListPtr->nodes[p].next;
}
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;
paraListPtr->used[q]=0;
}
注:删除之后各个元素下标不会改变
七、查找第x位元素
代码演示:
void getElement(ListPtr paraListPtr,int paraPosition){
int p=0;
char q;
for(int i=0;i<paraPosition;i++){
p=paraListPtr->nodes[p].next;
}
q=paraListPtr->nodes[p].data;
printf("第%d位是%c",paraPosition,q);
}
八、查找元素x在第几位
代码演示:
void locateElement(ListPtr paraListPtr,char paraChar){
int p=0,num = 0;
while(paraListPtr->nodes[p].data!=paraChar){
num++;
p=paraListPtr->nodes[p].next;
}
printf("%c在第%d位",paraChar,num);
}
注:这里不能直接用下标做位数,原因看删除操作注释
九、测试
void Test(){
ListPtr List=initLinkedList();
insertElement(List,'H',0);
insertElement(List,'E',1);
insertElement(List,'L',2);
insertElement(List,'a',3);
insertElement(List,'O',4);
printList(List);
deleteElement(List,'E');
printList(List);
getElement(List,2);
printf("\r\n");
locateElement(List,'L');
}
十、此时链表

十一、总代码
#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 = (ListPtr)malloc(sizeof (struct StaticLinkedList));
tempPtr->nodes=(NodePtr)malloc(sizeof(struct StaticLinkedNode)*DEFAULT_SIZE);
tempPtr->used=(int*)malloc(sizeof(int)*DEFAULT_SIZE);
tempPtr->nodes[0].data='\0' ;
tempPtr->nodes[0].next=-1;
tempPtr->used[0]=1;
for(int i=0;i<DEFAULT_SIZE;i++){
tempPtr->used[i]=0;
}
return tempPtr;
}
void printList(ListPtr paraListPtr){
int p=0;
while(p!=-1){
printf("%c",paraListPtr->nodes[p].data);
p=paraListPtr->nodes[p].next;
}
printf("\r\n");
}
void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition){
int p,q,i;
p=0;
for(i=0 ;i<paraPosition;i++){
p=paraListPtr->nodes[p].next;
if(p==-1){
printf("The position %d is beyond the scope of the list.\r\n",paraPosition);
return;
}
}
for(i=1;i<DEFAULT_SIZE;i++){
if(paraListPtr->used[i]==0){
printf("Space at %d allocated.\r\n", i);
paraListPtr->used[i]=1;
q=i;
break;
}
}
if(i==DEFAULT_SIZE){
printf("No space\r\n");
return;
}
paraListPtr->nodes[q].data=paraChar;
paraListPtr->nodes[q].next=paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next=q;
}
void deleteElement(ListPtr paraListPtr,char paraChar){
int p , q;
p=0;
while((paraListPtr->nodes[p].next!=-1) && (paraListPtr->nodes[paraListPtr->nodes[p].next].data!=paraChar)){
p=paraListPtr->nodes[p].next;
}
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;
paraListPtr->used[q]=0;
}
void getElement(ListPtr paraListPtr,int paraPosition){
int p=0;
char q;
for(int i=0;i<paraPosition;i++){
p=paraListPtr->nodes[p].next;
}
q=paraListPtr->nodes[p].data;
printf("第%d位是%c",paraPosition,q);
}
void locateElement(ListPtr paraListPtr,char paraChar){
int p=0,num = 0;
while(paraListPtr->nodes[p].data!=paraChar){
num++;
p=paraListPtr->nodes[p].next;
}
printf("%c在第%d位",paraChar,num);
}
void Test(){
ListPtr List=initLinkedList();
insertElement(List,'H',0);
insertElement(List,'E',1);
insertElement(List,'L',2);
insertElement(List,'a',3);
insertElement(List,'O',4);
printList(List);
deleteElement(List,'E');
printList(List);
getElement(List,2);
printf("\r\n");
locateElement(List,'L');
}
int main(){
Test();
}
十二、测试结果
Space at 1 allocated.
Space at 2 allocated.
Space at 3 allocated.
Space at 4 allocated.
No space
HELa
HLa
第2位是L
L在第2位
游标下标的关系需要非常注意,随时注意空间的分配情况
本文详细介绍了静态链表的概念、创建、初始化、打印、插入、删除、查找元素等基本操作。通过示例代码展示了如何在C语言中实现静态链表,并提供了测试用例及其结果,帮助读者理解静态链表的工作原理。
1494

被折叠的 条评论
为什么被折叠?



