一、代码
静态链表的初始化、插入和删除。
#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 = 1; i < DEFAULT_SIZE; i ++){
tempPtr->used[i] = 0;
}//of for i
return tempPtr;
} //of initLinkedList
/**
* 打印链表
* @param paraListPtr是链表的头指针
*/
void printList(ListPtr paraListPtr){
int p = 0;
while(p != -1){
printf("%c",paraListPtr->nodes[p].data);
p = paraListPtr->nodes[p].next;
}//of while
printf("\r\n");
} //of printList
/**
* 在指定位置插入元素
*/
void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition){
int p, q, i;
//step 1. 找位置
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 ;
}//of if
} //of for i
//step 2. 构造一个新节点
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;
}//of if
}//of for i
if(i == DEFAULT_SIZE){
printf("No space.\r\n");
return ;
}//of if
paraListPtr->nodes[q].data = paraChar;
//step 3.连接
printf("linking\r\n");
paraListPtr->nodes[q].next = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = q;
} //of insertElement
/**
* 删除指定元素
*/
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;
}//of while
if(paraListPtr->nodes[p].next == -1){
printf("Cannot delete %c\r\n",paraChar);
return ;
}//of if
q = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = paraListPtr->nodes[paraListPtr->nodes[p].next].next;
paraListPtr->used[q] = 0; //等价于free(p);
} // of deleteElement
/**
* unit test.
*/
void appendInsertDeleteTest(){
//step 1. 初始化链表
ListPtr tempList = initLinkedList();
printList(tempList);
//step 2. 加入元素
insertElement(tempList, 'H', 0);
insertElement(tempList, 'e', 1);
insertElement(tempList, 'l', 2);
insertElement(tempList, 'l', 3);
insertElement(tempList, 'o', 4);
printList(tempList);
//step 3. 删除元素
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);
}// of appendInsertDeleteTest
/**
* the entrance
*/
int main(){
appendInsertDeleteTest();
return 0;
}//of main
二、运行结果
三、图示
四、总结
1.静态链表是用数组描述的链表,并且由两个数据域组成,data和next,即数组的每个下标都对应一个data和一个next. 其中data用来存放元素,next则相当于链表中的next指针。
2.静态链表的内存空间是固定的,在插入数据时,可以把空间建立地大一些,以免在插入过程中会溢出。
3.静态链表不同于动态链表在申请和释放时可以用malloc()和free()函数,静态链表中操作的是数组,所以为了辨别数组中的空间是否已经被使用,会采用used数组来存储值,其中1表示被使用,0表示未被使用。
4.静态链表的优点:在插入和删除操作时只需要改变游标,不需要移动元素的位置。
缺点:不能解决连续存储分配带来的表长难以确定的问题。