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

    技术2022-05-11  36

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

     } 


    最新回复(0)