静态链表

一、静态链表的定义:
                            1.顺序表数组中的元素由两个数据域组成:date 和 next
                       2.date域用来存储数据
                       3.next域用来存储下一个元素在数组中的下标  



               静态链表是在顺序表的基础上利用数组实现的单链表。
静态链表的逻辑结构:


二、静态链表的相关定义:
     1.结点结构体定义
  
  
typedef struct _tag_StaticListNode
{
unsigned int data;
int next
}TstaitcListNode;
      2.静态链表结构体定义
  
  
typedef struct _tag_StaticList
{
int capacity;
TSstaticListNode header; //定义静态链表的头部
TSstaticListNode Node[]; //容纳静态链表数据元素数组的定义,为了避免使用指针,柔性
} TStaticList
三、静态链表操作:
   1.获取第POS个元素
         1.判断线性表是否合法
          2.判断位置是否合法
         3.有表头开始经过next域一移动POS个位置后,当前元素的next域既是当前要获取的数据元素的数组
   
   
for(i=0;i<pos;i++)
{
current=current->node[current].next; //current表示下一个元素在数组中的下标
}
object=current->node[current].next
    2.静态链表的插入操作
         1.判断线性表是否合法
          2.判断插入位置是否合法
          3.在数组元素中查找空闲位置index
          4.由表头开始通过next域一定POS次之后,当前元素的next域为要插入的位置
         5.将新元素插入
         6.线性表的长度加1
  
  
for(i=0 ;(i<pos)&&(sList->niode[current].next!=0);i++)
{
current=sList->node[current].next;
}
sList->node[index].next=sList->node[current].next;
sLsit->node[current].next=index;
 3.删除第POS个元素的操:
       1.判断线性表是否合法
      2.判断插入的位置是否合法
      3.获取低POS个元素
      4.将第POS个元素从链表中删除
      5.链表的长度减1
   
   
Object=sList->node[current].next;
sList->node[current].next=sList->node[Object].next

四、静态链表实例:
  StaticList.h
  
  
#ifndef _STATOCLIST_H_
#define _STATICLIST__H_
 
typedef void StaticList;
typedef void ListNode;
 
StaticList* StaticList_Creat(int capacity);
void StaticList_Destory(StaticList* list);
void StaticList_Clear(StaticList* list);
int StaticList_Capacity(StaticList* List);
int StaticList_Insert(StaticList* list, ListNode* node, int pos);
ListNode* StaticList_Delete(StaticList* list, int pos);
ListNode* StaticList_Get(StaticList* list, int pos);
int StaticList_Length(StaticList* list);
 
#endif
  StaticList.c
  
  
#include<stdio.h>
#include<malloc.h>
#include"StaticList.h"
 
#define AVALABLE -1 //为了查找空闲位置而定义
 
 
/*结点结构体定义*/
 
typedef struct _tag_StaticList
{
unsigned int data;
int next;
}TstaticListNode;
 
/*静态链表的结构体*/
typedef struct _tag_StaticListNode
{
int capacity;
TstaticListNode header;
TstaticListNode node[];
 
}TStaticList;
 
/*创建静态链表*/
StaticList* StaticList_Creat(int capacity)
{
TStaticList* ret = NULL;
int i = 0;
if (capacity >= 0)
{
ret = (StaticList*)malloc(sizeof(TStaticList) + sizeof(TstaticListNode)*(capacity + 1));
}
if (ret != NULL)
{
ret->capacity = capacity;
ret->header.data = 0;
ret->header.next = 0;
for (i = 1; i <= capacity; i++) //标识所有的位置为可用
{
ret->node[i].next=AVALABLE;
}
 
}
return ret;
 
}
void StaticList_Destory(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 = 1; i <= sList->capacity; i++) //标识所有的位置为可用
{
sList->node[i].next = AVALABLE;
}
}
}
int StaticList_Length(StaticList* list)
{
TStaticList* sList = (TStaticList*)list;
int ret = -1;
if (sList != NULL)
{
ret = sList->header.data;
}
 
return ret;
}
int StaticList_Capacity(StaticList* list)
{
TStaticList* sLsit = (TStaticList*)list;
int ret = -1;
if (sLsit != NULL)
{
ret = sLsit->capacity;
}
return ret;
 
}
int StaticList_Insert(StaticList* list, ListNode* node, int pos)
{
TStaticList* sList = (TStaticList*)list;
int ret = -1;
int index = 0; //标识可用位置的下标
int current = 0;
int i = 0;
ret = ret && (sList != NULL) && ((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 == AVALABLE) //找到可用位置下标,立即跳出
{
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;
}
/*新元素的next等于当前元素的next*/
sList->node[index].next= sList->node[current].next;
sList->node[current].next = index;
sList->node[0].data++; //链表的长度加1
sList->header = sList->node[0]; //更新表头信息
 
}
return ret;
}
 
ListNode* StaticList_Get(StaticList* list, int pos)
{
TStaticList* sList = (TStaticList*)list;
TstaticListNode* ret = NULL;
int i = 0;
int current = 0;
int object=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;
ret = (TstaticListNode*)(sList->node[object].data);
}
return ret;
}
ListNode* StaticList_Delete(StaticList* list, int pos)
{
 
TStaticList* sList = (TStaticList*)list;
TstaticListNode* ret = NULL;
int i = 0;
int current = 0;
int object = 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;
sList->node[current].next = sList->node[object].next;
sList->node[0].data--;
sList->header = sList->node[0]; //更新表头信息
sList->node[object].next = AVALABLE;
ret = (TstaticListNode*)(sList->node[object].data);
}
return ret;
 
 
}
main.c
   
   
#include<stdio.h>
#include"StaticList.h"
 
void main()
{
int index = 0;
 
int i = 0;
int j = 1;
int k = 2;
int x = 3;
int y = 4;
int z = 5;
StaticList* list = StaticList_Creat(10);
StaticList_Insert(list,&i,0);
StaticList_Insert(list, &j, 0);
StaticList_Insert(list, &k, 0);
StaticList_Insert(list, &x, 0);
StaticList_Insert(list, &y, 0);
StaticList_Insert(list, &z, 0);
 
for (index = 0; index < StaticList_Length(list); index++)
{
int *p = (int*)StaticList_Get(list, index);
printf("%d\n", *p);
}
while (StaticList_Length(list) > 0)
{
int *p = (int*)StaticList_Delete(list, 0);
printf("%d\n", *p);
}
printf("length is %d\n", StaticList_Length(list));
 
printf("Capacity is %d\n", StaticList_Capacity(list));
 
 
StaticList_Destory(list);
}

五、静态链表小结:
      1.静态链表其实是单链表的另外一种实现
     2.静态链表的实现“媒介”不是指针而数组
     3.静态链表的实现主要用于不支持指针的程序设计语言中
    4.静态链表的实现是一种简单的内存管理方法
 






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值