线性表的顺序存储

"Common.h"

#ifndef COMMON_H_INCLUDED
#define COMMON_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

typedef int Status;
typedef int ElemType;

#endif // COMMON_H_INCLUDED

"List_Seq.h"

//链表的顺序存储

#ifndef LIST_SEQ_H_INCLUDED
#define LIST_SEQ_H_INCLUDED

#include "Common.h"

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef struct {
    ElemType * elem;//存储空间基址
    int length;//当前长度
    int listsize;//当前分配的存储容量,以sizeof(ElemType)为单位
}SqList;

Status InitList_Seq(SqList * list);
Status DestroyList_Seq(SqList * list);
Status ClearList_Seq(SqList * list);
Status ListEmpty_Seq(SqList * list);
Status ListLength_Seq(SqList * list);
void GetElem_Seq(SqList * list, int i, ElemType * e);
int LocateElem_Seq(SqList * list, ElemType e);
void PriorElem_Seq(SqList * list, ElemType cur_e, ElemType * pre_e);
void NextElem_Seq(SqList * list, ElemType cur_e, ElemType * next_e);
Status ListInsert_Seq(SqList * list, int i, ElemType e);
Status ListDelete_Seq(SqList * list, int i, ElemType * e);
void ListTraverse(SqList * list);

#endif // LIST_SEQ_H_INCLUDED

"List_Seq.c"

#include "List_Seq.h"
Status InitList_Seq(SqList * list)
{
    list->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
    if(!list->elem)
        exit(OVERFLOW);
    list->length = 0;
    list->listsize = LIST_INIT_SIZE;

    return OK;
}

Status DestroyList_Seq(SqList * list)
{
    if(!list->elem)
        exit(ERROR);
    free(list->elem);
    list->elem = NULL;
    list->length = 0;
    list->listsize = 0;

    return OK;
}

Status ClearList_Seq(SqList * list)
{
    if(!list->elem)
        exit(ERROR);
    list->length = 0;
    //list->listsize = LIST_INIT_SIZE;

    return OK;
}

Status ListEmpty_Seq(SqList * list)
{
    if(!list->elem)
        exit(ERROR);
    if(list->length == 0)
        return TRUE;
    else
        return FALSE;
}

Status ListLength_Seq(SqList * list)
{
    if(!list->elem)
        exit(ERROR);
    return list->length;
}

void GetElem_Seq(SqList * list, int i, ElemType * e)
{
    if(!list->elem || i>list->length || i<1)
        exit(ERROR);
    *e = list->elem[i-1];
}

//返回与e相等的元素的位序
int LocateElem_Seq(SqList * list, ElemType e)
{
    if(!list->elem)
        exit(ERROR);
    int i;
    for(i=0; i<ListLength_Seq(list); i++)
    {
        if(list->elem[i] == e)
            return i+1;
    }
    return 0;
}

//返回cur_e的前驱,保存在pre_e中
void PriorElem_Seq(SqList * list, ElemType cur_e, ElemType * pre_e)
{
    if(!list->elem)
        exit(ERROR);
    int i;
    for(i=0; i<ListLength_Seq(list); i++)
    {
        if(cur_e == list->elem[i] && i!=0)
            *pre_e = list->elem[i-1];
    }
}

//返回cur_e的后继,保存在next_e中
void NextElem_Seq(SqList * list, ElemType cur_e, ElemType * next_e)
{
    if(!list->elem)
        exit(ERROR);
    int i;
    for(i=0; i<ListLength_Seq(list); i++)
    {
        if(cur_e == list->elem[i] && i!=ListLength_Seq(list)-1)
            *next_e = list->elem[i-1];
    }
}

Status ListInsert_Seq(SqList * list, int i, ElemType e)
{
    if(!list->elem)
        exit(ERROR);
    if(i<1 || i>ListLength_Seq(list)+1)
        return ERROR;
    if(list->length == list->listsize)
    {
        ElemType * newbase = (ElemType *)realloc(list->elem, (list->listsize + LISTINCREMENT)*sizeof(ElemType));
        if(!newbase)
            exit(OVERFLOW);
        list->elem = newbase;
        list->listsize += LISTINCREMENT;
    }
    int j;
    for(j=ListLength_Seq(list); j>i-1; j--)
    {
        list->elem[j] = list->elem[j-1];
    }
    list->elem[i-1] = e;
    list->length++;
    return OK;
}

Status ListDelete_Seq(SqList * list, int i, ElemType * e)
{
    if(!list->elem)
        exit(ERROR);
    if(i<1 || i>ListLength_Seq(list))
        return ERROR;
    int j;
    *e = list->elem[i-1];
    for(j=i; j<ListLength_Seq(list); j++)
    {
        list->elem[j-1] = list->elem[j];
    }
    list->length--;
    return OK;
}

void ListTraverse(SqList * list)
{
    if(!list->elem)
        exit(ERROR);
    int i;
    for(i=0; i<ListLength_Seq(list); i++)
    {
        printf("%d  ", list->elem[i]);
    }
    printf("\n");
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值