目录
介绍
静态链表是借助数组来描述线性表的链式存储结构,此时的数组的分量就是我们自己定义的结构体,数组中的一个分量表示一个结点,同时用int类型的元素代替指针描述结点在数组中的相对位置。此外,数组的第0个分量可以看成链表中的头结点,其游标即链表中的指针域则就指向链表的第一个结点。这种存储结构是需要预先分配一个较大的空间,但是在线性表的插入和删除时是不需要移动元素的,仅需要修改游标,因此是具有链式存储结构的主要优点的。
结构
typedef struct staticLinkedNode{
char data;
int next;
}*NodePtr;
typedef struct staticLinkedList{
NodePtr nodes;
int* used;
}*ListPtr;
初始化
ListPtr iniLinkedList(){
ListPtr tempPtr = (ListPtr)malloc(sizeof(struct staticLinkedList));
tempPtr->nodes = (NodePtr)malloc(sizeof(struct staticLinkedNode)*SIZE);
tempPtr->used = (int*)malloc(sizeof(int)*SIZE);
tempPtr->nodes[0].data = 0;
tempPtr->nodes[0].next = -1;
tempPtr->used[0] = 1;
for(int i = 1; i < SIZE;i++){
tempPtr->used[i] = 0;
}
return tempPtr;
}
打印
void printList(ListPtr pList){
int p = 0;
while(p != -1){
printf("%c",pList->nodes[p].data);
p = pList->nodes[p].next;
}
printf("\n");
}
插入元素
void insertElement(ListPtr pList,char pchar,int pos){
int p,q,i;
p = 0;
for(int i = 0; i < pos;i++){
p = pList->nodes[p].next;
if(p == -1){
printf("The position %d is beyond the scope of the list.\n",pos);
return;
}
}
for(i = 0;i < SIZE;i++){
if(pList->used[i]==0){
printf("Space at %d allocated.\n",i);
pList->used[i] = 1;
q = i;
break;
}
}
if(i == SIZE){
printf("No space.\n");
return;
}
pList->nodes[q].data = pchar;
printf("Linking\n");
pList->nodes[q].next = pList->nodes[p].next;
pList->nodes[p].next = q;
}
删除元素
void deleteElement(ListPtr pList,char pchar){
int p,q;
p = 0;
while((pList->nodes[p].next != -1) &&(pList->nodes[pList->nodes[p].next].data != pchar)){
p = pList->nodes[p].next;
}
if(pList->nodes[p].next == -1){
printf("Cannot delete %c\n",pchar);
return;
}
q = pList->nodes[p].next;
pList->nodes[p].next = pList->nodes[pList->nodes[p].next].next;
pList->used[q] = 0;
}
测试函数
void insertdeleteTest(){
ListPtr tempList = iniLinkedList();
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);
}
主函数
int main(){
insertdeleteTest();
return 0;
}
测试结果
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'.
Cannot delete a
Deleting 'o'.
Cannot delete o
Hll
Space at 2 allocated.
Linking
Hxll
完整代码
#include <stdio.h>
#include <malloc.h>
#define SIZE 5
typedef struct staticLinkedNode{
char data;
int next;
}*NodePtr;
typedef struct staticLinkedList{
NodePtr nodes;
int* used;
}*ListPtr;
ListPtr iniLinkedList(){
ListPtr tempPtr = (ListPtr)malloc(sizeof(struct staticLinkedList));
tempPtr->nodes = (NodePtr)malloc(sizeof(struct staticLinkedNode)*SIZE);
tempPtr->used = (int*)malloc(sizeof(int)*SIZE);
tempPtr->nodes[0].data = 0;
tempPtr->nodes[0].next = -1;
tempPtr->used[0] = 1;
for(int i = 1; i < SIZE;i++){
tempPtr->used[i] = 0;
}
return tempPtr;
}
void printList(ListPtr pList){
int p = 0;
while(p != -1){
printf("%c",pList->nodes[p].data);
p = pList->nodes[p].next;
}
printf("\n");
}
void insertElement(ListPtr pList,char pchar,int pos){
int p,q,i;
p = 0;
for(int i = 0; i < pos;i++){
p = pList->nodes[p].next;
if(p == -1){
printf("The position %d is beyond the scope of the list.\n",pos);
return;
}
}
for(i = 0;i < SIZE;i++){
if(pList->used[i]==0){
printf("Space at %d allocated.\n",i);
pList->used[i] = 1;
q = i;
break;
}
}
if(i == SIZE){
printf("No space.\n");
return;
}
pList->nodes[q].data = pchar;
printf("Linking\n");
pList->nodes[q].next = pList->nodes[p].next;
pList->nodes[p].next = q;
}
void deleteElement(ListPtr pList,char pchar){
int p,q;
p = 0;
while((pList->nodes[p].next != -1) &&(pList->nodes[pList->nodes[p].next].data != pchar)){
p = pList->nodes[p].next;
}
if(pList->nodes[p].next == -1){
printf("Cannot delete %c\n",pchar);
return;
}
q = pList->nodes[p].next;
pList->nodes[p].next = pList->nodes[pList->nodes[p].next].next;
pList->used[q] = 0;
}
void insertdeleteTest(){
ListPtr tempList = iniLinkedList();
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);
}
int main(){
insertdeleteTest();
return 0;
}