============algo2-1.cpp============
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 6
typedef struct{
char data[MaxSize];
int length;
}SqList;
void InitList(SqList *&L){
L=(SqList *)malloc(sizeof(SqList));
L->length=0;
}
void DestroyList(SqList *&L){
free(L);
}
bool ListEmpty(SqList *&L){
return (L->length==0);
}
int ListLength(SqList *&L){
return (L->length);
}
void DispList(SqList *&L){
int i=0;
for(i;i<L->length;i++)
printf("%c ",L->data[i]);
printf("\n");
}
bool GetChar(SqList *&L,int i,char &e){
if(i<1||i>L->length)
return false;
e=L->data[i-1];
return true;
}
int LocateChar(SqList *&L,char e){
int i=0;
while(i<L->length&&L->data[i]!=e)
i++;
if(i>=L->length)
return 0;
else
return i+1;
}
bool ListInsert(SqList *&L,int i,char e){
int j;
if(i<1||i>L->length+1)
return false;
i--;
for(j=L->length;j>i;j--)
L->data[j]=L->data[j-1];
L->data[i]=e;
L->length++;
return true;
}
bool ListDelete(SqList *&L,int i){
int j;
if(i<1||i>L->length)
return false;
i--;
for(j=i;j<L->length-1;j++)
L->data[j]=L->data[j+1];
L->length--;
return true;
}
void ListTialIns(SqList *&L,char e){
L->data[L->length]=e;
L->length++;
}
==========exp2-1============
#include "algo2-1.cpp"
void main(){
SqList *L;
char e;
InitList(L);
ListTialIns(L,'a');
ListTialIns(L,'b');
ListTialIns(L,'c');
ListTialIns(L,'d');
ListTialIns(L,'e');
DispList(L);
printf("The list-length is:%d\n",ListLength(L));
if(ListEmpty(L))
printf("The list is empty!\n");
else
printf("The list is not empty!\n");
if(GetChar(L,3,e))
printf("The third element of the list is:%c\n",e);
else
printf("The element is not found!\n");
printf("The element a's location is:%d\n",LocateChar(L,'a'));
ListInsert(L,4,'f');
DispList(L);
ListDelete(L,3);
DispList(L);
DestroyList(L);
}