【顺序表】的定义和实现1数组法

来源:互联网 发布:淘宝店铺的风格 编辑:程序博客网 时间:2024/05/21 16:11

/*
ADT List{
    数据对象:D={ai|ai∈ElemSet, i=1,2, …,n, n≧0}
    数据关系:R={<ai-1,ai>|ai-1,ai∈D, i=1,2, …,n }
    基本操作:
    InitList(&L)
      操作结果:构造一个空的线性表L。
    DestroyList(&L)
      初始条件:线性表L已存在。
      操作结果:销毁线性表L。
    ClearList(&L)
      初始条件:线性表L已存在。
      操作结果:将L重置为空表。
    ListEmpty(L)
      初始条件:线性表L已存在。
      操作结果:若L为空表,则返回TRUE,否则返回FALSE。
    ListLength(L)
      初始条件:线性表L已存在。
      操作结果:返回L中数据元素个数。
    GetElem(L,i,&e)
      初始条件:线性表L已存在,1≦i≦ListLength(L)。
      操作结果:用e返回L中第i个数据元素的值。
    LocateElem(L,e,compare())
      初始条件:线性表L已存在,compare()是数据元素判定函数。
      操作结果:返回L中第1个与e满足关系compare()的数据元素的位序。若这样的数据元素不存在,则返回值为0。
    PriorElem(L,cur_e,&pre_e)
      初始条件:线性表L已存在。
      操作结果:若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,否则操作失败,pre_e无定义。
    NextElem(L,cur_e,&next_e)
      初始条件:线性表L已存在。
      操作结果:若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,否则操作失败,next_e无定义。
    ListInsert(&L,i,e)
      初始条件:线性表L已存在,1≦i≦ListLength(L)+1。
      操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1。
    ListDelete(&L,i,&e)
      初始条件:线性表L已存在且非空,1≦i≦ListLength(L)。
      操作结果:删除L的第i个数据元素,并用e返回其值,L的长度减1。
    ListTraverse(L,visit())
      初始条件:线性表L已存在。
      操作结果:依次对L的每个数据元素调用函数visit()。一旦visit()失败,则操作失败。
}ADT List
*/

#include "stdio.h"
#include "conio.h"
#include "malloc.h"
#define INIT_SQ_SIZE 100
#define SQ_INCREMENT 10
#define INFEASIBLE -1
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType;

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

void InitList_Sq(SqList *L)
{
   L->elem=(ElemType *)malloc(sizeof(INIT_SQ_SIZE));
   if(!L->elem) exit(OVERFLOW);
   L->length=0;
   L->listsize=INIT_SQ_SIZE;
}

void DestroyList_Sq(SqList *L)
{
   free(L->elem);
   free(L);
}

void ClearList_Sq(SqList *L)
{
   int i;
   for(i=0;i<L->length;i++)
      L->elem[i]=0;
}

Status ListEmpty_Sq(SqList L)
{
   return (L.length==0?TRUE:FALSE);
}

int ListLength_Sq(SqList L)
{
   return L.length;
}

Status GetElem_Sq(SqList L,int i,int *e)
{
   if(i<1||i>L.length) return ERROR;
   *e=L.elem[i-1];
   return OK;
}

Status LocateElem_Sq(SqList L,int e,Status (*compare)(ElemType,ElemType))
{
   int i;
   for(i=0;i<L.length;i++)
      if(compare(e,L.elem[i])==0) return OK;
   return ERROR;
}

Status PriorElem_Sq(SqList L,ElemType cur_e,ElemType *pre_e)
{
   int i;
   if(L.elem[0]==cur_e)
      return ERROR;
   for(i=1;i<L.length;i++)
   {
      if(L.elem[i]==cur_e)
      {
         *pre_e=L.elem[i-1];
         return OK;
      }
   }
   return ERROR;
}

Status NextElem_Sq(SqList L,ElemType cur_e,ElemType *next_e)
{
   int i;
   for(i=0;i<L.length-1;i++)
   {
      if(L.elem[i]==cur_e) {
         *next_e=L.elem[i+1];
         return OK;
      }
   }
   return ERROR;
}     

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

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

Status ListTraverse_Sq(SqList L,Status (*visit)(ElemType))
{
   int i;
   for(i=0;i<L.length;i++)
      if(!visit(L.elem[i]))
         return ERROR;
   return TRUE;
}

原创粉丝点击