《数据结构》线性表伪码变代码

《数据结构》线性表伪码变代码
前几天读ruby049中,array.c时,发现和数据结构课本上的实现很像。于是今天就把严教授课本上的伪码输入到计算机中,进行了调试。
代码如下:
#define LIST_INIT_SIZE 100
#define LISTINCE 10
#include<stdio.h>
#include<stdlib.h>

typedef int ElemType;
typedef struct {
    ElemType *elem;
    int length;
    int listsize;
} SqList;

int InitList_Sq(SqList *L)
{
    L->elem=(ElemType *)malloc(LIST_INIT_SIZE *sizeof(ElemType));
    if (!L->elem) exit(-1);
    L->length=0;
    L->listsize=LIST_INIT_SIZE;
    return 0;
}
void Print_Sq(SqList *L)
{
    int i;
    for(i=0;i<L->length;i++){
        printf("%d,",L->elem[i]);
    }
    printf("\n");
}
int ListInsert_Sq(SqList *L,int i,ElemType e)
{
    if (i<1 || i>L->length+1) return -1;
    if (L->length >= L->listsize){
        ElemType *newbase;
        newbase=(ElemType *)(realloc(L->elem,(L->listsize+LISTINCE)*sizeof(ElemType)));
        if (!newbase) exit(-1);
        L->elem=newbase;
        L->listsize +=LISTINCE;
    }
    ElemType *q=&(L->elem[i-1]);
    ElemType *p;
    for(p=&(L->elem[L->length-1]);p>=q;--p)
        *(p+1)=*p;
    *q=e;
    L->length++;
    return 0;
}
int ListDelete_Sq(SqList *L,int i,ElemType *e)
{
    if (i<1 || i>L->length) return -1;
    ElemType *p=&(L->elem[i-1]);
    *e=*p;
    ElemType *q=&(L->elem[L->length-1]);
    for(++p;p<=q;++p)
        *(p-1)=*p;
    L->length--;
    return 0;
}
int main()
{
    SqList a;
    int status=InitList_Sq(&a);
    if (status==0) printf("ok\n");
    Print_Sq(&a);
    ListInsert_Sq(&a,1,8);
    ListInsert_Sq(&a,2,18);
    ListInsert_Sq(&a,3,28);
    Print_Sq(&a);
    ElemType e;
    ListDelete_Sq(&a,2,&e);
    Print_Sq(&a);
    printf("hello\n");
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值