数据结构-1-线性表的顺序表示和实现

参考教材:数据结构严蔚敏C语言第二版

环境:CodeBlocks   c++

InitList(SqList &L)            //构造一个空的线性表L

CreateList(SqList &L,int n)    //创建新表

printList(SqList L)            //输出表

ListInsert(SqList &L,int i,ElemType e) //插入表

ListDelete(SqList &L,int i) //删除表

#include 
#include                 //调用exit
#include                  //调用OVERFLOW
#define MAXSIZE 100
#define OK 1
#define ERROR 0

using namespace std;

typedef int Status;
typedef int ElemType;

//线性表存储空间
typedef struct
{
    ElemType *elem;
    int length;
}SqList;

//创建空表
Status InitList(SqList &L)
{
    L.elem = new ElemType[MAXSIZE];
    if(!L.elem) exit(OVERFLOW);
    L.length = 0;
    return OK;
}

//创建新表
void CreateList(SqList &L,int n)
{
    for(int i=0;i>L.elem[i];
    L.length=n;
}

//顺序表的插入
Status ListInsert(SqList &L,int i,ElemType e)       //在顺序表第i个位(L.length+1>=i>=1)插入e
{

    if((i<1)||(i>L.length+1)) return ERROR;         //判断输入的i的值合不合法
    if(L.length == MAXSIZE) return ERROR;            //判断表是否满

    for(int j=L.length-1;j>=i-1;j--)                //顺序表0号元素
        L.elem[j+1]=L.elem[j];
    L.elem[i-1]=e;
    ++L.length;
    return OK;
    }

Status ListDelete(SqList &L,int i)                     //删除操作
{
    if((i<1)||(i>L.length)) return ERROR;
    for(int j=i;j<=L.length-1;j++)
        L.elem[j-1]=L.elem[j];
    --L.length;
    return OK;
}



//输出表
void printList(SqList L)
{
    cout<<"输出线性表"<>i>>e;
    cout<<"在顺序表第"<>m;
    ListDelete(L,m);
    printList(L);
    return 0;
}



你可能感兴趣的:(数据结构)