线性表

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#define Error -10000
struct node
{
    int *elem;
    int len;
};
void InitList(struct node *l)
{
    l->elem=(int *)malloc(sizeof(int)*100);
    l->len=0;
    //如果结构体是指针类型的取里面的值就用->
    //而不用.
}
void DestroyList(struct node *l)
{
    free(l->elem);
    l->len=0;
}
void ClearList(struct node *l)
{
    l->len=0;
}
int GetList(struct node *l,int id)
{
    if(id<1||id>l->len)
        return Error;
    return l->elem[id-1];
}
bool ListEmpty(struct node *l)
{
    if(l->len==0)
        return true;
    return false;
}
int ListLength(struct node *l)
{
    return l->len;
}
int LocateList(struct node *l,int e)
{
    int i;
    for(i=0;i<l->len;i++)
    {
        if(l->elem[i]==e)
        {
            return i+1;
        }
    }
    return Error;
}
int PriorList(struct node *l,int e)
{
    int i;
    for(i=0;i<l->len;i++)
    {
        if(l->elem[i]==e)
        {
            if(i>0)
            {
                return l->elem[i-1];
            }
        }
    }
    return Error;
}
int NextList(struct node *l,int e)
{
    int i;
    for(i=0;i<l->len;i++)
    {
        if(l->elem[i]==e)
        {
            if(i<l->len-1)
                return l->elem[i+1];
        }
    }
    return Error;
}
void ListInsert(struct node *l,int id,int e)
{
    if(id>l->len)
    {
        l->elem[l->len++]=e;
        return ;
    }
    if(id<1)
        id=1;
    int i;
    for(i=l->len-1;i>=id-1;i--)
    {
        l->elem[i+1]=l->elem[i];
    }
    l->elem[id-1]=e;
    l->len++;
}
void DeleteList(struct node *l,int id)
{
    if(id<1||id>l->len)
        return ;
    int i;
    for(i=id-1;i<l->len-1;i++)
    {
        l->elem[i]=l->elem[i+1];
    }
    l->len--;
}
void DisplayList(struct node *l)
{
    int i;
    for(i=0;i<l->len;i++)
        printf("%d ",l->elem[i]);
    printf("\n");
}
int main()
{
    struct node list;
    InitList(&list);
    ListInsert(&list,3,5);
    ListInsert(&list,1,4);
    ListInsert(&list,1,3);
    ListInsert(&list,1,2);


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值