#include "stdafx.h"
#include "iostream.h"
const maxlen=5;
const nullelem=0;
typedef int elemtype;
struct seqlisttype
{
elemtype data[maxlen+1];
int last;
};
void initlist(seqlisttype &l)
{
l.last=0;
}
int length(seqlisttype l)
{
return l.last;
}
int isempty(seqlisttype l)
{
return (l.last==0);
}
int insert(seqlisttype &l,int i,elemtype x)
{
if ((i<1) || (i>l.last+1))
{
cout<<"位置不正确";
return (0);
}
if (maxlen==l.last)
{
cout<<"数组已经满了";
return(0);
}
for (int j=l.last;j>=i;j--)
{
l.data[j+1]=l.data[j];
}
l.data[i]=x;
l.last++;
return(1);
}
int deletei(seqlisttype &l,int i)
{
if ((i<1) || (i>l.last+1))
{
cout<<"位置不正确";
return (0);
}
if (isempty(l))
{
cout<<"数组已经清空了";
return(0);
}
for (int j=i;j<l.last;j++)
{
l.data[j]=l.data[j+1];
}
l.last--;
}
int main(int argc, char* argv[])
{
seqlisttype k;
initlist(k);
for (int i=0;i<=4;i++)
insert(k,i+1,i);
deletei(k,3);
}
本文介绍了一种使用C++实现顺序表的基本操作方法,包括初始化、获取长度、判断是否为空、插入元素及删除元素等功能,并提供了完整的代码示例。

被折叠的 条评论
为什么被折叠?



