自己写的线性结构的删除与插入

本文介绍了一种使用C++实现顺序表的基本操作方法,包括初始化、获取长度、判断是否为空、插入元素及删除元素等功能,并提供了完整的代码示例。

#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);

 
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值