三静态链表
Posted Alone0710
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三静态链表相关的知识,希望对你有一定的参考价值。
1. 单链表劣势
单链表严重依赖指针,数据元素中必须包含一个额外的指针域,没有指针的程序设计语言无法实现。
2. 静态链表的定义
- 循序表数组中的元素由两个数据域组成:data和next
- data域用于存储数据
- next域用于存储下一个元素在数组中的下标
注:header=1的意思是指第一个元素是数组下标为1的元素
静态链表是在顺序表的基础上利用数组实现的单链表!
3. 相关操作
柔性数组的概念在专栏《C语言剖析》里做过笔记
3. 语言描述
.h文件
#ifndef _STATICLIST_H_
#define _STATICLIST_H_
typedef void StaticList;
typedef void StaticListNode;
StaticList* StaticList_Create(int capacity);
void StaticList_Destroy(StaticList* list);
void StaticList_Clear(StaticList* list);
int StaticList_Length(StaticList* list);
int StaticList_Capacity(StaticList* list);
int StaticList_Insert(StaticList* list, StaticListNode* node, int pos);
StaticListNode* StaticList_Get(StaticList* list, int pos);
StaticListNode* StaticList_Delete(StaticList* list, int pos);
#endif // _STATICLIST_H_
#include <stdio.h>
#include <malloc.h>
#include "StaticList.h"
#define AVAILABLE -1
//结点结构体的定义
typedef struct _tag_StaticListNode
{
unsigned int data;
int next;
}TStaticListNode;
typedef struct _tag_StaticList
{
int capacity;
TStaticListNode header;
TStaticListNode node[];//柔性数组
}TStaticList;
StaticList* StaticList_Create(int capacity)
{
TStaticList* ret = NULL;
int i=0;
if(capacity >= 0)//+1申请出来的空间是给表头用的
{
ret = (TStaticList*)malloc(sizeof(TStaticList)+sizeof(TStaticListNode)*(capacity+1));
}
if(ret != NULL)
{
ret->capacity = capacity;
ret->header.data=0;
ret->header.next=0;
for(i=0;i<=capacity;i++)
{
ret->node[i].next = AVAILABLE;
}
}
return ret;
}
void StaticList_Destroy(StaticList* list)
{
free(list);
}
void StaticList_Clear(StaticList* list)
{
TStaticList* sList = (TStaticList*)list;
int i=0;
if(sList != NULL)
{
sList->header.data=0;
sList->header.next=0;
for(i=0;i<=sList->capacity;i++)
{
sList->node[i].next = AVAILABLE;
}
}
}
int StaticList_Length(StaticList* list)
{
TStaticList* sList = (TStaticList*)list;
int ret = -1;
if(sList!=NULL)
{
ret = sList->header.data;//data只是个数组下标
}
return ret;
}
int StaticList_Capacity(StaticList* list)
{
TStaticList* sList = (TStaticList*)list;
int ret = -1;
if(sList!=NULL)
{
ret = sList->capacity;
}
return ret;
}
int StaticList_Insert(StaticList* list, StaticListNode* node, int pos)
{
TStaticList* sList = (TStaticList*)list;
int ret = (sList!=NULL);
int index=0;
int i=0;
int current=0;
ret = ret && (sList->header.data+1 <= sList->capacity);//长度是否小于容量
ret = ret && (pos >= 0) && (node != NULL);
if(ret)
{
for(i=1;i<sList->capacity;i++)
{
if(sList->node[i].next == AVAILABLE)
{
index = i;
break;
}
}
sList->node[index].data = (unsigned int)node;
sList->node[0] = sList->header;
for(i=0;i<pos && sList->node[current].next!=0;i++)
{
current = sList->node[current].next;
}
sList->node[index].next = sList->node[current].next;
sList->node[current].next = index;
sList->node[0].data++;
sList->header = sList->node[0];
}
return ret;
}
StaticListNode* StaticList_Get(StaticList* list, int pos)
{
TStaticList* sList = (TStaticList*)list;
StaticListNode* ret = NULL;
int current = 0;
int object=0;
int i=0;
if(sList!=NULL && 0<=pos && pos<sList->header.data)
{
sList->node[0] = sList->header;
for(i=0;i<pos;i++)
{
current = sList->node[current].next;
}
object = sList->node[current].next;//object就是想要获取的元素的下标
ret = (StaticListNode*)(sList->node[object].data);
}
return ret;
}
StaticListNode* StaticList_Delete(StaticList* list, int pos)
{
TStaticList* sList = (TStaticList*)list;
StaticListNode* ret = NULL;
int current = 0;
int object=0;
int i=0;
if(sList!=NULL && 0<=pos && pos<sList->header.data)
{
sList->node[0] = sList->header;
for(i=0;i<pos;i++)
{
current = sList->node[current].next;
}
object = sList->node[current].next;//object就是想要获取的元素的下标
sList->node[current].next = sList->node[object].next;
sList->node[0].data--;
sList->header = sList->node[0];
sList->node[object].next = AVAILABLE;
ret = (StaticListNode*)(sList->node[object].data);
}
return ret;
}
#include <stdio.h>
#include <stdlib.h>
#include "StaticList.h"
int main()
{
StaticList* list = StaticList_Create(10);
int index=0;
int i=0;
int j=1;
int k=2;
int x=3;
int y=4;
int z=5;
StaticList_Insert(list,&i,0);
StaticList_Insert(list,&j,0);
StaticList_Insert(list,&k,0);
for(index=0;index<StaticList_Length(list);index++)
{
int* p=(int*)StaticList_Get(list,index);
printf("%d\\n",*p);
}
printf("\\n");
while( StaticList_Length(list) > 0 )
{
int* p=(int*)StaticList_Delete(list,0);
printf("%d\\n",*p);
}
printf("\\n");
StaticList_Insert(list,&x,0);
StaticList_Insert(list,&y,0);
StaticList_Insert(list,&z,0);
printf("Capacity: %d Length: %d\\n",StaticList_Capacity(list),StaticList_Length(list));
for(index=0;index<StaticList_Length(list);index++)
{
int* p=(int*)StaticList_Get(list,index);
printf("%d\\n",*p);
}
StaticList_Destroy(list);
return 0;
}
4. 小结
静态链表其实是单链表的另一种实现方法
静态链表的实现“媒介”不是指针而是数组
静态链表主要用于不支持指针的程序设计语言中
静态链表的实现是一种内存管理的简易方法
以上是关于三静态链表的主要内容,如果未能解决你的问题,请参考以下文章