顺序表的实现

1.代码的实现

SeqList.h

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

typedef int SLDateType;//将int重命名为SLDateType,方便以后要改变int的类型直接在这里改后面就都相应的改变了
typedef struct SeqList
{
	SLDateType* a;//这是一个数组用来存储数据
	int size;     //这个记录的是顺序表有多少个元素
	int capacity; //表示开辟了多大的空间
}SeqList;         //将顺序表命名的SeqList

// 对数据的管理:增删查改 
void SeqListInit(SeqList* ps);       //初始化顺序表
void SeqListDestroy(SeqList* ps);    //释放掉顺序表

void SeqListPrint(SeqList* ps);     // 打印
void SeqListPushBack(SeqList* ps, SLDateType x);//尾插数据
void SeqListPushFront(SeqList* ps, SLDateType x);//头插数据

void SeqListPopFront(SeqList* ps);//尾删
void SeqListPopBack(SeqList* ps);//头删


// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos);

注意这里传给SeqList的都是一级指针,因为如果不传指针那么传的是形参,而形参的改变不会影响实参。

SeqList.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void SeqListInit(SeqList* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;

}
void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	if (ps->a != NULL)
	{
		free(ps->a);
		ps->a = NULL;
		ps->size = 0;
		ps->capacity = 0;

	}

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

}
void SLCheckCapacity(SeqList* ps)
{
	assert(ps);
	if (ps->size==ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDateType* tem = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
		if (tem == NULL)     //使用tem是防止扩容失败,然后原来的空间也没有了
		{
			perror("realloc fail");
			return 1;
			;
		}
		ps->a = tem;
		ps->capacity = newcapacity;
	}
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->a[ps->size]=x;
	ps->size++;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	for (int i=ps->size;i>0;i--)
	{
		ps->a[i] = ps->a[i - 1];

	}
	ps->a[0] = x;
	ps->size++;
	
}
void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	for (int i=0;i<ps->size-1;i++)
	{

		ps->a[i] = ps->a[i + 1];

	}
	ps->size--;

}

void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	assert(ps->size>0);  
	 //删除之前检查size是否为0了,否则变为-1了会影响后面的代码
	ps->size--;
}

void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	assert(pos>=0&&pos<=ps->size-1);
	for (int i=pos;i<pos-1;i++)
	{
		ps->a[i] = ps->a[i + 1];


	}
	ps->size--;

}
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	assert(ps);
	assert(pos>=0&&pos<ps->size);
	SLCheckCapacity( ps);
	for (int i=ps->size;i>pos-1;i--)
	{
		ps->a[i+1] = ps->a[i];


	}
	ps->a[pos] = x;
	ps->size++;

}
int SeqListFind(SeqList* ps, SLDateType x)
{
	assert(ps);
	int i = 0;
		while (ps->a[i]!=x)
		{
			i++;

		}
		return i;

}


2.下面是对各个代码的实现的详细解释

这是顺序表的初始化

void SeqListInit(SeqList* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;

}

这里主要注意检查传来的指针是否为空

这是顺序表的释放

void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	if (ps->a != NULL)
	{
		free(ps->a);
		ps->a = NULL;
		ps->size = 0;
		ps->capacity = 0;
	}
}

注意不仅要将数组ps->a释放掉还要使得size和capacity都置为0;
这是顺序表的打印

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

}

这是插入数据的扩容过程

void SLCheckCapacity(SeqList* ps)
{
	assert(ps);
	if (ps->size==ps->capacity)    //当size=capacity时就要扩容了
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDateType* tem = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
		if (tem == NULL)     //使用tem是防止扩容失败,然后原来的空间也没有了
		{
			perror("realloc fail");
			return 1;
			
		}
		ps->a = tem;
		ps->capacity = newcapacity;
	}
}

realloc函数扩容分为原地扩容异地扩容
原地扩容:当数组后面的空间足够需要扩容的大小那么直接在数组后面扩容。
异地扩容:当数组后面的内存被其他数据占了,不可以在往后扩了,那么就会进行异地扩容。异地扩容是重新开辟一个足够大的空间,然后将数据复制到重新开辟的空间,并把原来的数据释放掉

这是尾插

void SeqListPushBack(SeqList* ps, SLDateType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->a[ps->size]=x;
	ps->size++;
}

ps接受传来的顺序表,x为传来的数据。每一次扩容在size的位置,同时记住要将size加1。
下面是头插

void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	for (int i=ps->size;i>0;i--)//移动整个数组
	{
		ps->a[i] = ps->a[i - 1];

	}
	ps->a[0] = x;      //将x交给下标为1的位置
	ps->size++;
	
}

头插每插一个数据都要将a数组整体后移一次。
下面是头删

void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	for (int i=0;i<ps->size-1;i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

头删也是每删一个数据都要将整个顺序表往前移一个位置
下面是释放pos位置的数据

void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	assert(pos>=0&&pos<=ps->size-1);
	for (int i=pos;i<pos-1;i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

在pos后面插入数据


void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	assert(ps);
	assert(pos>=0&&pos<ps->size);
	SLCheckCapacity( ps);
	for (int i=ps->size;i>pos-1;i--)
	{
		ps->a[i+1] = ps->a[i];


	}
	ps->a[pos] = x;
	ps->size++;

}

查找x所在的位置

int SeqListFind(SeqList* ps, SLDateType x)
{
	assert(ps);
	int i = 0;
		while (ps->a[i]!=x)//遍历数组进行查找
		{
			i++;

		}
		return i;

}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

南子北游

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

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

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

打赏作者

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

抵扣说明:

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

余额充值