main.cpp
#include "StaticList.h"
int main() {
StaticList SL;
InitSList(SL);
for (int i = 0; i < 5; ++i) {
Insert(SL,'A'+i);
}
ShowSList(SL);
DeleteSList(SL);
ShowSList(SL);
return 0;
}
StaticList.h(头文件)
#ifndef STATICLIST_STATICLIST_H
#define STATICLIST_STATICLIST_H
#include <stdio.h>
#define MAX_SIZE 20
#define ElemType char
typedef struct ListNode {
ElemType data;
int cur;
} ListNode;
typedef ListNode StaticList[MAX_SIZE];
int Malloc_SL(StaticList &space);
void Free_SL(StaticList &space, int k);
void InitSList(StaticList &space);
void Insert(StaticList &space, ElemType x);
void DeleteSList(StaticList &space);
void ShowSList(StaticList &space);
#endif //STATICLIST_STATICLIST_H
StaticList.cpp
#include "StaticList.h"
void InitSList(StaticList &space) {
for (int i = 1; i < MAX_SIZE - 1; ++i) {
space[i].cur = i + 1;
}
space[MAX_SIZE - 1].cur = 0;
space[0].cur = -1;
}
void Free_SL(StaticList &space, int k) {
space[k].cur = space[1].cur;
space[1].cur = k;
}
int Malloc_SL(StaticList &space) {
int i = space[1].cur;
if (space[1].cur != 0)
space[1].cur = space[i].cur;
return i;
}
void Insert(StaticList &space, ElemType x) {
int i = Malloc_SL(space);
if (i == 0) {
printf("申请节点空间失败\n");
return;
}
space[i].data = x;
if (space[0].cur == -1) {
space[i].cur = -1;
} else {
space[i].cur = space[0].cur;
}
space[0].cur = i;
}
void DeleteSList(StaticList &space) {
int i = space[0].cur;
space[0].cur = space[i].cur;
Free_SL(space, i);
// space[i].cur = space[1].cur;
// space[1].cur = i;
}
void ShowSList(StaticList &space) {
int i = space[0].cur;
while (i != -1) {
printf("%c-->", space[i].data);
i = space[i].cur;
}
printf("Null");
putchar(10);
}
标签:线性表,space,int,void,StaticList,链表,SL,cur
From: https://www.cnblogs.com/hekang520/p/17301323.html