实现顺序表的各种基本运算和整体建表方法

#include<iostream>
using namespace std;
#include<malloc.h>
#define Max 50
typedef char ElemType;
typedef struct{
ElemType a[Max];
int length;
}A;
void InitList(A *&L)
{
L=(A *)malloc(sizeof(A));
L->length=0;
}
bool InsertList(A *&L, ElemType e,int i)
{
int j;
if(i<1 || i>L->length+1)
return false;
i--;
for(j=L->length;j>i;j--)
{
L->a[j]=L->a[j-1];
}
L->a[i]=e;
L->length++;
return true;
}
void DisPlay(A *L)
{
for(int i=0;i<L->length;i++)
{
cout<<L->a[i]<<" ";
}
//cout<<L->a[i]<<" ";
cout<<endl;
}
void length(A *L)
{
cout<<L->length<<endl;
}
void empty1(A *L)
{
if(L->length==0){
cout<<"The list is empty!"<<endl;
}
else
cout<<"The list is not empty!"<<endl;
}
void DisPlayA(A *L,int i)
{
cout<<"The third element is "<<L->a[i-1]<<"."<<endl;
}
void Location(A *L,ElemType e)
{
for(int i=0;i<L->length;i++)
{
if(L->a[i]==e)
cout<<"The location of element 'a' is: "<<i+1<<endl;
}
}
bool Del(A *&L,int i)
{
int j;
if(i<1||i>L->length)
return false;
i--;
for(j=i;j<L->length-1;j++)
L->a[j]=L->a[j+1];
L->length--;
return true;
}
void Des(A *&L)
{
free(L);
}
int main()
{
A *s1;
ElemType e;
InitList(s1);
for(int i=1;i<6;i++)
{
cin>>e;
InsertList(s1,e,i);
}
//InsertList(s1,'a',1);
//InsertList(s1,'b',2);
//InsertList(s1,'c',3);
//InsertList(s1,'d',4);
//InsertList(s1,'e',5);
DisPlay(s1);
empty1(s1);
DisPlayA(s1,3);
Location(s1,'a');
InsertList(s1,'f',4);
DisPlay(s1);
Del(s1,3);
DisPlay(s1);
Des(s1);
}

上一篇:数据结构(线性表)


下一篇:栈的学习-1