顺序表的实现(C语言)

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值