顺序表(静态版本)-----动态版本的基础

今天,我带来顺序表的静态版本。





顺序表(静态版本)的三个文档

SeqList.h--------头文件的引用和函数的声明
SeqList.c--------函数的定义
test.c--------顺序表的检验



初始化函数

//初始化函数
void InitSeqList(SL* ps)
{
	assert(ps != NULL);
	memset(ps->data,0,MAX * sizeof(SeqListType));
	ps->size = 0;
}


打印函数

//打印函数
void Print(SL* ps)
{
	int i = 0;
	assert(ps != NULL);
	for(i = 0; i < ps->size; i++)
	{
		printf("%d ",ps->data[i]);
	}
	printf("\n");
}


查找函数

//查找函数
int Find(SL* ps,SeqListType x,int pos)
{
	int i = 0;
	assert(ps != NULL);
	assert(pos >= 0);
	assert(pos < ps->size);
	for(i = pos; i < ps->size; i++)
	{
		if(ps->data[i] == x)
		{
			return i;
		}
	}
	return -1;
}

中间插入数据函数和中间删除数据函数

//中间插入数据的函数
void SeqListInsert(SL* ps,int pos,SeqListType x)
{
	int end = ps->size - 1;
	assert(ps != NULL);
	assert(pos >= 0);
	assert(pos <= ps->size);
	if(ps->size == MAX)
	{
		printf("内存已满,存入数据失败\n");
		return;
	}
	while(pos <= end)
	{
		ps->data[end + 1] = ps->data[end];
		end--;
	}
	ps->data[pos] = x;
	ps->size++;
}

//中间删除数据函数
void SeqListErase(SL* ps,int pos)
{
	int begin = pos + 1;
	assert(ps != NULL);
	assert(pos >= 0);
	assert(pos < ps->size);
	while(begin < ps->size)
	{
		ps->data[begin - 1] = ps->data[begin];
		begin++;
	}
	ps->size--;
}


头插函数和尾插函数

//头插函数
void SLPushFront(SL* ps,SeqListType x)
{
	assert(ps != NULL);
	SeqListInsert(ps,0,x);
}

//头删函数
void SLPopFront(SL* ps)
{
	assert(ps != NULL);
	SeqListErase(ps,0);
}


尾插和尾删函数

//尾插函数
void SLPushBack(SL* ps,SeqListType x)
{
	assert(ps != NULL);
	SeqListInsert(ps,ps->size,x);
}

//尾删函数
void SLPopBack(SL* ps)
{
	assert(ps != NULL);
	SeqListErase(ps,ps->size-1);
}


函数的声明和结构体的定义

typedef int SeqListType;
typedef struct SeqList
{
	SeqListType data[MAX];
	int size;
}SL;

//初始化函数
void InitSeqList(SL* ps);

//打印函数
void Print(SL* ps);

//查找函数
int Find(SL* ps,int pos,SeqListType x);

//中间插入数据的函数
void SeqListInsert(SL* ps,int pos,SeqListType x);

//中间删除数据函数
void SeqListErase(SL* ps,int pos);

//头插函数
void SLPushFront(SL* ps,SeqListType x);

//头删函数
void SLPopFront(SL* ps);

//尾插函数
void SLPushBack(SL* ps,SeqListType x);

//尾删函数
void SLPopBack(SL* ps);


菜单和主函数

#include "SeqList.h"

void menu()
{
	printf("***************************************************\n");
	printf("**** 1.尾插                     2.尾删         ****\n");
	printf("**** 3.头插                     4.头删         ****\n");
	printf("**** 5.中间插入数据             6.中间删除数据 ****\n");
	printf("**** 7.查找数据                 8.打印数据     ****\n");
	printf("**** 0.退出                                    ****\n");
	printf("***************************************************\n");
}
int main()
{
	int input = 0;
	SeqListType number = 0;
	int address = 0;
	int location = 0;
	SL SeqList;
	InitSeqList(&SeqList);
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d",&input);
		switch(input)
		{
		case 1:
			printf("请输入你要尾插的数据:>");
			scanf("%d",&number);
			SLPushBack(&SeqList,number);
			break;
		case 2:
			SLPopBack(&SeqList);
			printf("尾删成功\n");
			break;
		case 3:
			printf("请输入你要头插的数据:>");
			scanf("%d",&number);
			SLPushFront(&SeqList,number);
			break;
		case 4:
			SLPopFront(&SeqList);
			break;
		case 5:
			printf("请输入你要在中间插入的数据:>");
			scanf("%d",&number);
			printf("请输入你要插入的位置:>");
			scanf("%d",&address);
			SeqListInsert(&SeqList,address-1,number);
			break;
		case 6:
			printf("请输入你要删除的的数据的位置:>");
			scanf("%d",&address);
			SeqListErase(&SeqList,address-1);
		    break;
		case 7:
			printf("请输入你要查找的数据:>");
			scanf("%d",&number);
			printf("请输入你要从哪个位置开始查找:>");
			scanf("%d",&address);
			location = Find(&SeqList,number,address);
			if(location == -1)
			{
				printf("找不到\n");
			}
			else
			{
				printf("该数字的位置是%d\n",location+1);
			}
			break;
		case 8:
			Print(&SeqList);
			break;
		case 0:
		    printf("退出顺序表\n");
			break;
		default:
			printf("选择错误,请重新选择\n");
			break;
		}
	}while(input);
}


SeqList.h文档的代码

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

#define MAX 100

typedef int SeqListType;
typedef struct SeqList
{
	SeqListType data[MAX];
	int size;
}SL;

//初始化函数
void InitSeqList(SL* ps);

//打印函数
void Print(SL* ps);

//查找函数
int Find(SL* ps,int pos,SeqListType x);

//中间插入数据的函数
void SeqListInsert(SL* ps,int pos,SeqListType x);

//中间删除数据函数
void SeqListErase(SL* ps,int pos);

//头插函数
void SLPushFront(SL* ps,SeqListType x);

//头删函数
void SLPopFront(SL* ps);

//尾插函数
void SLPushBack(SL* ps,SeqListType x);

//尾删函数
void SLPopBack(SL* ps);



SeqList.c文档的代码

#include"SeqList.h"

//初始化函数
void InitSeqList(SL* ps)
{
	assert(ps != NULL);
	memset(ps->data,0,MAX * sizeof(SeqListType));
	ps->size = 0;
}

//打印函数
void Print(SL* ps)
{
	int i = 0;
	assert(ps != NULL);
	for(i = 0; i < ps->size; i++)
	{
		printf("%d ",ps->data[i]);
	}
	printf("\n");
}

//查找函数
int Find(SL* ps,SeqListType x,int pos)
{
	int i = 0;
	assert(ps != NULL);
	assert(pos >= 0);
	assert(pos < ps->size);
	for(i = pos; i < ps->size; i++)
	{
		if(ps->data[i] == x)
		{
			return i;
		}
	}
	return -1;
}

//中间插入数据的函数
void SeqListInsert(SL* ps,int pos,SeqListType x)
{
	int end = ps->size - 1;
	assert(ps != NULL);
	assert(pos >= 0);
	assert(pos <= ps->size);
	if(ps->size == MAX)
	{
		printf("内存已满,存入数据失败\n");
		return;
	}
	while(pos <= end)
	{
		ps->data[end + 1] = ps->data[end];
		end--;
	}
	ps->data[pos] = x;
	ps->size++;
}

//中间删除数据函数
void SeqListErase(SL* ps,int pos)
{
	int begin = pos + 1;
	assert(ps != NULL);
	assert(pos >= 0);
	assert(pos < ps->size);
	while(begin < ps->size)
	{
		ps->data[begin - 1] = ps->data[begin];
		begin++;
	}
	ps->size--;
}

//头插函数
void SLPushFront(SL* ps,SeqListType x)
{
	assert(ps != NULL);
	SeqListInsert(ps,0,x);
}

//头删函数
void SLPopFront(SL* ps)
{
	assert(ps != NULL);
	SeqListErase(ps,0);
}

//尾插函数
void SLPushBack(SL* ps,SeqListType x)
{
	assert(ps != NULL);
	SeqListInsert(ps,ps->size,x);
}

//尾删函数
void SLPopBack(SL* ps)
{
	assert(ps != NULL);
	SeqListErase(ps,ps->size-1);
}

test.c文档的代码

#include "SeqList.h"

void menu()
{
	printf("***************************************************\n");
	printf("**** 1.尾插                     2.尾删         ****\n");
	printf("**** 3.头插                     4.头删         ****\n");
	printf("**** 5.中间插入数据             6.中间删除数据 ****\n");
	printf("**** 7.查找数据                 8.打印数据     ****\n");
	printf("**** 0.退出                                    ****\n");
	printf("***************************************************\n");
}
int main()
{
	int input = 0;
	SeqListType number = 0;
	int address = 0;
	int location = 0;
	SL SeqList;
	InitSeqList(&SeqList);
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d",&input);
		switch(input)
		{
		case 1:
			printf("请输入你要尾插的数据:>");
			scanf("%d",&number);
			SLPushBack(&SeqList,number);
			break;
		case 2:
			SLPopBack(&SeqList);
			printf("尾删成功\n");
			break;
		case 3:
			printf("请输入你要头插的数据:>");
			scanf("%d",&number);
			SLPushFront(&SeqList,number);
			break;
		case 4:
			SLPopFront(&SeqList);
			break;
		case 5:
			printf("请输入你要在中间插入的数据:>");
			scanf("%d",&number);
			printf("请输入你要插入的位置:>");
			scanf("%d",&address);
			SeqListInsert(&SeqList,address-1,number);
			break;
		case 6:
			printf("请输入你要删除的的数据的位置:>");
			scanf("%d",&address);
			SeqListErase(&SeqList,address-1);
		    break;
		case 7:
			printf("请输入你要查找的数据:>");
			scanf("%d",&number);
			printf("请输入你要从哪个位置开始查找:>");
			scanf("%d",&address);
			location = Find(&SeqList,number,address);
			if(location == -1)
			{
				printf("找不到\n");
			}
			else
			{
				printf("该数字的位置是%d\n",location+1);
			}
			break;
		case 8:
			Print(&SeqList);
			break;
		case 0:
		    printf("退出顺序表\n");
			break;
		default:
			printf("选择错误,请重新选择\n");
			break;
		}
	}while(input);
}

今天,顺序表的静态版本就讲到这里,关注点一点,下期更精彩。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值