静态链表--C语言实现

list.h

#ifndef _LIST_H_
#define _LIST_H_
#define SpaceSize 100

typedef int PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
typedef int ElementType;

int IsEmpty(const List L);
int IsLast(const Position P,const  List L);
Position Find(ElementType X, const List L);
void Delete(ElementType X, List L);
Position FindPrevious(ElementType X, const List L);
void Insert(ElementType X, List L, Position P);
void DeleteList(List L);
ElementType Retrieve(const Position P);

#endif

list.c

#include<stdio.h>
#include<stdlib.h>
#include"list.h"

struct Node
{
    ElementType Element;
    Position Next;
};
struct Node CursorSpace[SpaceSize];
static Position CursorAlloc()
{
    Position P;
    P = CursorSpace[0].Next;
    CursorSpace[0].Next = CursorSpace[P].Next;
    return P;
}
static void CursorFree(Position P)
{
    CursorSpace[P].Next = CursorSpace[0].Next;
    CursorSpace[0].Next = P;
}
int IsEmpty(const List L)
{
    return CursorSpace[L].Next == 0;
}
int IsLast(const Position P, const  List L)
{
    return CursorSpace[P].Next == 0;
}
Position Find(ElementType X, const List L)
{
    Position P;
    P = CursorSpace[L].Next;
    while (P && CursorSpace[P].Element!= X)
        P = CursorSpace[P].Next;
    return P;
}
void Delete(ElementType X, List L)
{
    Position P, TmpCell;
    P = FindPrevious(X, L);
    while (!IsLast(P, L))
    {
        TmpCell = CursorSpace[P].Next;
        CursorSpace[P].Next = CursorSpace[TmpCell].Next;
        CursorFree(TmpCell);
    }
}
Position FindPrevious(ElementType X, const List L)
{
    Position P = L;
    Position N = CursorSpace[P].Next;
    while (N != 0 &&CursorSpace[N].Element!=X )
    {
        P = CursorSpace[P].Next;
        N = CursorSpace[P].Next;
    }
    return P;
}
void Insert(ElementType X, List L, Position P)
{
    Position TmpCell;
    TmpCell = CursorAlloc();
    if (TmpCell == 0)
    {
        printf("FAILURE!\n");
        exit(EXIT_FAILURE);
    }
    CursorSpace[TmpCell].Element = X;
    CursorSpace[TmpCell].Next = CursorSpace[P].Next;
    CursorSpace[P].Next = TmpCell;
}
void DeleteList(List L)
{
    Position P, Tmp;
    P = CursorSpace[L].Next;
    CursorSpace[L].Next = 0;
    while (P)
    {
        Tmp = CursorSpace[P].Next;
        CursorFree(P);
        P = Tmp;
    }
}
ElementType Retrieve(const Position P)
{
    if (P != 0)
        return CursorSpace[P].Element;
    printf("ERROR!\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值