线性表-链表-顺序结构-SequentialList.c

#include  " ../include.h "

#define  LIST_INIT_SIZE    3
#define  LISTINCREMENT    2

typedef 
struct
{
    ElemType 
*base;
    
int length;
    
int listsize;
}
List;

Status InitList(List 
* l)
{
    l
->base = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));

    
if(!l->base)
    
{
    exit(OVERFLOW);
    }


    l
->length = 0;
    l
->listsize = LIST_INIT_SIZE;
}


Status DestoryList(List 
* l)
{
    
if(!l->base)
    
{
    free(l
->base);
    }


    l
->length = 0;
    l
->listsize = 0;
}


Status ListInsert(List 
* l,ElemType e, int  i)
{
    
if(i < 0 || i > l->length)
    
{
    
return ERROR;
    }


    
if(l->length >= l->listsize)
    
{
    ElemType 
*newbase = (ElemType *)realloc(l->base,(l->listsize + LISTINCREMENT) * sizeof(ElemType));

    
if(!newbase)
    
{
        exit(OVERFLOW);
    }


    l
->base = newbase;
    l
->listsize += LISTINCREMENT;
    }


    ElemType 
*pei = l->base + i;
    ElemType 
*pej = l->base + l->length - 1;

    
while(pej >= pei)
    
{
    
*(pej + 1= *pej;

    pej
--;
    }


    
*pei = e;

    l
->length++;

    
return OK;
}


Status ListDelete(List 
* l,ElemType  * e, int  i)
{
    
if(i < 0 || i >= l->length)
    
{
    
return ERROR;
    }


    ElemType 
*pei = l->base + i;
    ElemType 
*pej = l->base + l->length - 1;

    
*= *pei;

    
while(pei < pej)
    
{
    
*pei = *(pei + 1);

    pei
++;
    }


    l
->length--;

    
return OK;
}


int  Locate(List l,ElemType e)
{
    
int i;

    
for(i = 0; i < l.length; i++)
    
{
    
if(*(l.base + i) == e)
    
{
        
return i;
    }

    }


    
return INFEASIBLE;
}


void  Print(List l)
{
    
int i;

    
for(i = 0; i < l.length; i++)
    
{
    printf(
"%d ",*(l.base + i));
    }


    printf(
" ");
}


int  main()
{
    List l;

    printf(
"Init list...");

    InitList(
&l);

    printf(
"initialed. ");

    printf(
"Insert elements: 6 3 2 19 0 9 38 42 85 99 34 63 ");

    ListInsert(
&l,6,0);
    ListInsert(
&l,3,1);
    ListInsert(
&l,2,2);
    ListInsert(
&l,19,3);
    ListInsert(
&l,63,4);
    ListInsert(
&l,34,4);
    ListInsert(
&l,99,4);
    ListInsert(
&l,85,4);
    ListInsert(
&l,42,4);
    ListInsert(
&l,38,4);
    ListInsert(
&l,9,4);
    ListInsert(
&l,0,4);
    
    printf(
"Result: ");

    Print(l);

    printf(
"Locate elements: 42 63 6 ");

    printf(
"Result: ");

    printf(
"%d ",Locate(l,42));
    printf(
"%d ",Locate(l,63));
    printf(
"%d ",Locate(l,6));

    printf(
"Delete elements: 0 42 63 6 ");

    ElemType e;

    ListDelete(
&l,&e,11);
    ListDelete(
&l,&e,7);
    ListDelete(
&l,&e,4);
    ListDelete(
&l,&e,0);

    printf(
"Result: ");

    Print(l);

    printf(
"Destory list...");

    DestoryList(
&l);

    printf(
"destoried. ");
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值