| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 311 人关注过本帖
标题:新手求助一个静态链表的问题
只看楼主 加入收藏
sun110115
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-7-2
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
新手求助一个静态链表的问题
头文件
#ifndef _STATICLIST_H_
#define _STATICSLIT_H_

typedef void StaticList;
typedef void StaticListNode;

StaticList* StaticList_Create(int capacity);

void StaticList_Destroy(StaticList* list);

void StaticList_Clear(StaticList* list);

int StaticList_Capatcity(StaticList* list);

int StaticList_Length(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

实现文件

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

typedef struct _tag_TStaticListNode //定义插入的节点结构体
{
    unsigned int data; //存放插入的地址
    int next;//记录下一个节点的下标
}TStaticListNode;

typedef struct _tag_TStaticList//定义插入的链表头
{
    int capacity;//存放链表的最大长度
    int availible;//记录能存放的下标
    TStaticListNode header;//表头
    TStaticListNode node[];//存放数据的数组
   
}TStaticList;

StaticList* StaticList_Create(int capacity)
{
    TStaticList* ret = (TStaticList*)malloc(sizeof(TStaticList) + sizeof(TStaticListNode) * (capacity + 1) );//动态申请内存空间
    int i = 0;
    if(ret != NULL)//链表的初始化
    {
        ret->capacity = capacity;
        ret->header.data = 0;
        ret->header.next = 0;
        for(i = 1;i<capacity  ; i++)//将链表初始化从1开始,
        {
            ret->node[i].next = (i+1);   
        }
        ret->node[i].next = 0;
        ret->availible = 1;   
    }
   
    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 = 1;i<sList->capacity;i++)
        {
            sList->node[i].next = (i+1);   
        }
        sList->node[i].next = 0;
        sList->availible = 1;
    }
}

int StaticList_Capatcity(StaticList* list)
{
    int ret = -1;
    TStaticList* sList = (TStaticList*)list;
    if(sList != NULL)
    {
        ret = sList->capacity;   
    }
   
    return ret;
}

int StaticList_Length(StaticList* list)
{
    int ret = -1;
    TStaticList* sList = (TStaticList*)list;
    if(sList != NULL)
    {
        ret = sList->header.data;   
    }
   
    return ret;
   
}

int StaticList_Insert(StaticList* list, StaticListNode* node,int pos)
{
    int ret = 0;
    TStaticList* sList = (TStaticList*)list;
    ret = (sList != NULL);
    ret = ret && (0<=pos) && (sList->header.data +1 <= sList->capacity);
    int index = 0;
    int current = 0;
    int i = 0;
    if(ret)
    {
            index = sList->availible;
            sList->node[0] = sList->header;
            sList->node[index].data = (unsigned int)node;
            sList->availible = sList->node[index].next;
            for(i = 0;i<pos &&(sList->node[i].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)
{
    StaticListNode* ret = NULL;
    int current;
    int i = 0;
    TStaticList* sList = (TStaticList*)list;
    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;   
            }
            
            int index = sList->node[current].next;
            ret = (StaticListNode*)(sList->node[index].data);
        
    }
   
    return ret;
}

StaticListNode* StaticList_Delete(StaticList* list,int pos)
{
    StaticListNode* ret = NULL;
    int current = 0;
    int i = 0;
    TStaticList* sList = (TStaticList*)list;
    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;   
            }
            
            int index = sList->node[current].next;
            ret = (StaticListNode*)(sList->node[index].data);
            sList->node[current].next = sList->node[index].next;
            sList->node[index].next = sList->availible;
            sList->availible = index;
            sList->node[0].data--;
            sList->header = sList->node[0];
        
    }
   
    return ret;
}

测试文件

#include <stdio.h>
#include <malloc.h>
#include "StaticList2.0.h"

int main(void)
{
    StaticList* list = StaticList_Create(5);
    int v1 = 0;
    int v2 = 1;
    int v3 = 2;
    int v4 = 3;
    int v5 = 4;
    int i = 0;
    StaticList_Insert(list,&v1,0);
    StaticList_Insert(list,&v2,0);
    StaticList_Insert(list,&v3,0);
    StaticList_Insert(list,&v4,0);
    StaticList_Insert(list,&v5,0);  
    for(i= 0;i<StaticList_Length(list);i++)
    {
        printf("%d\n",*(int*)StaticList_Get(list,4));
        //printf("%d\n",i);   
    }
   
     

   
    return 0;
}

用for循环打印链表会出错,但是把i<StaticList_Length(list)改成i<5就不会错,
 for(i= 0;i<StaticList_Length(list);i++)
    {
        
        printf("%d\n",i);   
    }
这样测试了一下也没错,求教各位大神
搜索更多相关主题的帖子: capacity capacity 
2015-07-02 15:27
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9024
专家分:54030
注 册:2011-1-18
收藏
得分:20 
你所谓的出错,应该是“运行时出错吧”,编译时报了个错,说是current未初始化就使用了

StaticListNode* StaticList_Get(StaticList* list,int pos)
{
    StaticListNode* ret = NULL;
    int current;
    int i = 0;
    TStaticList* sList = (TStaticList*)list;
    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;  
            }
            
            int index = sList->node[current].next;
            ret = (StaticListNode*)(sList->node[index].data);
        
    }
   
    return ret;
}
2015-07-02 15:39
sun110115
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-7-2
收藏
得分:0 
回复 2楼 rjsp
谢谢大牛!!
2015-07-02 18:48
快速回复:新手求助一个静态链表的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016775 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved