#include<stdio.h>
#include<iostream>
#define MAX_SIZE 100
using namespace std;
typedef int Elementtype;
typedef struct
{
int date[MAX_SIZE];
Elementtype x;
int Length;
}List;
//初始化顺序表
void InitialList(List *L)
{
//List L=new List;
if(L==NULL)
{
return;
}
L->Length=-1;
}
//求顺序表的长度
int Length_List(List *L)
{
return L->Length+1;
}
//查找指定位置的元素
int Search(List *L,int x)
{
if(L->Length<0||L->Length>MAX_SIZE)
return 0;
return L->date[x-1];
}
//插入操作
bool InserList(List *L,int p,Elementtype x)
{
if(L->Length>=MAX_SIZE-1)
{
printf("表满");
return false;
}
if(p<1||p>L->Length+2)
{
printf("位置不合法");
return false;
}
for(int i=L->Length;i>=p-1;i--)
{
L->date[i+1]=L->date[i];
}
L->date[p-1]=x;
L->Length++;
return true;
}
//删除操作
bool Delete_List(List *L,int p)
{
if(L==NULL)
return false;
for(int i=p-1;i<=L->Length;i++)
{
L->date[i]=L->date[i+1];
}
L->Length--;
return true;
}
//清空顺序表
bool Clear_List(List *L)
{
L->Length=0;
return true;
}
int ShowList(List *L)
{
if(L==NULL)
{
return 0;
}
for(int i=0;i<=L->Length;i++)
{
cout<<L->date[i]<<" "<<std::endl;
}
return 0;
}
int main()
{
int b;
List *LL=new List;
//Ptr L;
InitialList(LL);
ShowList(LL);
InserList(LL,1,1);
InserList(LL,2,2);
InserList(LL,3,3);
InserList(LL,4,4);
ShowList(LL);
b=Length_List(LL);
cout<<"长度为"<<b<<std::endl;
Delete_List(LL,3);
ShowList(LL);
return 0;
}
共同进步,永远向前!