定义:静态链表就是借助数组来描述线性表的链式存储结构,此时的数组的分量就是我们自己定义的结构体,数组中的一个分量表示一个结点,同时用游标代替指针描述结点在数组中的相对位置,即数组的分量形式是结点+游标。此外,数组的第0个分量可以看成链表中的头结点,其游标即链表中的指针域则就指向链表的第一个结点。
游标 | 0 | 1 | 2 | 3 | 4 | ······ | ······· |
数据data | |||||||
used | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
next | -1 | -1 | -1 | -1 | -1 | -1 | -1 |
1、初始化链表
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){
//与malloc分配相同
printf("space at %d allocated.\r\n",i);
paraListPtr->used[i]=1;
q=i;
break;
}
if(i==DEFAULT_SIZE){
printf("无可用空间.\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;
}
2、插入元素
#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(StaticLinkedList));
//分配总空间
tempPtr->nodes=(NodePtr)malloc(sizeof(struct StaticLinkedNode)*DEFAULT_SIZE);
//设置表头,头节点
tempPtr->nodes[0].data='\0';
tempPtr->nodes[0].next=-1;
//只有头节点被占用
tempPtr->used[0]=1;
for(int i=1;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");
}
3、删除元素
void deleteElement(ListPtr paraListPtr,char paraChar)
{
int p,q;
p=0;
while((paraListPtr_>noedes[p].next!=-1)&&(paraList->nodes[paraListptr->nodes[p].next].data!=paraChar)){
p=paraListPtr->nodes[p].next;
}
if(paraListPtr->nodes[p].next==-1){
printf("无法删除%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 appendInsertDeleteTest(){
// Step 1. Initialize an empty list.
ListPtr tempList = initLinkedList();
printList(tempList);
// Step 2. Add some characters.
insertElement(tempList, 'H', 0);
insertElement(tempList, 'e', 1);
insertElement(tempList, 'l', 2);
insertElement(tempList, 'l', 3);
insertElement(tempList, 'o', 4);
printList(tempList);
// Step 3. Delete some characters (the first occurrence).
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);
}
完整代码:
#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(StaticLinkedList));
//分配总空间
tempPtr->nodes=(NodePtr)malloc(sizeof(struct StaticLinkedNode)*DEFAULT_SIZE);
//设置表头,头节点
tempPtr->nodes[0].data='\0';
tempPtr->nodes[0].next=-1;
//只有头节点被占用
tempPtr->used[0]=1;
for(int i=1;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){
//与malloc分配相同
printf("space at %d allocated.\r\n",i);
paraListPtr->used[i]=1;
q=i;
break;
}
if(i==DEFAULT_SIZE){
printf("无可用空间.\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;
p=0;
while((paraListPtr_>noedes[p].next!=-1)&&(paraList->nodes[paraListptr->nodes[p].next].data!=paraChar)){
p=paraListPtr->nodes[p].next;
}
if(paraListPtr->nodes[p].next==-1){
printf("无法删除%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 appendInsertDeleteTest(){
// Step 1. Initialize an empty list.
ListPtr tempList = initLinkedList();
printList(tempList);
// Step 2. Add some characters.
insertElement(tempList, 'H', 0);
insertElement(tempList, 'e', 1);
insertElement(tempList, 'l', 2);
insertElement(tempList, 'l', 3);
insertElement(tempList, 'o', 4);
printList(tempList);
// Step 3. Delete some characters (the first occurrence).
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(){
appendInsertDeleteTest();
}
运行结果:
Space at 1 allocated.
linking
Space at 2 allocated.
linking
Space at 3 allocated.
linking
Space at 4 allocated.
linking
无可用空间.
Hell
Deleting 'e'.
Deleting 'a'.
Cannot delete a
Deleting 'o'.
Cannot delete o
Hll
Space at 2 allocated.
linking
Hxll