【c】动态顺序表

目录

SeqList.h

SeqList.c

text.c


SeqList.h

#pragma once
typedef int SLDateType;
#include<stdio.h>
#include<stdilb.h>
#include<assert.h>
typedef int SLDateType; 
typedef struct SeqList
{
	SLDateType* a;//指向数据的指针
	int size;
	int capacity; //容量
}SeqList;
//顺序表的初始化
void SeqListInit(SeqList* ps);
//顺序表的销毁
void SeqListDestroy(SeqList* ps);
//顺序表是否需要扩容
void CheckCapacity(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.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
//顺序表的初始化
void SeqListInit(SeqList* ps)
{
	assert(ps);
	ps->a = (SLDateType*)malloc(sizeof(SLDateType) * 4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->size = 0;
	ps->capacity = 4;
}

初始化思路:

1传参指针有效

2动态开辟内存成功

3size初始化0,capacity初始化4

//顺序表的销毁
void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

销毁思路:

1传参指针有效

2释放指向动态开辟的数组空间

3size赋值0,capacity赋值4

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

打印思路:

遍历数组打印

//顺序表是否需要扩容
void CheckCapacity(SeqList* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * (ps->capacity) * 2);
		if(tmp==NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->capacity *= 2;
	}
}

检查是否需要扩容思路:

1.传参指针有效

2是否满足扩容条件:大小=容量

//顺序表的尾插
void SeqListPushBack(SeqList* ps, SLDateType x)
{
	assert(ps);
	CheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
}

尾插思路:

1传参指针有效,是否有插入的空间

2直接插入

//顺序表的头插
void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);
	CheckCapacity(ps);
	int i = 0;
	for (i = ps->size - 1;i>=0; i--)
	{
		ps->a[i + 1] =ps->a[i];
	}
	ps->a[0] = x;
	ps->size++;
}

头插思路:

1传参指针有效,是否有插入的空间

2元素从前往后移动

3直接插入

//顺序表的尾删
void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	if (ps->size == 0)
	{
		printf("没有能够删除的数据\n");
		return;
	}
	ps->size--;
}

尾删思路:

1.传参地址有效,并且有元素可供删除

2直接大小-1

//顺序表的头删
void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	if (ps->size == 0)
	{
		printf("没有能够删除的数据\n");
		return;
	}
	int i = 0;
	for (i = 0;i<ps->size-1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

 头删思路:

1传参地址有效,并且有元素可供删除

2元素从后往前移动

3直接大小-1

//顺序表的查找
int SeqListFind(SeqList* ps, SLDateType x)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}

查找思路:

1传参指针有效

2遍历数组查找,返回下标

//顺序表在pos位置插入元素
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	assert(ps);
	CheckCapacity(ps);
	assert(pos >= 0 && pos <= ps->size);
	int i = 0;
	for (i = ps->size - 1; i >= pos; i++)
	{
		ps->a[i + 1] = ps->a[i];
	}
	ps->a[pos] = x;
	ps->size++;
}

特定位置插入思路:

1传参指针有效,有位置可以插入

2找出元素对应的pos位置

3数组元素往后移动

4直接插入

5大小+1

//顺序表在pos位置删除元素
void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	if (ps->size == 0)
	{
		printf("没有能够删除的数据\n");
		return;
	}
	assert(pos >= 0 && pos < ps->size);
	int i = 0;
	for(i=pos;i<ps->size-1;i++)
	{ 
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

特定位置删除元素的思路:

1传参指针有效,数组有元素能够被删除,删除位置要合法

2移动元素

3大小-1

text.c

可以主函数直接测试

也可以主函数里面分很多小模块测试

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
 
void TestSeqList1(){}
void TestSeqList2(){}
void TestSeqList3(){}

int main()
{
    TestSeqList1();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值