好友
阅读权限10
听众
最后登录1970-1-1
|
天地玄黄
发表于 2020-11-28 11:09
顺序表程序:list.h
typedef char EleType;
typedef struct {
EleType *buf;
unsigned int n;
unsigned int max;
}List;
List * CreateList(int);
void DestroyList(List *);
void ClearList(List *);
int ListAppend(List *,EleType);
int ListInsert(List *,int ,EleType);
int ListDelete(List *,int);
int GetElement(List *,int ,EleType *);
int IsFull(List *);
int IsEmpty(List *);
int TraverseList(List *,int (*)(EleType *));
List * CreateList(int n){
List *list;
list=(List *)malloc(sizeof(List));
if(!list) return 0;
list->buf=(EleType *)malloc(n*sizeof(List));
list->n=0;
list->max=n;
return list;
}
void DestroyList(List *list){
ClearList(list);
free(list->buf);
free(list);
}
void ClearList(List *list){
list->n=0;
}
int ListAppend(List *list,EleType data){
if(IsFull(list)) return 0;
list->buf[list->n++]=data;
return 1;
}
int ListInsert(List *list,int pos,EleType data){
int i;
if(IsFull(list) || pos<1 || pos>list->n+1) return 0;
pos-=1;
for(i=list->n;i>=pos;i--)
list->buf[i+1]=list->buf[i];
list->buf[i+1]=data;
list->n++;
return 1;
}
int ListDelete(List *list,int pos){
int i;
if(IsEmpty(list) || pos<1 || pos>list->n) return 0;
pos-=1;
for(i=pos;i<list->n;i++)
list->buf[i]=list->buf[i+1];
list->n--;
}
int GetElement(List *list,int pos,EleType *data){
if(IsEmpty(list) || pos<1 || pos>list->n) return 0;
(*data)=list->buf[pos-1];
return 1;
}
int IsFull(List *list){
if(list->n==list->max) return 1;
return 0;
}
int IsEmpty(List *list){
if(list->n == 0) return 1;
return 0;
}
int TraverseList(List *list,int (*f)(EleType *data)){
int i;
for(i=0;i<list->n;i++)
if(!f(&(list->buf[i])))
return i+1;
return 0;
}
测试程序: m.c
#include"list.h"
#define CREATELIST 0
#define LISTAPPEND 1
#define LISTINSERT 2
#define LISTDELETE 3
#define GETELEMENT 4
#define CLEARLIST 5
#define titleTest printf("yours:");
#define titleSyst printf("\n syst:");
char *strArr[]={
"----------CreateList TEST----------",
"----------ListAppend TEST----------",
"----------ListInsert TEST----------",
"----------ListDelete TEST----------",
"----------GetElement TEST----------",
"----------ClearList TEST----------",
};
void ShowResult(List *list,int pos,char *answer);
int putE(char *data);
main(){
int n;
List *list;
char ch;
char *arr="perfect world";
char chArr[]={0,0,0,0,0};
list = CreateList(30);
puts(strArr[CREATELIST]);
if(!list){
puts("CreateList error!");
return ;
}else
puts("CreateList Success!");
puts(""); getch();
for(n=0;n<13;n++) ListAppend(list,arr[n]);
ShowResult(list,LISTAPPEND,"perfect world");
ListInsert(list,1,'I');
ListInsert(list,2,' ');
ListInsert(list,16,'!');
ListInsert(list,3,' ');
ListInsert(list,3,'a');
ListInsert(list,2,'n');
n=ListInsert(list,0,'0');
n+=ListInsert(list,-1,'0');
n+=ListInsert(list,21,'0');
ShowResult(list,LISTINSERT,"In a perfect world!");
if(n) puts("ListInsert error!");
GetElement(list,1,&ch); chArr[0]=ch;
GetElement(list,19,&ch); chArr[1]=ch;
n=GetElement(list,0,&ch);
n+=GetElement(list,-1,&ch);
n+=GetElement(list,20,&ch);
puts(strArr[GETELEMENT]);
titleTest printf("%s",chArr);
titleSyst printf("I!");
puts(""); getch();
if(n) puts("GetElement error!");
ListDelete(list,11);
ListDelete(list,5);
ListDelete(list,10);
ListDelete(list,6);
ListDelete(list,8);
ListDelete(list,5);
ListDelete(list,13);
ListDelete(list,6);
ListDelete(list,5);
n=ListDelete(list,0);
n+=ListDelete(list,-1);
n+=ListDelete(list,11);
ShowResult(list,LISTDELETE,"In a world");
if(n) puts("ListDelete error!");
ClearList(list);
ShowResult(list,CLEARLIST,"");
}
void ShowResult(List *list,int pos,char *answer){
puts(strArr[pos]);
titleTest TraverseList(list,putE);
titleSyst printf("%s",answer);
puts(""); getch();
}
int putE(char *data){
printf("%c",*data);
return 1;
}
|
|