顺序线性表的实现

SqList.h

//函数结果状态代码
#define    TRUE    1
#define    FALSE    0
#define    OK        1
#define    ERROR    0
#define    INFEASIBLE    -1
#define    OVERFLOW    -2
//Status是函数的类型,其值是函数结果状态代码
typedef int Status;
typedef int ElemType;


#define LIST_INIT_SIZE        100        //线性表存储空间的初始分配量
#define LISTINCREMENT        10        //线性表存储空间的分配增量

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

Status InitList_Sq(SqList &L){
    //构造一个空的线性表L。
    L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem)    exit(OVERFLOW);                //存储分配失败
    L.length=0;                                //空表长度为0
    L.listsize=LIST_INIT_SIZE;                //初始存储容量
    return OK;
}//InitList_Sq

Status ListInsert_Sq(SqList &L,int i,ElemType e){
    int *newbase,*q,*p;
    if(i<1||i>L.length+1)    return ERROR;        //i值不合法
    if(L.length>=L.listsize){                    //当前存储空间已满,增加分配
        newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
        if(!newbase)    exit(OVERFLOW);            //存储分配失败
        L.elem=newbase;                            //新基址
        L.listsize+=LISTINCREMENT;                //增加存储容量
    }
    q=&(L.elem[i-1]);                            //q为插入位置
    for(p=&(L.elem[L.length-1]);p>=q;--p)    *(p+1)=*p;
                                                //插入位置及之后的元素右移
    *q=e;                //插入e
    ++L.length;            //表增长
    return OK;
}//InitList_Sq

Status ListDelete_Sq(SqList &L,int i,ElemType &e){
    //在顺序线性表L中删除第i个元素,并用e返回其值
    //i的合法值为1<=i<=ListLength_Sq(L)
    int *p,*q;
    if((i<1)||(i>L.length))    return ERROR;    //i值不合法
    p=&(L.elem[i-1]);                        //p为被删除元素的位置
    e=*p;                                    //被删除元素的值赋给e
    q=L.elem+L.length-1;                    //表尾元素的位置
    for(++p;p<=q;++p)    *(p-1)=*p;            //被删除元素之后的元素左移
    --L.length;                                //表长减1
    return OK;
}//ListDelete_Sq

int LocateElem_Sq(SqList L,ElemType e,Status(*compare)(ElemType,ElemType)){
    //在顺序线性表L中查找第1个值与e满足compare()的元素的位序
    //若找到,则返回其在L中得位序,否则返回0
    int i=1;
    int *p=L.elem;
    while(i<=L.length && !(*compare)(*p++,e))    ++i;
    if(i<=L.length)    return i;
    else    return 0;
}//LocateElem_Sq

int Compare_Sq(ElemType e1,ElemType e2){
    //比较两个ElemType类型的元素是否相等。相等返回1,否则返回-1。
    if(e1==e2)    
        return 1;
    else    
        return 0;
}//Compare_Sq

void MergeList_Sq(SqList La,SqList Lb,SqList &Lc){
    //已知顺序线性表La和Lb的元素按值非递减排序
    //归并La和Lb得到新的顺序线性表Lc,Lc的元素也按值非递减排序
    int *pa,*pb,*pc;
    int *pa_last,*pb_last;
    pa=La.elem;
    pb=Lb.elem;
    Lc.listsize=Lc.length=La.length+Lb.length;
    pc=Lc.elem=(ElemType*)malloc(Lc.listsize*sizeof(ElemType));
    if(!pc)    exit(OVERFLOW);            //存储分配失败
    pa_last=La.elem+La.length-1;
    pb_last=Lb.elem+Lb.length-1;
    while(pa<=pa_last && pb<=pb_last){    //归并
        if(*pa<=*pb)    *pc++=*pa++;
        else    *pc++=*pb++;
    }
    while(pa<=pa_last)    *pc++=*pa++;    //插入La的剩余元素
    while(pb<=pb_last)    *pc++=*pb++;    //插入Lb的剩余元素
}//MergeList_Sq
#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include "malloc.h"

#include "SqList.h"

int main(int argc, char* argv[])
{
    SqList L;
    int s1=123,s2=456;
    int l;

    InitList_Sq(L);
    ListInsert_Sq(L,1,s1);
    ListInsert_Sq(L,2,s2);
    printf("%d	%d	",*L.elem,L.elem[0-1]);

    l=LocateElem_Sq(L,*(L.elem+1),Compare_Sq);
    printf("%d	",l);
    return 0;
}
原文地址:https://www.cnblogs.com/13yan/p/3376401.html