顺序表的增删查改

本文介绍了在Windows环境下如何使用C语言创建了一个名为SeqList的动态顺序表数据结构,包括接口声明、函数实现(如初始化、销毁、插入与删除操作)及测试用例。重点讲解了SeqList.c中的关键代码段和如何在SeqList.h中进行接口设计。
摘要由CSDN通过智能技术生成

1.在win32x下新建项目,且勾选点掉SLD选项(作用是检查更严格,scanf变为scanf_s).在这里插入图片描述在这里插入图片描述
在这里插入图片描述
2.新建头文件SeqList.h,再在SeqList.h中新建SeqList.c文件
在这里插入图片描述
在这里插入图片描述
3.SeqList.h中放接口,函数和结构的声明,SeqList.c中是具体的实现程序,text.c中放的测试文件。

1,SeqList.c

在这里插入代码片
```#include "SeqList.h"

void SeqListInit(SL* ps)
{
	/*s.size = 0;
	s.a = NULL;
	s.capacity = 0;*/

	ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (ps->a == NULL)
	{
		printf("申请内存失败\n");
		exit(-1);
	}

	ps->size = 0;
	ps->capacity = 4;
}

void SeqListDestory(SL* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}

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

void SeqListCheckCapacity(SL* ps)
{
	// 如果满了需要增容
	if (ps->size == ps->capacity)
	{
		ps->capacity *= 2;
		ps->a = (SLDataType*)realloc(ps->a, sizeof(SLDataType)*ps->capacity);
		if (ps->a == NULL)
		{
			printf("扩容失败\n");
			exit(-1);
		}
	}
}

// 尾插尾删  头插头删
void SeqListPushBack(SL* ps, SLDataType x)
{
	//assert(ps);

	//SeqListCheckCapacity(ps);

	//ps->a[ps->size] = x;
	//ps->size++;

	SeqListInsert(ps, ps->size, x);
}

void SeqListPopBack(SL* ps)
{
	assert(ps);

	//ps->a[ps->size - 1] = 0;
	//ps->size--;
	SeqListErase(ps, ps->size - 1);
}

void SeqListPushFront(SL* ps, SLDataType x)

{
	//assert(ps);

	//SeqListCheckCapacity(ps);

	//int end = ps->size - 1;
	//while (end >= 0)
	//{
	//	ps->a[end + 1] = ps->a[end];
	//	--end;
	//}

	//ps->a[0] = x;
	//ps->size++;

	SeqListInsert(ps, 0, x);
}

void SeqListPopFront(SL* ps)
{
	/*assert(ps);

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

	ps->size--;*/

	SeqListErase(ps, 0);
}

// 任意位置的插入删除
// size_t 就是 unsigned int
void SeqListInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos <= ps->size && pos >= 0);

	SeqListCheckCapacity(ps);

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

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

void SeqListErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos < ps->size && pos >= 0);

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

	ps->size--;
}

int SeqListFind(SL* ps, SLDataType x)
{
	assert(ps);

	int i = 0;
	while (i < ps->size)
	{
		if (ps->a[i] == x)
		{
			return i;
		}

		++i;
	}
	
	return -1;
}

**

## 2.Seqlist.h



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

// 顺序表,有效数组在数组中必须是连续
// 静态顺序表设计 (固定大小)
//typedef int SLDataType;
//#define N 10
//
 vector
//struct SeqList
//{
//	SLDataType a[N];
//	int size;
//};
//
//void SeqListPushBack(struct SeqList* ps, SLDataType x);
//void SeqListPopBack(struct SeqList* ps);
//void SeqListPushFront(struct SeqList* ps, SLDataType x);
//void SeqListPopFront(struct SeqList* ps);

// 动态顺序表设计 (大小可变)
typedef int SLDataType;

// vector
typedef struct SeqList
{
	SLDataType* a;
	int size;      // 有效数据的个数
	int capacity;  // 容量
}SL, SeqList;

// 尾插尾删  头插头删
void SeqListInit(SL* ps);
void SeqListDestory(SL* ps);

void SeqListPrint(SL* ps);
void SeqListCheckCapacity(SL* ps);

void SeqListPushBack(SL* ps, SLDataType x);
void SeqListPopBack(SL* ps);
void SeqListPushFront(SL* ps, SLDataType x);
void SeqListPopFront(SL* ps);

// 任意位置的插入删除
void SeqListInsert(SL* ps, int pos, SLDataType x);
void SeqListErase(SL* ps, int pos);

// 顺序表查找
int SeqListFind(SL* psl, SLDataType x);

// ...
//int SeqListSort(SL* psl);
//int SeqListBinaryFind(SL* psl, SLDataType x);


## 3.测试文件

#include "SeqList.h"


// 测试头尾插入删除
void TestSeqList1()
{
	SeqList s;
	SeqListInit(&s);
	SeqListPushBack(&s, 1);
	SeqListPushBack(&s, 2);
	SeqListPushBack(&s, 3);
	SeqListPushBack(&s, 4);
	SeqListPushBack(&s, 5);
	SeqListPushBack(&s, 6);
	SeqListPushBack(&s, 7);
	SeqListPushBack(&s, 8);
	//SeqListPushBack(&s, 9);
	SeqListPrint(&s);

	SeqListPopBack(&s);
	SeqListPopBack(&s);
	SeqListPrint(&s);

	SeqListPushFront(&s, -1);
	SeqListPushFront(&s, -2);
	SeqListPushFront(&s, -3);
	SeqListPrint(&s);

	SeqListPopFront(&s);
	SeqListPopFront(&s);
	SeqListPrint(&s);


	int pos = SeqListFind(&s, 5);
	if (pos != -1)
	{
		SeqListErase(&s, pos);
	}

	SeqListPrint(&s);


	SeqListDestory(&s);

}

int main()
{
	TestSeqList1();

	return 0;
}
*





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值