【数据结构】顺序表基本操作(创建、初始化、赋值、插入、删除、查询、替换、输出)c语言实现

1、创建、申请空间
2、初始化、顺序表数据结构大小、长度
3、赋值、顺序表数据结构赋值
4、插入、在指定位置插入数据,后续数据循环后移,长度增加,空间大小增加或者不变
5、删除、删除指定位置的数据,后续数据循环前移,长度减小、空间大小不变
6、查询、查看指定数据是否在顺序表结构中
7、替换、将顺序表结构中指定数值替换为另外的数值
8、输出、输出顺序表结构中存储的数据(根据长度大小输出)

  • Seqlist.h
#pragma once

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

typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* a;
	int size;
	int capacity;
}SL;

//初始化 销毁
void SLInit(SL* psl);
void SLDestroy(SL* psl);

//打印 检查空间
void SLPrint(SL* psl);
void SLCheckCapacity(SL* psl);

//头尾插入删除

void  SLPushBack(SL* psl);
void  SLPushFront(SL* psl);
void  SLPopBack(SL* psl);
void  SLPopFront(SL* psl);

//任意下标位置的插入删除

void SLInsert(SL* psl, int pos, SLDataType x);
void SLErase(SL* psl, int pos);

//找到返回下标 没有找到返回-1
int SLFind(SL* psl, SLDataType x);
void SLModify(SL* psl, int pos, SLDataType x);

 顺序表的操作实现

  • Seqlist.c 
#include"Seqlist.h"

void SLInit(SL* psl) //初始化
{
	/*assert(psl);
	psl->a = NULL;
	psl->size = 0;
	psl->capacity = 0;*/
	assert(psl);
	psl->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (psl->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	psl->size = 0;
	psl->capacity = 4;
}

void SLDestroy(SL* psl) // 销毁
{
	assert(psl);
	if (psl->a != NULL)
	{
		free(psl->a);
		psl->a = NULL;
		psl->size = 0;
		psl->capacity = 0;
	}
}

void SLPrint(SL* psl) //打印
{
	assert(psl);

	for (int i = 0;i < psl->size;i++)
	{
		printf("%d ", psl->a[i]);
	}
	printf("\n");
}

void SLCheckCapacity(SL* psl) // 检查空间大小是否合适
{
	assert(psl);
	if (psl->size == psl->capacity)
	{
		SLDataType* tmp = (SLDataType*)realloc(psl->a, sizeof(SLDataType) * psl->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		psl->a = tmp;
		psl->capacity *= 2;
	}
}

void  SLPushBack(SL* psl,SLDataType x)
{
	assert(psl);
	//psl->a[psl->size] = x;
	//psl->size++;

	//psl->a[psl->size++] = x;
	//SLCheckCapacity(psl);
	SLInsert(psl, psl->size, x);
}

void  SLPushFront(SL* psl, SLDataType x)
{
	assert(psl);
	//SLCheckCapacity(psl);

	//int end = psl->size - 1;
	//while (end >= 0)
	//{
	//	psl->a[end + 1] = psl->a[end];
	//	end--;
	//}
	//psl->a[0] = x;
	//psl->size++;
	SLInsert(psl, 0, x);
}

void  SLPopBack(SL* psl)
{
	assert(psl);
	//psl->size--;
	SLErase(psl, psl->size - 1);
}

void  SLPopFront(SL* psl)
{
	assert(psl);
	/*int start = 0;
	while (start < psl->size - 1)
	{
		psl->a[start] = psl->a[start + 1];
		start++;
	}
	psl->size--;*/
	SLErase(psl, 0);
}

//插入
void SLInsert(SL* psl, int pos, SLDataType x)
{
	assert(psl);
	assert(0 <= pos && pos <= psl->size);
	SLCheckCapacity(psl);

	int end = psl->size - 1;
	while (pos <= end)
	{
		psl->a[end + 1] = psl->a[end];
		end--;
	}
	psl->a[pos] = x;
	psl->size++;
}

//删除
void SLErase(SL* psl, int pos)
{
	assert(psl);
	assert(0 <= pos && pos <= psl->size);

	int start = pos + 1;
	while (start < psl->size )
	{
		psl->a[start - 1] = psl->a[start];
		start++;
	}

	psl->size--;
}

//找到返回下标 没有找到返回-1
int SLFind(SL* psl, SLDataType x)
{
	assert(psl);

	for (int i = 0;i < psl->size;i++)
	{
		if (psl->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}

void SLModify(SL* psl, int pos, SLDataType x)
{
	assert(psl);

	assert(0 <= pos && pos <= psl->size);
	psl->a[pos] = x;
}

 完成了菜单的编写

  • test.c
    #define _CRT_SECURE_NO_WARNINGS 1
    #include<stdio.h>
    #include"Seqlist.h"
    
    // 建议,写好函数,先单个测试,没问题,最后在写菜单
    void menu()
    {
    	printf("*******************************\n");
    	printf("1、尾插数据  2、尾删数据\n");
    	printf("3、头插数据  4、头删数据\n");
    	printf("5、打印数据  0、退出  \n");
    	printf("*******************************\n");
    }
    
    int main()
    {
    	SL s;
    	SLInit(&s);
    
    	int option = 0;
    	do
    	{
    		menu();
    		printf("请输入你的选择:>");
    		scanf("%d", &option);
    		if (option == 1)
    		{
    			/*printf("请输入你的要尾插的数据,以-1结束:>");
    			int x = 0;
    			scanf("%d", &x);
    			while (x != -1)
    			{
    				SLPushBack(&s, x);
    				scanf("%d", &x);
    			}*/
    			printf("请依次输入你的要尾插数据个数和数据:>");
    			int n = 0;
    			scanf("%d", &n);
    			for (int i = 0; i < n; i++)
    			{
    				int x = 0;
    				scanf("%d", &x);
    				SLPushBack(&s, x);
    			}
    		}
    		else if (option == 2)
    		{
    			printf("请依次输入你的要尾删数据个数:>");
    			int n = 0;
    			scanf("%d", &n);
    			for (int i = 0; i < n; i++)
    			{
    				SLPopBack(&s);
    			}
    		}
    		else if (option == 3)
    		{
    			printf("请依次输入你的要头插数据个数和数据:>");
    			int n = 0;
    			scanf("%d", &n);
    			for (int i = 0; i < n; i++)
    			{
    				int x = 0;
    				scanf("%d", &x);
    				SLPushFront(&s, x);
    			}
    		}
    		else if (option == 4)
    		{
    			printf("请依次输入你的要头删数据个数:>");
    			int n = 0;
    			scanf("%d", &n);
    			for (int i = 0; i < n; i++)
    			{
    				SLPopFront(&s);
    			}
    		}
    		else if (option == 5)
    		{
    			SLPrint(&s);
    		}
    		else if (option == 0)
    		{
    			break;
    		}
    		else
    		{
    			printf("无此选项,请重新输入\n");
    		}
    	} while (option != 0);
    
    	SLDestroy(&s);
    
    	return 0;
    }
     
  • 29
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值