顺序表的实现

头文件 

#ifndef _StaticList_
#define _StaticList_

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

//默认链表长度
#define DefaultListSize 50

/*
静态链表的代码实现
*/

typedef struct _SList
{
	//链表
	int pBuffer[DefaultListSize];

	//当前使用大小
	int nSize;
}SList,*PSList;

//操作类型
enum _OperatorType
{
	//退出系统
	ExitSystem = 0,

	//添加数据
	Add_Data,

	//删除数据
	Delete_Data,

	//修改数据
	Modify_Data,

	//查找数据
	Search_Data
};

//显示菜单
void ShowMenu();

//添加数据
void AddData(PSList);

//删除数据
void DeleteData(PSList);

//修改数据
void ModifyData(PSList);

//查找数据
void SearchData(PSList);

//输出链表
void ShowList(PSList);

//创建静态链表
PSList CreateList();

//释放静态链表
void ReleaseList(PSList);

//清空链表
void ClearList(PSList);

#endif

实现文件

#define _CRT_SECURE_NO_WARNINGS
#include "StaticList.h"

int main(int argc, char* argv[])
{
	//静态链表
	PSList pstList = NULL;
	int nIndex = NULL;

	//创建链表
	pstList = CreateList();
	if (pstList == NULL)
	{
		puts("创建链表失败");
		system("pause");
		return -1;
	}

	//死循环
	do 
	{
		//显示菜单
		ShowMenu();
		scanf("%d", &nIndex);
		
		//退出系统
		if (nIndex == ExitSystem)
		{
			break;
		}

		switch (nIndex)
		{
		case Add_Data:
			AddData(pstList);
			break;
		case Delete_Data:
			DeleteData(pstList);
			break;
		case Modify_Data:
			ModifyData(pstList);
			break;
		case Search_Data:
			SearchData(pstList);
			break;
		default:
			puts("输入无效");
			break;
		}
		ShowList(pstList);
	} while (1);
	ReleaseList(pstList);
	return 0;
}

void ShowMenu()
{
	puts("0.退出程序");
	puts("1.添加数据");
	puts("2.删除数据");
	puts("3.修改数据");
	puts("4.查找数据");
}

void AddData(PSList pstList)
{
	int nIndex = NULL;
	int nData = NULL;
	int nCount = NULL;

	//链表内存错误
	if (pstList == NULL)
	{
		puts("链表内存错误");
		system("pause");
		exit(-1);
	}

	//判断链表是不是满了
	if (pstList->nSize >= DefaultListSize)
	{
		puts("链表数据已满");
		return;
	}

	puts("插入数据:");
	scanf("%d", &nData);

	//如果当前为空链表的情况下
	if (pstList->nSize == NULL)
	{
		//插入数据
		pstList->pBuffer[pstList->nSize] = nData;

		//计数加一
		pstList->nSize++;

		return;
	}

	puts("插入位置:");
	scanf("%d", &nIndex);

	//位置错误
	if (nIndex > pstList->nSize || nIndex < NULL)
	{
		puts("位置错误");
		return;
	}

	//获取移动次数
	nCount = pstList->nSize;

	while (nIndex <= --nCount)
	{
		//数据右移
		pstList->pBuffer[nCount + 1] = pstList->pBuffer[nCount];
	}

	//将需要的数据插入
	pstList->pBuffer[nIndex] = nData;

	//数量加一
	pstList->nSize++;
}

void DeleteData(PSList pstList)
{
	int nIndex = NULL;

	//链表内存错误
	if (pstList == NULL)
	{
		puts("链表内存错误");
		system("pause");
		exit(-1);
	}

	//为空链表
	if (pstList->nSize == NULL)
	{
		puts("链表为空,无法删除");
		return;
	}

	puts("删除位置:");
	scanf("%d", &nIndex);

	//不能小于0和不能大于数据的数量
	if (nIndex < NULL || nIndex > pstList->nSize)
	{
		puts("位置错误");
		return;
	}

	while (nIndex <= pstList->nSize)
	{
		//数据左移
		pstList->pBuffer[nIndex] = pstList->pBuffer[nIndex + 1];
		nIndex++;
	}

	//将后面的数据清楚掉
	pstList->pBuffer[pstList->nSize - 1] = 0;

	//计数减一
	pstList->nSize--;
}

void ModifyData(PSList pstList)
{
	int nIndex = NULL;
	int nData = NULL;

	//链表内存错误
	if (pstList == NULL)
	{
		puts("链表内存错误");
		system("pause");
		exit(-1);
	}

	if (pstList->nSize == NULL)
	{
		puts("链表为空,无法修改");
		return;
	}

	puts("修改位置");
	scanf("%d", &nIndex);

	if (nIndex<NULL || nIndex>pstList->nSize - 1)
	{
		puts("位置错误");
		return;
	}

	puts("新的数据");
	scanf("%d", &nData);

	//数据覆盖
	pstList->pBuffer[nIndex] = nData;
}

void SearchData(PSList pstList)
{
	int nData = NULL;
	int nIndex = NULL;

	//链表内存错误
	if (pstList == NULL)
	{
		puts("链表内存错误");
		system("pause");
		exit(-1);
	}

	if (pstList->nSize == NULL)
	{
		puts("链表为空,无法查找");
		return;
	}

	puts("输入数据");
	scanf("%d", &nData);

	printf("数据索引: \n[");
	for (nIndex = 0; nIndex <= pstList->nSize - 1; nIndex++)
	{
		if (pstList->pBuffer[nIndex] == nData)
		{
			printf("%2d", nIndex);
		}
	}
	printf("]\n");

}

void ShowList(PSList pstList)
{
	int nIndex = NULL;

	//链表内存错误
	if (pstList == NULL)
	{
		puts("链表内存错误");
		system("pause");
		exit(-1);
	}

	//循环输出
	printf("链表数据: \n[");
	while (nIndex < pstList->nSize)
	{
		printf("%2d", pstList->pBuffer[nIndex++]);
	}
	printf("]\n");
}

PSList CreateList()
{
	PSList pstList = NULL;

	//申请内存空间
	pstList = (PSList)malloc(sizeof(SList) * 1);
	if (pstList == NULL)
	{
		return NULL;
	}

	//清空链表
	ClearList(pstList);

	return pstList;
}

void ReleaseList(PSList pstList)
{
	if (pstList == NULL)
	{
		return;
	}

	//释放内存
	free(pstList);
}

void ClearList(PSList pstList)
{
	if (pstList == NULL)
	{
		return;
	}

	//清空内存
	memset(pstList, 0, sizeof(SList));
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值