【数据结构】顺序表

1.顺序表的概念及结构

顺序表是线性表的一种,线性表是n个具有相同特性的数据元素的有限序列。 线性表是⼀种在实际中⼴泛使 ⽤的数据结构,常⻅的线性表:顺序表、链表、栈、队列、字符串... 线性表在逻辑上是线性结构,也就说是连续的⼀条直线。但是在物理结构上并不⼀定是连续的, 线性表在物理上存储时,通常以数组和链式结构的形式存储。 案例:蔬菜分为绿叶类、⽠类、菌菇类。线性表指的是具有部分相同特性的⼀类数据结构的集合。

顺序表和数组的区别:顺序表的底层结构是数组,对数组的封装,实现了常⽤的增删改查等接⼝

2.顺序表分类

1.静态顺序表

typedef int SLDataType;
#define N 7
typedef struct SeqList
{
	SLDataType a[N];//定长数组
	int size;//有效数据个数
}SL;

静态顺序表缺陷:空间给少了不够⽤,给多了造成空间浪费 

2.动态顺序表

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

3.动态顺序表的实现

SeqList.h文件

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* arr;
	int size;//有效数据个数
	int capacity;//空间容量
}SL;

void SLInit(SL* ps);//初始化
void SLDestroy(SL* ps);//销毁
void SLPushBack(SL* ps, SLDataType x);//尾部插入
void SLPushFront(SL* ps, SLDataType x);//头部插入
void SLPopBack(SL* ps);//尾部删除
void SLPopFront(SL* ps);//头部删除
void SLInsert(SL* ps, int pos, SLDataType x);//指定位置之前插⼊
void SLErase(SL* ps, int pos);//指定位置删除
int Print(SL ps);//打印
void SLExps(SL* ps);//扩容
int SLFind(SL* ps, SLDataType x);//查找

SeqList.c文件

注意:

在这里我们不可以直接使用数组加1的方法进行头删。原因是我们如果加1的话,就会使数组第一个元素的数据的地址有所改变,这可能对当时没有影响,但是当最后我们要销毁顺序表的时候,在执行free指令的时候,释放掉的就不再是当初开辟的那一块空间了,这就会导致内存泄漏以及出现程序运行错误。

在出栈之前数组第一个元素的地址与开辟的空间的地址:

在出栈之后数组第一个元素的地址与开辟的空间的地址:

//错误示范
void QueuePop(Queue* q)
{
	assert(q);
	assert(q->top > 0);
	q->a++;
	q->top--;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void SLInit(SL* ps)
{
	ps->arr = 0;
	ps->size = ps->capacity = 0;
}
void SLDestroy(SL* ps)
{
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->size = ps->capacity = 0;
}
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLExps(ps);
	ps->arr[ps->size++] = x;
}
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLExps(ps);
	int i = 0;
	for (i = ps->size ; i>0; i--)
	{
		ps->arr[i] = ps->arr[i-1];
	}
	ps->arr[0] = x;
	ps->size++;
}
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	int i = 0;
	for (i = 0; i<ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	SLExps(ps);
	int i = 0;
	for (i=ps->size; i>pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	ps->size++;
}
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(ps->size);
	for (int i=pos; i<ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}
int Print(SL ps)
{
	for (int i = 0; i < ps.size; i++)
	{
		printf("%d ", ps.arr[i]);
	}
	printf("\n");
}
void SLExps(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}
int SLFind(SL* ps, SLDataType x)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}

test.c文件

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void test01()
{
	SL s;
	SLInit(&s);//初始化
	SLPushBack(&s, 1);
	SLPushBack(&s, 2);
	SLPushBack(&s, 3);
	SLPushBack(&s, 4);
	SLPushFront(&s, 5);
	SLPopBack(&s);
	SLPopFront(&s);
	SLInsert(&s, 1, 99);
	SLErase(&s, 1);
	Print(s);
	int count = SLFind(&s, 1);
	if (count == -1)
	{
		printf("没有找到");
	}
	else
	{
		printf("找到了,下标是:%d", count);
	}
	SLDestroy(&s);//销毁
}
int main()
{
	test01();
	return 0;
}

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值