#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char bookid[10];
char bookname[20];
int state;
}data;
typedef struct {
data* booklist;
int length;
int capacity;
int expansion;
}Sqlist;
// The function describe the initialize Sequential table,witch contain
//itself and its booklist pointer allocates memory using the malloc function
// And the initialization of other properties
Sqlist *iniSize(){ //ok
Sqlist* L=(Sqlist*)malloc(sizeof(Sqlist));
L->capacity=100;
L->booklist=(data*)malloc(sizeof(data)*L->capacity);
L->expansion=0;
L->length=0;
return L;
};
// The function desgin
void reIniSize(Sqlist *L){
free(L->booklist);
L->capacity=100;
L->booklist=(data*)malloc(sizeof(data)*L->capacity);
L->expansion=0;
L->length=0;
}
// The function describes putting the book into the Sequential table,
//which includes the length dilatation of the Sequential, adding a book, increasing the expansion, and the actual length.
void addbook(data book,Sqlist* list){ //ok
if(list->length==list->capacity) {
list->booklist=(data*)realloc(list->booklist,sizeof(data) * list->capacity * 2);
list->capacity *= 2;
}
list->booklist[list->length]=book;
list->expansion+=2;
list->length++;
};
//The function is used to get the size of the Sequential table
int size(Sqlist* L){ //ok
return L->length;
};
data getbook(Sqlist* L,int index){ //ok
if(index>=L->length||index<0){
printf("Index out of bounds\n");
exit(1);
}
return L->booklist[index];
};
//The function's purpose is to retrieve the forward Index of an element in a sequential table
int getIndex(Sqlist* list,data *book){
int i;
for(i=0;i<list->length;i++){
if(strcmp(list->booklist[i].bookid,book->bookid)==0&&strcmp(list->booklist[i].bookname,book->bookname)==0){
return i;
}
}
return 0;
}
//The function's purpose is to retrieve the posterior Index of an element in a sequential table
int getReIndex(Sqlist* list,data *book){
int i;
for(i=0;i<list->length;i++){
if(strcmp(list->booklist[i].bookid,book->bookid)==0&&strcmp(list->booklist[i].bookname,book->bookname)==0){
return i;
}
}
return 0;
}
void clearList(Sqlist* L){ //
reIniSize(L);
};
//The function's purpose is to delete an element of the sequential table by index
void deleteListByIndex(Sqlist *list,int index){ //ok
if(index>list->length||index<0){
printf("Index out of bounds\n");
}else {
if(list->length-1==index){
list->length--;
}else{
int i;
for(i=index;i<=list->length-1;i++){
list->booklist[i]=list->booklist[i+1];
}
list->length--;
}
}
};
//The functi's purpose is to insert an element into a sequential table
void insertBook(Sqlist* list,data book,int index){ //ok
if(list->length<index||index<0){
printf("This index exceed length of Sequential table !\n");
}else {
if(list->length==0&&index>0){
list->booklist[0]=book;
}else if(list->length==index){
list->booklist[list->length]=book;
}else {
int i;
for(i=list->length-1;i>=index;i--){
list->booklist[i+1]=list->booklist[i];
}
list->booklist[index]=book;
}
list->length++;
}
}
//The
void printAllBook(Sqlist *list){ //ok
int i;
if(list->length==0){
printf("this sequence list is Empty !");
}else{
for(i=0;i<list->length;i++){
printf("bookid:%s,bookname:%s\n",list->booklist[i].bookid,list->booklist[i].bookname);
}
}
}
int isEmpty(Sqlist* L){ //ok
return L->length==0?1:0;
};
void freeList(Sqlist* L){ //L will be dangling pointer ok
free(L->booklist);
free(L);
};
int main(){
Sqlist* L=iniSize();
data book={"102914","wadgaskljdg",1};
data book1={"102924","wadgasklj",1};
data book2={"102934","wadgaskdg",1};
addbook(book,L);
addbook(book1,L);
addbook(book2,L);
deleteListByIndex(L,0);
printAllBook(L);
system("pause");
return 0;
}
顺序表的实现(C语言)
于 2023-09-19 16:58:00 首次发布