数据结构的线性表没错误但运行不了???
# include<iostream>
using namespace std;
typedef class List
{
public:
char date[5];
List*p;
int length;
}SqList;
SqList *L;
void CreateList(SqList*L,char s[])//初始化顺序表
{
int i;
L=(SqList*)malloc(sizeof(SqList));
L->length=0;
for(i=0;i<5;++i)
{
L->date[i]=s[i];
L->length++;
}
}
void DispList(SqList*L)//输出顺序表L
{
cout<<"Please output"<<" "<<L->length<<" "<<"numbers";
for(int i=0;i<L->length;++i)
cout<<"output the "<<i<<" "<<"number"<<L->date[i];
}
void ListLength(SqList*L)//输出顺序表L的长度
{
cout<<"The length of the List is "<<L->length;
}
bool ListEmpty(SqList*L)//判断顺序表L是否为空
{
return(L->length==0);
}
char GetList(SqList* L,int k)//输出顺序表的第3个元素
{ char e;
if(k<1||k>L->length)
cout<<"It is wrong!!";
else
e=L->date[k-1];
return e;
}
int LocateElem(SqList*L,char e)//输出元素e的逻辑位置
{
int i=0;
while(i<L->length&&L->date[i]!=e)
i++;
if(i>=L->length)
return 0;
else
return i+1;
}
bool ListInsert(SqList*&L,int i,char e)//在第4个元素位置上插入元素f
{
int j;
if(i<1||i>L->length+1)
cout<<"It is wrong!!";
else
i--;
for(j=L->length;j>i;j--)
L->date[j]=L->date[j-1];
L->date[i]=e;
L->length++;
return true;
}
bool ListDelete(SqList*&L,int i)//删除L的第3个元素
{
int j;
if(i<1||i>L->length)
return false;
i--;//将顺序表逻辑序号转化为物理序号
for(j=i;j<L->length-1;j++)
L->date[j]=L->date[j+1];
L->length--;
return true;
}
void main()
{
char s[5]={'a','b','c','d','e'};
int q=3;
char e1='a';
char e2='f';
int j=4;
CreateList(L,s);
DispList(L);
ListLength(L);
ListEmpty(L);
GetList(L,3);
LocateElem(L,e1);
ListInsert(L,j,e2);
DispList(L);
ListDelete(L,q);
DispList(L);
free(L);//释放L
}