【C语言数据结构】模拟·顺序表·总项目实现

在这里插入图片描述

💐 🌸 🌷 🍀 🌹 🌻 🌺 🍁 🍃 🍂 🌿 🍄🍝 🍛 🍤
📃个人主页 阿然成长日记 👈点击可跳转
📆 个人专栏: 🔹数据结构与算法🔹C语言进阶
🚩 不能则学,不知则问,耻于问人,决无长进
🍭 🍯 🍎 🍏 🍊 🍋 🍒 🍇 🍉 🍓 🍑 🍈 🍌 🍐 🍍

前言

我在上一篇博客中,详细讲解啦每一个函数的实现思路和代码展现,在这一篇博客中,我将像是做项目一样,去实现顺序表的总体实现。

一、项目源文件构成

该项目由三部分组成
1️⃣ 用来存放库函数,宏定义,函数申明等的一个头文件:SqList.h
2️⃣ 主函数的所在文件 test.c
3️⃣各个函数的实现,我们主要在此完成函数的代码编写:SqList.c

二、菜单

建立一个菜单是很重要的,菜单能够实现和用户的交互,以便于用户的操作。

代码如下:

void meun()
{
	printf("**************************************************\n");
	printf("*    1.顺序表尾插            2.顺序表尾删        *\n");
	printf("*    3.顺序表头插            4.顺序表头删        *\n");
	printf("*    5.顺序表的查找          6.在pos位置插入x    *\n");
	printf("*    7.删除pos位置的值       8.顺序表的打印      *\n");
	printf("*    0.退出                                      *\n");
	printf("**************************************************\n");
}

三、顺序表结构体

typedef int SLDataType;

typedef struct SeqList
{
	SLDataType* array;
	int size;//当前存储个数
	int capacity;//空间大小

}SL;

四、源文件展示

下面是整个项目的代码:

1.SqList.h

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

#define INT_SIZE 2

typedef int SLDataType;

typedef struct SeqList
{
	SLDataType* array;
	int size;//当前存储个数
	int capacity;//空间大小

}SL;

//顺序表的初始化
void SeqListInit(SL* ps);


// 顺序表尾插
void SeqListPushBack(SL* ps, SLDataType x);


// 顺序表尾删
void SeqListPopBack(SL* ps);


// 顺序表头插
void SeqListPushFront(SL* p, SLDataType x);


// 顺序表头删
void SeqListPopFront(SL* p);


// 顺序表查找
int SeqListFind(SL* p, SLDataType x);


// 顺序表在pos位置插入x
void SeqListInsert(SL* p, size_t pos, SLDataType x);


// 顺序表删除pos位置的值
void SeqListErase(SL* p, size_t pos);


// 顺序表销毁
void SeqListDestory(SL* p);


// 顺序表打印
void SeqListPrint(SL* p);
 

2.SqList.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"SqList.h"
//顺序表的初始化
void SeqListInit(SL* ps)
{
	ps->array = (SLDataType*)malloc(sizeof(SLDataType)*4);
	if (ps->array == NULL)
	{
		perror("malloc failed\n");
		exit(-1);
	}
	ps->size = 0;
	ps->capacity = 4;
}


// 检查空间,如果满了,进行增容
int CheckCapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->array, (ps->capacity + INT_SIZE)*sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("扩容失败!\n");
			return 0;
		}
		else
		{
			ps->array = tmp;
			ps->capacity += INT_SIZE;
			printf("扩容成功\n");
			return 1;
		}	
	}
	return 1;
}



// 顺序表销毁
void SeqListDestory(SL* ps)
{
	free(ps->array);
	ps->array = NULL;
	ps->capacity = 0;
	ps->size = 0;
}


// 顺序表尾插
void SeqListPushBack(SL* ps, SLDataType x)
{
	if (CheckCapacity(ps) == 0)
	{
		printf("空间已满,插入失败!\n");
	}
	if (CheckCapacity(ps) == 1)
	{
		ps->array[ps->size] = x;
		ps->size ++;
	}
}


// 顺序表尾删
void SeqListPopBack(SL* ps)
{
	if (ps->size < 1)
	{
		printf("已经为空,无元素可删除\n");
		return;
	}
	ps->array[ps->size - 1] = 0;
	ps->size--;
}

// 顺序表头插
void SeqListPushFront(SL* ps, SLDataType x)
{
	//判断空间是否够。
	
	//先挪动
	if (CheckCapacity(ps) == 0)
	{
		printf("空间已满,头插入失败!\n");
	}
	if (CheckCapacity(ps) == 1)
	{
	int end = ps->size - 1;
	while (end>=0)
	{
		ps->array[end + 1] = ps->array[end];
		end--;
	}
	ps->array[0] = x;
	ps->size++;

	}
}


// 顺序表头删
void SeqListPopFront(SL* ps)
{
	int n = 1;
	while (n < ps->size)
	{
		ps->array[n - 1] = ps->array[n];
		n++;
	}
	ps->size--;
}

// 顺序表打印
void SeqListPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->array[i]);
	}
	printf("\n");
}


// 顺序表查找
int SeqListFind(SL* ps, SLDataType x)
{
	int i = 0,count=0;
	for (i = 0; i < ps->size - 1; i++)
	{
		if (x == ps->array[i])
		{
			printf("找到啦!下标是%d\n",i);
			count++;
			return i;
		}
	}
	if (count == 0)
	{
		printf("没找到!\n");
		return;
	}
}


// 顺序表在pos位置插入x
void SeqListInsert(SL* ps, size_t pos, SLDataType x)
{
	assert(pos>=0&&pos<ps->size);
	if (CheckCapacity(ps) == 0)
	{
		printf("空间已满,插入失败!\n"); 
	}
	if (CheckCapacity(ps) == 1)
	{
		int end = ps->size - 1 ;
		while (end >= pos)
		{
			ps->array[end + 1] = ps->array[end];
			end--;
		}
		ps->array[pos] = x;
		ps->size++;
	}
}



// 顺序表删除pos位置的值
void SeqListErase(SL* ps, size_t pos)
{
	assert(pos >= 0 && pos < ps->size);
	int n = pos + 1;
	while (n <= ps->size - 1)
	{
		ps->array[n - 1] = ps->array[n];
		n++;
	}
	ps->size--;
}

3.test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"SqList.h"

void meun()
{
	printf("**************************************************\n");
	printf("*    1.顺序表尾插            2.顺序表尾删        *\n");
	printf("*    3.顺序表头插            4.顺序表头删        *\n");
	printf("*    5.顺序表的查找          6.在pos位置插入x    *\n");
	printf("*    7.删除pos位置的值       8.顺序表的打印      *\n");
	printf("*    0.退出                                      *\n");
	printf("**************************************************\n");
}
int main()
{
	int x,pos;
	SL s;
	//初始化
	SeqListInit(&s);
	int input = 0;
	do
	{
		meun();
		printf("请输入你的选择:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("请输入要尾插的值:");
			scanf("%d", &x);
			SeqListPushBack(&s, x);
			break;
		case 2:
			SeqListPopBack(&s);
			break;
		case 3:
			printf("请输入要头插的值:");
			scanf("%d", &x);
			SeqListPushFront(&s,x);
			break;
		case 4:SeqListPopFront(&s);
			break;
		case 5:
			printf("请输入要查找的值:");
			scanf("%d", &x);
			SeqListFind(&s,x); 
			break;
		case 6:
		{
			printf("请输入要插入的位置和数据(空格分开):");
			scanf("%d %d", &pos,&x);
			SeqListInsert(&s, pos, x);
			break;
		}
		case 7:
			printf("请输入要删除的位置:");
			scanf("%d", &pos);
			SeqListErase(&s,pos); 
			break;
		case 8:
			 SeqListPrint(&s);
			 break;
		case 0:
			SeqListDestory(&s);
			break;
		default:printf("输入有误,请重新输入:\n");
		}
	}while (input);
}

五、运行截图

1.顺序表尾插,头插展示

在这里插入图片描述

2.顺序表的头删

在这里插入图片描述

3.顺序表的尾删

在这里插入图片描述

4.顺序表的查找

在这里插入图片描述

5.在pos位置插入x

在这里插入图片描述

6.在pos位置删除元素

在这里插入图片描述

总结:

本次项目当中遇到许多之气没有注意到的问题,尤其是数组越界问题等等,在接下来学习数据结构预算法是非常重要的,🌈相信自己,踏踏实实走好每一步,梦想终会成为现实! ⛵️

  • 12
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿然成长日记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值