顺序表的相关操作

线性表

  • 概念:线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
    线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

顺序表

  • 概念:顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
    顺序表一般可以分为:
  1. 静态顺序表:使用定长数组存储。
  2. 动态顺序表:使用动态开辟的数组存储。

每一种数据结构说到底都逃不过增删改查,相对而言顺序表的相关操作比较简单,话不多说,咱们直接上代码。

#include<iostream>

using namespace std;

//顺序表的定义
typedef struct SeqList
{
	int* array;  // 指向在堆上开辟的空间
	int capacity;  //顺序表的容量
	int size;  //顺序表的有效元素个数
}SeqList;

//顺序表的初始化
void SeqListInit(SeqList* p,int capacity)
{
	if (p->array != NULL)
	{
		p->array = (int*)malloc(sizeof(int)*capacity);
		p->capacity = capacity;
		p->size = 0;
	}
}

//打印顺序表的内容
void printSeqList(SeqList* p)
{
	cout << "顺序表的容量为" << p->capacity << "个" << endl;
	cout << "顺序表的有效元素为" << p->size << "个" << endl;
	for (int i = 0; i < p->size; i++)
	{
		cout << p->array[i] << " ";
	}
	cout << endl;
}

//顺序表的销毁
void DestorySeqList(SeqList* p)
{
	if (p->array != NULL)
	{
		free(p->array);
		p->array = NULL;
		p->capacity = 0;
		p->size = 0;
	}
}
//扩容
void CheckSeqListCapacity(SeqList* p)
{
	int newcapacity = 2 * p->capacity;
	int* newArray = (int *)malloc(sizeof(int)*newcapacity);
	free(p->array);
	p->array = newArray;
	p->capacity = newcapacity;
}

//头插入
void SeqListInsertFront(SeqList* p,int value)
{
	if (p->array == NULL)
	{
		return ;
	}
	if (p->size == p->capacity)
	{
		CheckSeqListCapacity(p);
	}
	for (int i = p->size - 1; i >= 0; i--)
	{
		p->array[i + 1] = p->array[i];
	}
	p->array[0] = value;
	p->size++;
}

//尾插入
void SeqListInsertBack(SeqList* p, int value)
{
	if (p->array == NULL)
	{
		return;
	}
	p->array[p->size] = value;
	p->size++;
}
//中间插入数据
void SeqListInsertMid(SeqList* p, int value,int pos)
{
	if (p->array == NULL)
	{
		return;
	}
	for (int i = p->size - 1; i >= pos; i--)
	{
		p->array[i + 1] = p->array[i];
	}
	p->array[pos] = value;
	p->size++;
}
//头删除
void SeqListDelFront(SeqList* p)
{
	if (p->array == NULL)
	{
		return;
	}
	for (int i = 1; i < p->size; i++)
	{
		p->array[i-1] = p->array[i];
	}
	p->size--;
}

//尾删除
void SeqListDelBack(SeqList* p)
{
	if (p->array == NULL)
	{
		return;
	}
	p->size--;
}

//中间删除
void SeqListDelMid(SeqList* p,int pos)
{
	if (p->array == NULL)
	{
		return;
	}
	for (int i = pos; i < p->size; i++)
	{
		p->array[i] = p->array[i + 1];
	}
	p->size--;
}

//修改顺序表元素内容
void SeqListModify(SeqList* p, int pos, int value)
{
	if (p->array == NULL)
	{
		return;
	}
	p->array[pos] = value;
}
//查找
bool SeqListFind(SeqList* p, int value)
{
	if (p->array == NULL)
	{
		return false;
	}
	for (int i = 0; i < p->size; i++)
	{
		if (p->array[i] == value)
		{
			return true;
		}
	}
	return false;
}

int main()
{
	SeqList ps;
	int capacity;
	cout << "--------------------------" << endl;
	cout << "请输入顺序表的容量:";
	cin >> capacity;
	SeqListInit(&ps,capacity);
	cout << "顺序表初始化后内容如下:" << endl;
	printSeqList(&ps);
	cout << "--------------------------" << endl;

	cout << "请输入头插入数据:";
	int value;
	cin >> value;
	SeqListInsertFront(&ps, value);
	cout << "请输入尾插入数据:";
	int value1;
	cin >> value1;
	SeqListInsertBack(&ps, value1);
	cout << "请输入中间插入的数据:";
	int value2;
	cin >> value2;
	cout << endl;
	cout << "请输入插入位置:";
	int pos;
	cin >> pos;
	SeqListInsertMid(&ps, value2, pos);
	printSeqList(&ps);

	cout << "请输入修改元素的下标:";
	int pos2;
	cin >> pos2;
	cout << "请输入要修改的值:";
	int value3;
	cin >> value3;
	SeqListModify(&ps, pos2, value3);
	printSeqList(&ps);

	cout << "请输入要查找的值:";
	int value4;
	cin >> value4;
	SeqListFind(&ps,value4);
	printSeqList(&ps);

	cout << "请输入中间删除的位置:";
	int pos1;
	cin >> pos1;
	SeqListDelMid(&ps, pos1);
	printSeqList(&ps);

	cout << "头删除" << endl;
	SeqListDelFront(&ps);
	printSeqList(&ps);

	cout << "尾删除" << endl;
	SeqListDelBack(&ps);
	printSeqList(&ps);

	system("pause");
	return 0;
}

注:其实顺序表的操作并不是很难,当我们要是实在想不通时,不妨动手去画一画,有了图我们才能更好的去理解。在此我还要说一点,就是顺序表中数据连续,具有随机访问能力,所以在访问数据时很快,但同时因为数据连续,在我们进行增删的时候就比较费时了,总体来说,对于我们初学者来说,学好顺序表我们才能够继续学习相对复杂的数据结构。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值