C语言的线性表实现

在学习数据结构的时候老师大多给出的是伪代码,本文实现了数据结构中线性表的c语言表达,一方面以供学习者参考,另一方面作为自己学习的记录。使用的IDE是VS2019,作为c语言的初学者,许多地方做的不够完善还请海涵。在代码过程中遇到的问题与解答已附在代码中。

Part1

结构体的定义:

typedef struct {
	char *name[5];
	int price[5];
}NumberList;


typedef struct {
	NumberList *lists;
	int length;
}Lineartable;

主函数部分:(由于我只是在测试各个函数的运行情况,主函数部分大家可自行修改)

int main()
{
	Lineartable* sqlist = (Lineartable*)malloc(sizeof(Lineartable));
	NumberList* nlist = (NumberList*)malloc(sizeof(NumberList));
	int i, result;
	char *check;
	char* books[5] = { "The great Gastby", "Harry Potter", "Sea and the old", "Education of love", "Island" };
	int money[5] = { 100, 200, 138, 36, 60 };
	if (sqlist && nlist)  //If "if" sentence is deleted, the computer doesn't konw if  function malloc offers enough memory for sqlist. A warning will be existed. 
	{
		for (i = 0; i <= 4; i++) {
			nlist->name[i] = books[i];
			nlist->price[i] = money[i];
		}
	sqlist->lists = nlist;
	sqlist->length = 5;
	printf("The book is: %s\n", nlist->name[0]);  //Q1: How to print a string array? A1: Just print the point of array with "%s".
	printf("%d book in library now.\n", sqlist->length);
	ShowBooks(sqlist);
    //LocateElem(sqlist);
	//ListInsert(sqlist);
	printf("The book is: %s\n", sqlist->lists->name[3]);
	ShowBooks(sqlist);
	check = GetElem(nlist, 3);  //Q2: Why isn't check a pointer?   A2: You forget to define check as a pointer.
	printf("%s is at the position.\n", check);
	//Clearlist(sqlist);
	printf("%d book in library now.\n", sqlist->length);   //annotation for lines:ctrl + k, ctrl + c
	DeleteList(sqlist);
	printf("%d book in library now.\n", sqlist->length);
	ShowBooks(sqlist);
	result = IsEmpty(sqlist);
	if (result)
	{
		printf("Lineartable is empty now.");
	}
	else
	{
		printf("Lineartable is not empty.");
	} 
	}	
	return 0;
}

子函数部分:

void DestoryList(Lineartable *sqlist, NumberList* lists) {
	if (sqlist->lists->name && sqlist->lists->price)
	{
		free(sqlist);
		free(lists);
	}
}


void Clearlist(Lineartable *sqlist) {
	int i;
	for (i = 0; i <= 4; i++)
	{
	sqlist->lists->name[i] = "";
	sqlist->lists->price[i] = 0;
	sqlist->length = 0;
	}
}


int Getlenth(Lineartable* sqlist) {
	return sqlist->length;
}


int IsEmpty(Lineartable* sqlist) {
	if (sqlist->length == 0) 
	{
		return 1;
	}
	else
	{
		return 0;
	}
}


char* GetElem(NumberList* lists, int i){
	return lists->name[i];
}


void LocateElem(Lineartable* sqlist){
	char bn[50];
	int i;
	printf("Input the book you are looking for:");
	gets_s(bn, 50);
	const char* pointer1 = &bn;  //Pay attention here: function strcmp only accept its parameters in form of const type *, in this case, bn and name should be transformed
	const char* pointer2 = &(sqlist->lists->name);
	for(i=0;i<=sqlist->length;i++)
	{
		if (strcmp(pointer1, pointer2) == 0)
		{
			printf("The book is at the position %d", i);
		}
	}
}


void ListInsert(Lineartable* sqlist) {  //Q4:Why can't I change the Lineartable forever even with a "static"? A4: You should place "static" before book[30]
	int i, j;
	static char book[30];
	printf("Input the position and book name you would like to insert: ");
	scanf_s("%d\n", &i);
	printf("Input the book name:\n");
	setvbuf(stdin, NULL, _IOFBF, 512);
	gets_s(book, 30);  //Q3: Why this line shows before the last one?  A4:Becasue of the exist of buffer.
	char* pointer = (char*)book;
	if (i < 1 || i > sqlist->length)
	{
		printf("Fail to insert!");
	}
	else
	{
		for (j = sqlist->length - 1; j >= i - 1; j--)
		{
			sqlist->lists->name[j + 1] = sqlist->lists->name[j];
			sqlist->lists->price[j + 1] = sqlist->lists->price[j];
		}
		sqlist->lists->name[i] = pointer;
	}
}


void ShowBooks(Lineartable* sqlist)
{
	int i;
	for (i = 0; i <= 4; i++) {
		printf("Book %d is: %s\n", i+1, sqlist->lists->name[i]);
	}
	
}


void DeleteList(Lineartable* sqlist) 
{
	int i, j;
	char* pointer = "";
	printf("Input the position you want to delete:");
	scanf_s("%d", &i);
	if (i < 1 || i > sqlist->length)
	{
		printf("Fail to delete!");
	}
	else
	{
		for (j = i; j < sqlist->length; j++)
		{
			sqlist->lists->name[j - 1] = sqlist->lists->name[j];
			sqlist->lists->price[j - 1] = sqlist->lists->price[j];
		}
		sqlist->length -= 1;
		sqlist->lists->name[4] = pointer;
		sqlist->lists->price[4] = 0;
	}

}

Part2

反思与改进:
1.对于c语言的内存机制还不够清晰,初次写长段c的代码报错很多很多。
2.尝试在多个.c文件下进行后续数据结构的表达。
3.接口与出口做的不够好,在未来尝试不做任何返回,只接受最简单基础的单一变量,在函数内部完成各项功能。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值