#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <cstring>
using namespace std;
#define OK 1
#define ERROR -1
#define LIST_INIT_SIZE 100
#define LISTSIZE 10
#define true 1
#define flase 0
typedef int ElemType ;
typedef int Status ;
typedef struct list_{
ElemType * elem ;
int length ; //当前长度
int listsize ; //线性表的容量
}sqList;
Status InitList(sqList &L)
{
/* creat a empty sqlist */
/*创建了一个空线性表*/
L.elem = (ElemType *)malloc(sizeof(ElemType)*LIST_INIT_SIZE);
if(L.elem!=NULL)
cout<<"creat a empty sqlist "<<endl;
L.length = 0;
L.listsize =LIST_INIT_SIZE ;
return OK;
}
Status DestroyList(sqList &L)
{
/*销毁一个线性表 L*/
/*初始条件 : 线性表存在*/
if(L.elem ==NULL)
exit(ERROR);
free(L.elem) ;
L.elem =NULL ;
L.length = 0 ;
L.listsize = 0;
return OK ;
}
Status clearList(sqList &L)
{
if(L.elem==NULL)
exit(ERROR);
memset(&L,0,sizeof(sqList));
return OK;
}
Status ListLength(sqList &L)
{
if(L.elem==NULL)
exit(ERROR);
return L.length ;
}
void List_show(sqList &L)
{
if(L.elem==NULL)
exit(ERROR);
int i ;
for(i =0 ;i<L.length ;i++)
{
cout<<" "<<L.elem[i]<<" ";
}
}
Status write_List(sqList &L,int e)
{
int i ;
if(L.elem==NULL)
exit(ERROR) ;
if(L.listsize<e)
{
cout<<"容量不够"<<endl;
exit(ERROR);
}
for(i=0 ;i<e ;i++)
{
cin>>L.elem[i];
L.length++;
}
return OK ;
}
Status Getelem(sqList &L,int e)
{
if(L.elem==NULL)
exit(ERROR);
if(e<=0 || e>L.length)
{
cout<<"failed"<<endl;
exit(ERROR);
}
return L.elem[e-1];
}
Status ListEmpty(sqList &L)
{
if(L.elem==NULL)
exit(ERROR);
if(L.length ==0)
return false ;
}
Status ListInsert(sqList &L ,int i ,int e)
{
if(L.elem == NULL)
exit(ERROR) ;
if(i<=0 ||i>L.length)
exit(ERROR);
int *p = &L.elem[L.length-1];
int *q = &L.elem[i-1];
while(p>=q)
{
*(p+1) = *(p) ;
p--;
}
*q = e ;
L.length++;
return OK ;
}
void unionList(sqList &M ,sqList &N)
{
/*将所有的在线性表 M 中但不在 N 中的数据元素插入到M 中*/
int lenM = M.length ;
int lenN = N.length ;
int i ;
// M.length+=N.length ;
for(i = 0 ;i<lenM ;i++)
{
if(Getelem(M,i+1) )
}
}
int main()
{
int e ;
int m ;
int i ;
sqList a ;
InitList(a);
cout<<"输入个数:\t"<<endl;
scanf("%d",&e);
write_List(a,e);
cout<<"获得第几个:"<<endl;
cin>>m ;
cout<< Getelem(a,m)<<endl;
cout<<"在第几个位置插入几 :"<<endl;
cin>> i >> e;
ListInsert(a ,i ,e) ;
List_show(a);
return 0;
}
标签:线性表,--,elem,int,length,exit,sqList,ERROR,数据结构 From: https://blog.51cto.com/u_15970235/6064107