Seqlist顺序表的实现

本文介绍了Seqlist.h文件中定义的动态顺序表的数据结构及其关键操作,包括初始化、销毁、打印、扩容、尾插、头插、删除和查找。重点展示了如何使用这些函数进行元素的插入、删除和修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Seqlist.h

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



typedef int SLDataType;

//创立动态顺序表
typedef struct SeqList
{
	SLDataType* a;//指向动态数组的指针
	int sz;//数据个数
	int capacity;//容量
}SL;


void IntiSeqList(SL* ps);//初始化Seqlist
void SLDestory(SL* ps);//销毁Seqlist
void SLPrint(SL* ps);//打印Seqlist

void SLCheckCapacity(SL* ps);//扩容


void SLPushBack(SL* ps, SLDataType x);//尾插
void SLPopBack(SL* ps);//尾删
void SLPPushFront(SL* ps, SLDataType x);//头插
void SLPopFront(SL* ps);//头删
void SLInsert(SL* ps, int pos, SLDataType x);//指定任意位置(下标pos)插入
void SLErase(SL* ps, int pos);//指定任意位置(下标pos)删除


int SLFind(SL* ps, SLDataType x);//查找,打印并返回下标
void SLModify1(SL* ps, SLDataType x, SLDataType y);//指定某个值 x 修改为 y
void SLModify2(SL* ps, int pos, SLDataType x);//修改指定下标的元素为x

Seqlist.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "Seqlist.h"



void IntiSeqList(SL* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->sz = 0;
	ps->capacity = 0;
}


void SLDestory(SL* ps)
{
	assert(ps);
	if (ps->a != NULL)
	{
		free(ps->a);
		ps->a = NULL;
	}
	ps->sz = 0;
	ps->capacity = 0;
}


void SLPrint(SL* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->sz; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}


void SLCheckCapacity(SL* ps)
{
	assert(ps);
	if (ps->sz == ps->capacity)
	{
		/*if (ps->capacity == 0)
		{
			SLDataType *tmp=(SLDataType*)realloc(ps->a,4 * sizeof(SLDataType));
		}
		else
		{
			SLDataType* tmp = (SLDataType*)realloc(ps->a,  2 * ps->capacity * sizeof(SLDataType));
		}*/
		//等价于
		int Newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, Newcapacity * sizeof(SLDataType));

		if (tmp != NULL)
		{
			ps->a = tmp;
			ps->capacity = Newcapacity;
			printf("扩容成功\n");
		}
		else
		{
			printf("扩容失败\n");
			perror("SLCheckCapacity::realloc");
			//exit(-1);
			return;
		}
	}
}


void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);//判断是否需要扩容
	ps->a[ps->sz] = x;
	ps->sz++;
}


void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->sz > 0);//暴力检查

	//if (ps->sz == 0)
	//{
	//	printf("无数据可删:> Seqlist is empty");//温柔检查
	//	return;
	//}


	ps->a[ps->sz-1]=0;//多余步骤,实际上可以不要
	ps->sz--;
}


void SLPPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);//判断是否需要扩容
	int i = 0;
	for (i = ps->sz-1; i >= 0; i--)
	{
		ps->a[i + 1] = ps->a[i];
	}
	ps->a[0] = x;
	ps->sz++;
}


void SLPopFront(SL* ps)
{
	int i = 0;
	for (i = 0; i < ps->sz-1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->sz--;
}


void SLInsert(SL* ps, int pos, SLDataType x)
{
	int i = 0;
	for (i = ps->sz-1; i >= pos; i--)
	{
		ps->a[i + 1] = ps->a[i];
	}
	ps->a[pos] = x;
	ps->sz++;
}


void SLErase(SL* ps, int pos)
{
	int i = 0;
	for (i = pos+1; i < ps->sz ; i++)
	{
		ps->a[i - 1] = ps->a[i];
	}
	ps->sz--;
}



int SLFind(SL* ps, SLDataType x)
{
	int i = 0;
	for (i = 0; i < ps->sz; i++)
	{
		if (ps->a[i] == x)
		{
			printf("找到了,下标为:> %d\n", i);
			return i;
		}
	}
	printf("很遗憾,没找到\n");
	return -1;
}



void SLModify1(SL* ps,SLDataType x, SLDataType y)//指定某个值 x 修改为 y
{
	int PreModifyPos = SLFind(ps, x);
	if (PreModifyPos > 0 && PreModifyPos <ps->sz)
	{
		ps->a[PreModifyPos] = y;
		printf("修改成功:>已将下标为 %d 的元素 %d 修改为 %d\n", PreModifyPos,x, y);
	}
	else
	{
		printf("未能找到下标为 %d ,修改失败", PreModifyPos);
	}
}



void SLModify2(SL* ps, int pos, SLDataType x)//指定某个下标 pos 修改为 y;
{
	int i = 0;
	for (i = 0; i < ps->sz; i++)
	{
		if (i == pos)
		{
			ps->a[i] = x;
		}
	}
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "Seqlist.h"

int main()
{
	SL seqlist;
	IntiSeqList(&seqlist);
	SLPushBack(&seqlist, 1);//尾插
	SLPrint(&seqlist);
	SLPushBack(&seqlist, 2);//尾插
	SLPrint(&seqlist);
	SLPushBack(&seqlist, 3);//尾插
	SLPrint(&seqlist);
	SLPushBack(&seqlist, 4);//尾插
	SLPrint(&seqlist);
	SLPushBack(&seqlist, 5);//尾插
	SLPrint(&seqlist);
	SLPushBack(&seqlist, 6);//尾插
	SLPrint(&seqlist);
	SLPPushFront(&seqlist, 11);
	SLPrint(&seqlist);
	SLPopFront(&seqlist);
	SLPrint(&seqlist);
	SLInsert(&seqlist,1,5);
	SLPrint(&seqlist);
	SLErase(&seqlist, 2);
	SLPrint(&seqlist);
	SLFind(&seqlist, 3);
	SLPrint(&seqlist);
	SLModify1(&seqlist, 4, 20);
	SLPrint(&seqlist);

	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");


	int pos=SLFind(&seqlist, 3);
	if (pos > 0 && pos < seqlist.sz)
	{
		 SLModify2(&seqlist, pos, 50);
		 printf("修改成功,已将下标为 %d 的元素3 修改为 50\n ", pos);
	}
	SLPrint(&seqlist);
}

运行图片:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值