顺序表头插头删尾插尾删以及任意位置的插入删除和顺序表中的查找

本文通过C++代码展示了动态顺序表的基本操作,包括初始化、尾插、尾删、头插、头删、元素查找与删除。通过TestSeqlist1()函数的测试用例,详细演示了顺序表的增删改查流程,帮助理解数据结构中的顺序表实现。
摘要由CSDN通过智能技术生成

今天,话不多说,直接上代码

//这里对函数进行测试
#include"Seqlist.h"
#include<assert.h>
#include<stdlib.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);
	

	SeqlistPrint(&s);


	SeqlistPopBack(&s);
	SeqlistPopBack(&s);
	SeqlistPopBack(&s);
	SeqlistPrint(&s);



	SeqlistPushFront(&s, -1); 
	SeqlistPrint(&s);


	SeqlistPopFront(&s);
	SeqlistPopFront(&s);
	SeqlistPrint(&s);
	SeqlistDestory(&s);


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

	SeqlistPrint(&s);
	SeqlistDestory(&s);
	

 }
int main()
{
	TestSeqlist1();
	return  0;
}
















#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//这个文件中写的是函数的申明
//顺序表其实就是数组,且有效数组在数组中必须是连续的
//动态顺序表实现
typedef int SlDATAType;

typedef    struct Seqlist
{
	SlDATAType* p;
	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 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);









#include"Seqlist.h"//这里写函数的定义
typedef int SlDATAType;

void SeqlistInit(SL* ps)
{
	ps->p = (SlDATAType*)malloc(sizeof(SlDATAType) * 4);
	if (ps->p == NULL)
	{
		printf("申请内存失败\n");
		exit(-1);
	}

	ps->size= 0;
	ps->capacity = 4;
}
void SeqlistDestory(SL* ps)
{
	free(ps->p);
	ps->p = NULL;
	ps->size = ps->capacity = 0;
}
void SeqlistPrint(SL* ps)
{
	assert(ps);
		for (int i = 0; i < ps->size; ++i)
		{
			printf("%d", ps->p[i]);
		}
		printf("\n");
}
void SeqlistCheckCapacity(SL* ps)
{
	//如果满了需要增容
	if (ps->size == ps->capacity)
	{
		ps->capacity *= 2;
		ps->p = (SlDATAType*)realloc(ps->p, sizeof(SlDATAType) * ps->capacity);

		if (ps->p == NULL)
		{
			printf("扩容失败");
			exit(-1);
		}
	}
}
void SeqlistPushBack( SL* ps, SlDATAType x)
{
	assert(ps);
	SeqlistCheckCapacity(ps);
	ps->p[ps->size] = x;
	ps->size++;
}
void SeqlistPopBack(SL* ps)
{
	assert(ps);
	//ps->p[ps->size - 1] = 0;
	ps->size--;


}

void SeqlistPushFront(SL* ps, SlDATAType x)
{
	assert(ps);
	SeqlistCheckCapacity(ps);
	int end = ps->size - 1;
	while(end >= 0)
	{
		ps->p[end + 1] = ps->p[end];
		--end;
	}
	ps->p[0] = x;
	ps->size++;
}
void SeqlistPopFront(SL* ps)
{
	assert(ps);
	int start = 0;
	while (start < ps->size - 1)
	{
		ps->p[start] = ps->p[start + 1];
		++start;
	}
	ps->size--;
}

//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->p[end + 1] = ps->p[end];
		--end;
	}
	ps->p[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->p[start] = ps->p[start + 1];
		++start;
	}
	ps->size--;

}
//顺序表中的查找
int SeqlistFind(SL* ps, SlDATAType x)
{
	assert(ps);
	int i = 0;
	while (i < ps->size)
	{
		if (ps->p[i] == x)
		{
			return i;
		}
		++i;
	}
	return -1;
}



















今天就分享到这里,我们下期再见啦

评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值