初识顺序表/顺序表增删查找的实现

 

概念及结构:
顺序表是用一段 物理地址连续 的存储单元依次存储数据元素的线性结构,一般情况下采用 数组 存储。在数组上完成数据的增删查改。

学习目标: 

// SeqList.h
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
 
typedef int SLDateType;
typedef struct SeqList
{
	SLDateType* a;
	int size;
	int capacity;
}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);

1.顺序表的初始化 

注意:

//顺序表的初始化
void SeqListInit(SeqList* ps) {
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;//capacity表示顺序表的容量
	ps->size = 0;//size表示顺序表存储数据的个数
}

注意点: 

这里assert(ps)是为了防止使用者误写,如下:
int main()
{ SeqList* ps=NULL;
  SeqListInit(ps);
  return 0;
}
正确写法应该是
int main()
{   SeqList ps;
    SeqListInit(&ps);//传结构体的地址
     return 0;
}

 2.顺序表的摧毁

void SeqListDestroy(SeqList* ps) {
	assert(ps);
	free(ps->a);//在顺序表中,存储数据的空间是开辟出来的,所以摧毁时要free开辟出的空间
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}

 3.顺序表的打印: 

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

4.顺序表的容量检查 

void SeqListCheck(SeqList* ps) {
	assert(ps);
	if (ps->capacity == ps->size) {
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		int* tmp = (int*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
		if (tmp != NULL) {
			ps->a = tmp;
		}
		ps->capacity = newcapacity;
	}
}

 注意点:当realloc传的地址是空指针时,效果相当于malloc

5.顺序表的尾插 

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

 注意点:因为实现的是动态顺序表,在尾插数据时,需要判断顺序表开辟的空间是否够用,防止越界。

6. 顺序表的头插

void SeqListPushFront(SeqList* ps, SLDateType x) {
	assert(ps);
	SeqListCheck(ps);//检查空间是否够用
	int end = ps->size;                      
	while ( end > 0) {
		ps->a[end] = ps->a[end - 1];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}

注意点:数据的移动顺序需要注意一下,避免数据覆盖导致丢失(从后往前移) 

7.顺序表的头删 

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

注意点:assert(ps->size>0)在顺序表为空时无法继续删除。

              数据移动顺序(从前往后移) 。

8.顺序表的尾删 

void SeqListPopBack(SeqList* ps) {
	assert(ps);
	assert(ps->size > 0);
	ps->size--;//只需size--,在下次插入数据时,会把该位置原来的数据覆盖掉
}

9. 顺序表的查找

int SeqListFind(SeqList* ps, SLDateType x,int begin) {
	assert(ps);
    //int i=begin,意在从下标为i的数据开始往后查找
	for (int i = begin; i < ps->size; i++) {
		if (ps->a[i] == x) {
			return i;//返回该数据在数组中的下标
		}
	}
	return -1;//如果未找到返回负一
}

10.在顺序表的指定下标位置插入数据 

void SeqListInsert(SeqList* ps, int pos, SLDateType x) {
	assert(ps);
	assert(pos >= 0);
	assert(pos < ps->size);
    //上面两个断言-》使数据插入到合法位置
	SeqListCheck(ps);
	int end = ps->size;
		while (end > pos)
		{
			ps->a[end] = ps->a[end - 1];
			end--;
		}
		ps->a[pos] = x;
		ps->size++;
}

11. 在顺序表的指定下标位置删除数据

void SeqListErase(SeqList* ps, int pos) {
	assert(ps);
	assert(pos >= 0);
	assert(pos < ps->size);
	//assert(ps->size>0);可有可无,因为上面两个断言效果是0<=pos<ps->size
	int begin = pos;
	while (begin < ps->size - 1) {
		ps->a[begin] = ps->a[begin + 1];
		begin++;
	}
	ps->size--;
}

12. 删除顺序表中所有值为x的数据(重要)

void SeqListDelete(SeqList* ps, SLDateType x) {
	assert(ps);
	int pos = SeqListFind(ps, x, 0);
    //从0的位置开始查找,把找到数据的下标位置赋给pos,删除该位置的数据,继续从该位置查找,如此循环
	while (pos != -1) {
		SeqListErase(ps, pos);
		pos = SeqListFind(ps, x, pos);
	}

}

 

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值