三、创建一个可复用的静态单链表

一、StaticList.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.c


#include <stdio.h>
#include <malloc.h>
#include "StaticList.h"


#define AVAILABLE -1     //链表空闲位置的标志 


typedef struct _tag_StaticListNode   //结点结构体的定义 
{
    unsigned int data;               //data为存放节点的数据信息 
    int next;                        //next存放下标元素 
} TStaticListNode;


typedef struct _tag_StaticList         //静态链表结构体定义 
{
    int capacity;                     //链表的最大容量 
    TStaticListNode header;          //表头结点 
    TStaticListNode node[];         //结点信息 
} TStaticList;


StaticList* StaticList_Create(int capacity) // O(n)
{
    TStaticList* ret = NULL;       
    int i = 0;
    
    if( capacity >= 0 )
    {
        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=1; i<=capacity; i++)
        {
            ret->node[i].next = AVAILABLE;
        }
    }
    
    return ret;
}


void StaticList_Destroy(StaticList* list) // O(1)
{
    free(list);    
}


void StaticList_Clear(StaticList* list) // O(n)
{
    TStaticList* sList = (TStaticList*)list;      //将链表进行强制转化 
    int i = 0;
    
    if( sList != NULL )
    {
        sList->header.data = 0;         //链表的长度置0 
        sList->header.next = 0;         //头节点的下标清空   
        
        for(i=1; i<=sList->capacity; i++)
        {
            sList->node[i].next = AVAILABLE;
        }
    }
}


int StaticList_Length(StaticList* list) // O(1)
{
    TStaticList* sList = (TStaticList*)list;       //将链表进行强制转化 
    int ret = -1;
    
    if( sList != NULL )
    {
        ret = sList->header.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)  // O(n)
{
    TStaticList* sList = (TStaticList*)list;         //将链表进行强制转化 
    int ret = (sList != NULL);
    int current = 0;
    int index = 0;
    int i = 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;      //将链表的头赋给链表的第0个元素,此时的current的下标为0 
        
        for(i=0; (i<pos) && (sList->node[current].next != 0); i++)      
        {
            current = sList->node[current].next;       //获取第pos元素的下标current 
        }
        
        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)  // O(n)
{
    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;              //将链表的头赋给链表的第0个元素,此时的current的下标为0 
        
        for(i=0; i<pos; i++)
        {
            current = sList->node[current].next;   //获取第pos个元素的下标 
        }
        
        object = sList->node[current].next;       //将第pos个元素的赋值给object 
        
        ret = (StaticListNode*)(sList->node[object].data);     //强制转化第pos元素的值,然后赋给ret 
    }
    
    return ret;
}


StaticListNode* StaticList_Delete(StaticList* list, int pos) // O(n)
{
    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;
        
        sList->node[current].next = sList->node[object].next;    //删除元素的下标赋给删除的前一个元素的下标 
        
        sList->node[0].data--;
        
        sList->header = sList->node[0];   //表头重新指向第0个元素 
        
        sList->node[object].next = AVAILABLE;  //标记为链表的空闲位置 
        
        ret = (StaticListNode*)(sList->node[object].data);//将删除的元素赋值给ret 
    }
    
    return ret;
}


三、main.c


#include <stdio.h>
#include <stdlib.h>
#include "StaticList.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(int argc, char *argv[])
{
    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;
}




静态链表的特点:

1.通过移动元素的下标来实现链表的相关操作;

2.pos为0代表头结点元素,最后一个元素的下标为0

3.将某些元素删除后,要将元素进行链表空闲位置的标记

4.在插入新元素时,要先给插入元素查找链表的空闲位置



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值