数据结构C语言:顺序表实现

1. 头文件:

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

typedef int SLDateType;
typedef struct SeqList
{
	SLDateType* a;
	size_t size;
	size_t capacity; // unsigned int
}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, size_t pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos);

2 头文件的实现

注意点:

  • 初始化:顺序表这一数据结构中的数组以指针形式创建,一定要 ps->a=NULL;对这个指针做初始化
  • 销毁:因为扩容都是对arealloc或malloc,所以最后别忘了free(ps->a)
  • 扩容:realloc和malloc使用区别只是前者多ps->a原始指针参数,都需要做指针不空的检查。涉及插入都需要检查是否要扩容
  • 断言:帮助确定在哪儿出错,带pos参数的判断:pos>=0 && pos <= size-1
  • 访问: ps->a[i],这是我之前不清楚的地方,利用ps->a[pos]得到顺序表a位置pos的值
#include "ds_1_seqlst.h"

 // 1. ??** 初始化给一点吧?视频怎么给的
void SeqListInit(SeqList* ps)
{
	// 落了这个? 让a为NULL
	ps->a = NULL;
	// 容量
	ps->capacity = 0;
	// 当前size
	ps->size = 0;
}

// 注意free申请的空间
void SeqListDestroy(SeqList* ps)
{
	// free并置空,忘了free
	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++)
	{
		// 可以用: ps->(a[i]);
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

void CheckCapacity(SeqList* ps)
{
	assert(ps);
	if (ps->capacity == ps->size)
	{
		// 1. 定义新的大小 防止初始化是0,扩容给2倍仍是0
		//int newcapacity = ps->capacity*2;
		int newcapacity = ps->capacity == 0?2: ps->capacity* 2;

		// 2. malloc申请全新arr空间:(类型)malloc(大小)  和realloc不一样
		//SLDateType* newp = (SLDateType*)malloc(sizeof(SLDateType)*newcapacity);
		// 2. realloc直接对原来申请:参数要给原空间 都要强转,比malloc多一个原始指向
		SLDateType* newp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
		if (newp==NULL)
		{
			printf("realloc fail");
			exit(-1);
		}
		ps->a = newp;
		ps->capacity = newcapacity;	// capacity是最大容量
	}
}

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

void SeqListPushFront(SeqList* ps, SLDateType x)
{
	CheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end+1] = ps->a[end];
		end--;
	}
	// 挪完才放
	ps->a[0] = x;
	// 大小一定得加
	ps->size++;
}

void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	assert(ps->size>0);	// 
	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);
	ps->size--;
}

int SeqListFind(SeqList* ps, SLDateType x)
{
	assert(ps);
	// 做O(n)的遍历
	for(int i = 0;i < ps->size;i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}

void SeqListInsert(SeqList* ps, size_t pos, SLDateType x)
{
	assert(ps);
	// 标记 ,看它有没有内容
	//assert(ps->size > 0);
	// 应该检查它插入位置合法吗
	assert(pos >= 0 && pos <= ps->size);
	CheckCapacity(ps);
	for (int i = ps->size;i>pos;i--)
	{
		ps->a[i] = ps->a[i-1];
	}
	ps->a[pos] = x;
	ps->size++;
}

void SeqListErase(SeqList* ps, size_t pos)
{
	assert(ps);
	// 标记 ,看它有没有内容
	//assert(ps->size > 0);
	// 保证pos合法
	assert(pos >= 0 && pos < ps->size);
	// ?????????有一个位置的出入
	for (int i = pos;i<ps->size-1;i++)
	{
		ps->a[i] = ps->a[i+1];
	}
	ps->size--;
}


// 移除题:双指针做法和开辟新空间做法。

3. 测试的主函数

注意点:

  • 函数所有接口都是指针, 所以我们传入的是顺序表地址,习惯上用指针接收地址。
#include "ds_1_seqlst.h"

int main()
{
	SeqList s;
	// 函数中全是地址,用指针接收
	SeqListInit(&s);
	SeqListInsert(&s, 0, 1);
	SeqListPushBack(&s, 5);
	SeqListPrint(&s);
	SeqListPushFront(&s, 6);
	SeqListPrint(&s);
	int pos = SeqListFind(&s, 6);
	printf("找到在%d\n", pos);
	printf("popfront之后\n", pos);
	SeqListPopFront(&s);
	SeqListPrint(&s);
	int pos2 = SeqListFind(&s, 15);
	printf("找个不在的,结果是%d\n", pos2);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值