实验 1 顺序表和链表的基本实现
实验目的:掌握顺序表和链表的基本操作, 并代码实现。
(三道题)
实验 2-1 实现顺序表的基本操作。
代码:(由文件 sqlist.cpp 和 exp2-1.cpp)
//文件名:sqlist.cpp
//顺序表运算算法
#include <stdio.h>
#include <malloc.h>
#define MaxSize 50
typedef char ElemType;
typedef struct
{ ElemType data[MaxSize]; //存放顺序表元素
int length; //存放顺序表的长度
} SqList; //声明顺序表的类型
void CreateList(SqList *&L,ElemType a[],int n) //整体建立顺序表
{
L=(SqList *)malloc(sizeof(SqList));
for (int i=0;i<n;i++)
L->data[i]=a[i];
L->length=n;
}
void InitList(SqList *&L) //初始化线性表
{
L=(SqList *)malloc(sizeof(SqList)); //分配存放线性表的空间
L->length=0;
}
void DestroyList(SqList *&L) //销毁线性表
{
free(L);
}
bool ListEmpty(SqList *L) //判线性表是否为空表
{
return(L->length==0);
}
int ListLength(SqList *L) //求线性表的长度
{
return(L->length);
}