一、静态链表的意义
静态链表,就是用数组描述的链表。静态链表在初始化时要确定数组的长度,所以需要预先分配空间,其空间大小一般是静态的,故名静态链表
二、创建静态链表
代码演示:
typedef struct StaticLinkedNode{
char data;
int next; //游标
}*NodePtr;
typedef struct StaticLinkedList{
NodePtr nodes;!(https://img-blog.csdnimg.cn/8fc4adde65c245c28eda9bbd8f1dbfd7.png)
int *used;
}*ListPtr;
1
2
3
4
5
6
7
8
9
10
*1、next代表游标
2、用used指针判断空间是否已经分配 *
三、初始化静态链表
代码演示
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;
}
六、删除
代码演示:
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位