数据结构:C语言实现线性表01:顺序表

为了使程序合理,代码分为三个部分

  • 1.初始化部分

  • 2.函数部分

  • 3.测试部分

1.初始化部分

#define LIST_INIT_SIZE 20
typedef int ElemType;
enum Status
{
	success,fatal,fail,range_error
};
typedef struct SqList
{
	ElemType* elem;
	int length;
	int list_size;
}SqList, * SqlistPtr;

2.函数部分

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

//初始化列表
Status list_init(SqlistPtr L)
{
	Status status = success;
	L->elem = (ElemType*)malloc(sizeof(ElemType) * (L->list_size));
	L->length = 0;
	L->list_size = LIST_INIT_SIZE;
	if (L->elem == NULL)
		status = fail;
	return status;
}

//销毁列表
void list_destory(SqlistPtr L)
{
	free(L->elem);
	L->elem = NULL;
	L->length = 0;
	L->list_size = 0;
}

//清空列表
void list_clear(SqlistPtr L)
{
	free(L->elem);
	L->length = 0;
}

//判断列表是否为空
bool is_empty(SqlistPtr L)
{
	return L->length == 0;
}

//返回列表的元素个数
int list_size(SqlistPtr L)
{
	return L->length;
}

//根据索引找值
Status list_retrieve(SqlistPtr L, int pos, ElemType* elem)
{
	Status status = range_error;
	int i = pos - 1; // pos是逻辑位置, i是真实位置
	if (L)
	{
		if (i >= 0 && i < L->length)
		{
			*elem = L->elem[i];
			status = success;
		}
	}
	else
		status = fatal;
	return status;
}

//根据值找索引
Status list_locate(SqlistPtr L, ElemType elem, int *pos)
{
	Status status = range_error;
	if (L)
	{
		int i;
		for (i = 0; i < L->length && L->elem[i] != elem; i++);
		if (i < L->length)
		{
			*pos = i + 1;
			status = success;
		}
	}
	else
		status = fatal;
	return status;
}

//在pos处插入elem
Status list_insert(SqlistPtr L, int pos, ElemType elem)
{
	Status status = range_error;
	int i = pos - 1;
	if ((L && i >= 0 && i <= L->length && L->length < L->list_size))
	{
		for (int j = L->length - 1; j >= i; j--)
		{
			L->elem[j + 1] = L->elem[j];
		}
		L->elem[i] = elem;
		L->length++;
		status = success;
	}
	else
		status = fatal;
	return status;
}

//删除pos处的元素
Status list_delete(SqlistPtr L, int pos) 
{
	Status status = range_error;
	int i = pos - 1;
	if (L && i >= 0 && i < L->length) 
	{
		for (int j = i + 1; j < L->length; j++) 
		{
			L->elem[j - 1] = L->elem[j];
		}
		L->length--;
		status = success;
	}
	else
		status = fail;
	return status;
}

//将现行表中的元素输出
void list_desplay(SqlistPtr L)
{
	printf("\n\n");
	printf("列表中的数有:\n");
	for (int i = 0; i < L->length; i++)
	{
		printf("%d\n", L->elem[i]);
	}
	printf("\n\n");
}

3.测试部分

Status CreateList(SqlistPtr L);      //创建列表
Status ClearList(SqlistPtr L);       //清空列表
void RetrivalRriorNext(SqlistPtr L);   //根据索引找值
void locate(SqlistPtr L);            //根据值找索引
void length(SqlistPtr L);            //打印长度
void PrintScreen();                  //打印界面

int main(int argc, char* argv[])
{
	PrintScreen();
	int a;
	int* prior = new int;
	int* next = new int;
	SqlistPtr L = new SqList;

	while (scanf("%d", &a) == 1)
	{
		switch (a)
		{
			case 1: CreateList(L); PrintScreen(); break;
			case 2: ClearList(L); PrintScreen(); break;
			case 3: RetrivalRriorNext(L); PrintScreen(); break;
			case 4: locate(L); PrintScreen(); break;
			case 5: length(L); PrintScreen(); break;
		}
	}
	return 0;
}

//创建列表
Status CreateList(SqlistPtr L)
{
	Status status;
	int elem;
	list_init(L);
	printf("请输入你要插入的元素:(0为结束)\n");
	while (scanf("%d", &elem) == 1 && elem != 0)
	{
		status = list_insert(L, L->length + 1, elem);
		if (status != success)
		{
			printf("插入失败\n");
			break;
		}
		else
			printf("\n插入成功,请继续:\n");
	}
	list_desplay(L);
	return status;
}

//清空列表
Status ClearList(SqlistPtr L)
{
	Status status = success;
	printf("清空列表。。。\n");
	if (!is_empty(L))
		list_clear(L);
	else
		printf("列表已经是空的了");
	printf("清空完毕!\n");
	return status;
}

//根据索引找值
void RetrivalRriorNext(SqlistPtr L)
{
	Status status;
	int pos;
	ElemType elem;
	printf("输入查询的位置:\n");
	scanf("%d", &pos);
	status = list_retrieve(L, pos, &elem);
	if (status == success)
		printf("%d位置上的元素为%d\n", pos, elem);
	else
		printf("查找失败!");
}

//根据值找索引
void locate(SqlistPtr L)
{
	ElemType elem;
	int pos = 0;
	printf("请输入要查找的元素:\n");
	scanf("%d", &elem);
	list_locate(L, elem, &pos);
	printf("位置是%d\n",pos);
}

//打印长度
void length(SqlistPtr L)
{
	//printf("长度为:%d\n", list_size(L)>=20?list_size(L):0);
	if (L->length > 0 && L->length <= 20)
		printf("长度为:%d\n", list_size(L));
	else
		printf("目前没有列表");
}
//打印界面
void PrintScreen()
{
	printf("\n\n\n");
	printf("*******************************************\n");
	printf("*[1]             建立列表                  *\n");
	printf("*[2]             清除列表                  *\n");
	printf("*[3]             给索引查值                *\n");
	printf("*[4]             给值查索引                *\n");
	printf("*[5]             求列表长度                *\n");
	printf("*******************************************\n");
	printf("请选择测试项目:");
	printf("\n\n\n");
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值