#include<iostream>
using namespace std;
#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 StaticLinkedNode));
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;
}
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;
p=0;
for(int 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;
}
}//判断插入位置是否合法
int i=1;
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;
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->nodes[p].next!=-1)&&(paraListPtr->nodes[paraListPtr->nodes[p].next].data!=paraChar))
{
p=paraListPtr->nodes[p].next;
}
if (paraListPtr->nodes[p].next==-1)
{
printf("Cannot 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 appendInsertDeleteTest(){
ListPtr tempList = initLinkedList();
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()
{
appendInsertDeleteTest();
return 0;
}
学习心得:
-
静态链表:这段代码实现了静态链表,即使用数组来模拟链表结构。在静态链表中,节点的指针指向数组的索引,而不是直接指向下一个节点的地址。这样做的好处是可以避免频繁的内存分配和释放操作,但缺点是需要提前确定链表的最大长度。
-
初始化链表:在
initLinkedList()
函数中,通过分配内存空间来初始化链表。首先分配StaticLinkedList
结构体的内存空间,然后分配存储节点和空间状态的数组的内存空间。接着,初始化头结点,并将空间状态数组初始化为未使用状态。 -
插入元素:
insertElement()
函数用于在链表的指定位置插入元素。首先找到插入位置,然后检查是否有空间可用,如果没有则提示无空间。如果有空间可用,则将节点插入到链表中,并更新链表结构。 -
删除元素:
deleteElement()
函数用于删除链表中指定元素。首先找到要删除元素的前一个节点,然后更新前一个节点的指针,将其指向被删除元素的下一个节点,最后释放被删除元素的空间。 -
测试函数:
appendInsertDeleteTest()
函数用于测试链表的初始化、插入、删除等功能。通过连续调用插入函数,将字符 ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ 插入到链表中,并进行删除操作。最后插入字符 ‘x’ 并打印链表。
这段代码帮助我更深入地理解了链表的底层实现原理,以及静态链表的概念和实现方式。同时,通过阅读和分析这段代码,我也学会了如何设计和实现基本的链表操作函数。