头文件:
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#define maxsize 50
#define error 0
#define ok 1
typedef int ElemType;
typedef struct{
ElemType *elem; //存储空间基址
int length; //当前空间
int listsize; //当前分配的存储容量(以sizeof(ElemType)为单位)
}SqList;
int InitSqList(SqList &l);
int DestroySqList(SqList &l);
int IsSqListEmpty(SqList &l);
int IsSqListFull(SqList &l);
int SqListLength(SqList &l);
int GetSqListElement(SqList &l, int i, ElemType &e);
int SqListLocation(SqList &l, ElemType x);
int SqListPrior(SqList &l, ElemType x);
int SqListNext(SqList &l, ElemType x);
int InsertSqList(SqList &l, int i, ElemType e);
int DeleteSqList(SqList &l, int i, ElemType &e);
void SortSqList(SqList &l);
void PrintSqList(SqList &l);
//初始化线性表,构造一个空的线性表
int InitSqList(SqList &l)
{
l.elem=(ElemType *)malloc(maxsize*sizeof(ElemType));
if(!l.elem) //若存储分配失败,则返回0
{
cout<<"error";
return error;
}
l.length=0; //当前空间为零
l.listsize=maxsize; //当前分配的存储容量为maxsize
cout<<"初始化已完成!"<<endl;
return ok;
}
//销毁已存在的线性表
int DestroySqList(SqList &l)
{
if(!l.elem) //线性表不存在时
{
cout<<"该线性表不存在!";
return error;
}
else //线性表存在时
{
free(l.elem); //释放空间线性表
cout<<"该线性表已销毁!";
return ok;
}
}
//判断线性表是否为空
int IsSqListEmpty(SqList &l)
{
if(!l.elem) //线性表不存在时
{
cout<<"该线性表不存在!";
return error;
}
else //线性表存在时
{
if(l.length==0)
{
cout<<"该线性表为空!"<<endl;
return ok;
}
else
{
cout<<"该线性表不为空!"<<endl;
return error;
}
}
}
//判断线性表是否已满
int IsSqListFull(SqList &l)
{
if(!l.elem) //线性表不存在时
{
cout<<"该线性表不存在!";
return error;
}
else //线性表存在时
{
if(l.listsize==maxsize) //判断线性表是否刚好等于分配的存储空间
{
cout<<"该线性表已满!";
return ok;
}
else
{
cout<<"该线性表未满!";
return error;
}
}
}
//求取线性表的数据元素个数
int SqListLength(SqList &l)
{
if(!l.elem) //线性表不存在时
return -1;
else //线性表存在时
cout<<"线性表长度为:"<<l.length<<endl;
return l.length;
}
//定位查找线性表是否第i位的元素
int GetSqListElement(SqList &l,int i,ElemType &e)
{
if(i<1||i>l.length)
{
cout<<"该位置不存于线性表";
return error;
}
e=l.elem[i-1];
cout<<"存在第"<<i<<"位为:"<<e<<endl;
return ok;
}
//定位查找线性表是否存在某元素并返回元素位置
int SqListLocation(SqList &l,ElemType x)
{
if(!l.elem) //线性表不存在
{
cout<<"线性表不存在!"<<endl;
return error;
}
else
{
int i;
for(i=0;i<l.length;i++) //循环查找线性表的元素
{
if(l.elem[i]==x) //存在元素
{
cout<<"存在元素"<<x<<"位置为:"<<i<<endl;
return ok;
}
if(i==l.length-1)
{
cout<<"不存在元素"<<x<<endl;
}
}
}
}
//查找某元素的直接前驱
int SqListPrior(SqList &l, ElemType x)
{
if(l.elem) //线性表存在
{
int i;
for(i=0; i<l.length; i++) //循环查找线性表的元素
{
if(l.elem[i]==x)
{
cout<<"存在元素"<<x<<"位置为:"<<i<<",";
if(i==0)
{
cout<<"该元素是第一位元素,无直接前驱"<<endl;
return error;
}
else
{
cout<<"该元素的直接前驱为:"<<l.elem[i-1]<<endl;
return ok;
}
}
else
{
cout<<"不存在该元素!"<<endl;
return error;
}
}
}
else
{
cout<<"线性表不存在!"<<endl;
}
}
//查找某元素的直接后继
int SqListNext(SqList &l, ElemType x)
{
if(!l.elem) //线性表不存在
{
cout<<"线性表不存在!"<<endl;
return error;
}
else
{
int i;
for(i=0;i<l.length;i++) //循环查找线性表的元素
{
if(l.elem[i]==x)
{
cout<<"存在元素"<<x<<"位置为:"<<i<<",";
if(i==l.length-1)
{
cout<<"该元素是最后一位元素,无直接后继"<<endl;
return error;
}
else
{
cout<<"该元素的直接后继为:"<<l.elem[i+1]<<endl;
return ok;
}
}
else
{
cout<<"不存在该元素!"<<endl;
return error;
}
}
}
}
//向线性表的指定位置第i位前插入一个新元素x
int InsertSqList(SqList &l, int i, ElemType e)
{
ElemType *newbase, *p, *q; //定义三个新指针
if(i<1 || i>l.length+1) //判断指定位置是否合法
{
cout<<"不存在该位置i!"<<endl;
return error;
}
if(l.length >= l.listsize) //判断线性表当前的长度是否以及满了
{
newbase=(ElemType *)realloc(l.elem, (l.listsize+50)*sizeof(ElemType));
if (!newbase) //新线性表是否创建成功
{
cout<<"存储分配失败"<<endl;
return error;
}
l.elem=newbase;
l.listsize+=50;
}
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;
}
//删除线性表指定位置第i位的元素
int DeleteSqList(SqList &l, int i, ElemType &e)
{
if(i<1||i>l.length)
{
return error;
}
else
{
e=l.elem[i-1];
for(int k=i; k<l.length; k++)
{
l.elem[k-1]=l.elem[k];
}
}
--l.length;
return ok;
}
//线性表的元素降序排序
void SortSqList(SqList &l)
{
ElemType e;
for (int i=0; i<l.length-1; i++)
{
for (int j=0; j<l.length-i+1; j++)
{
if (l.elem[j+1] > l.elem[j])
{
e=l.elem[j];
l.elem[j]=l.elem[j+1];
l.elem[j+1]=e;
}
}
}
}
//输出这个线性表
void PrintSqList(SqList &l)
{
if(l.length==0)
{
cout<<"顺序表为空!";
}
int i;
for(i=0; i<l.length; i++)
{
cout<<l.elem[i]<<",";
}
cout<<endl;
}
/*
void PrintSqList(SqList &l)
{
if(l.length==0)
{
cout<<"顺序表为空!";
}
int i;
int *p=l.elem;
for(i=0; i<l.length; i++)
{
cout<<*p<<",";
p++;
}
cout<<endl;
}
*/
源文件:
#include "Header.h"
void main()
{
SqList M;
ElemType e;
InitSqList(M);
IsSqListEmpty(M);
SqListLength(M);
InsertSqList(M, 1, 15);
InsertSqList(M, 1, 9);
InsertSqList(M, 1, 20);
InsertSqList(M, 2, 6);
InsertSqList(M, 4, 17);
InsertSqList(M, 1, 4);
SortSqList(M);
PrintSqList(M);
SqListLength(M);
GetSqListElement(M, 2, e);
SqListLocation(M, 9);
cout<<"删除元素后的线性表为:";
DeleteSqList(M, 3, e);
PrintSqList(M);
SqListLocation(M, 15);
SqListPrior(M, 20);
SqListNext(M, 20);
IsSqListFull(M);
cout<<endl;
IsSqListEmpty(M);
DestroySqList(M);
cout<<endl;
}