顺序表(纯代码)

1.主要代码主体

#define _CRT_SECURE_NO_WARNING
#include"顺序表.h"
//扩容
void SLCheckCapacity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		SLDatetype* tmp = (SLDatetype*)realloc(ps->a, sizeof(SLDatetype) * ps->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->capacity *= 2;
	}
}
//顺序表初始化
void SeqInit(SL* ps)
{
	ps->a = (SLDatetype*)malloc(sizeof(SLDatetype) * INIT_CAPACITY);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->size = 0;
	ps->capacity = INIT_CAPACITY;
}
//顺序表销毁
void SeqDestroy(SL* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}
//尾插
void SLPushBack(SL* ps, SLDatetype x)
{
	assert(ps);
	//扩容
	SLCheckCapacity(ps);
	ps->a[ps->size++] = x;
}
//尾删
void SLPopBack(SL* ps) 
{
	//温柔检查
	/*if (ps->size == 0)
		return;*/
	//暴力检查
	assert(ps->size > 0);
	ps->size--;
}
//打印
void SLPrint(SL* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
     }
	printf("\n");
}
//头插
void SLPushFront(SL* ps, SLDatetype x)
{
	assert(ps->a);
	SLCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[0] = x;
	ps->size++;
}
//中间插入
void SLInsert(SL* ps, int pose, SLDatetype x)
{
	assert(ps);
	assert(pose >=0 && pose <=ps->size);
	SLCheckCapacity(ps);
	int end =ps-> size - 1;
	while (end >=pose)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[pose] = x;
	ps ->size++;
}
//中间删除
void SLErase(SL* ps, int pose)
{
	assert(ps);
	assert(pose > 0 && pose <ps->size);
	int begin = pose + 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
	}
	ps->size--;
}
//查找
int SLFind(SL* ps, SLDatetype x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return 1;
		}
	}
	return -1;
}

//尾删
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size > 0);
	int begin = 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		++begin;
	}
	ps->size--;
}

2.头文件(函数声明) 

#pragma once
#include <stdio.h>
#include<assert.h>
#include<stdlib.h>
//静态顺序表,开少了不够用,开多了浪费
//typedef int SLDatetype;
//#define N 10
//struct Seqlist
//{
//	SLDatetype a[N];
//		int size;
//};

#define INIT_CAPACITY 4
//动态顺序表 按需申请
typedef int SLDatetype;
typedef struct SeqList 
{
 SLDatetype*a;
	int size;//有效数据个数
	int capacity;//空间容量
}SL;
//增删查改
void SeqInit(SL* s);
void SeqDestroy(SL* s);
void SLPushBack(SL* ps, SLDatetype x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDatetype x);
void SLPopFront(SL* ps);
void SLPrint(SL* ps);
void SLCheckCapacity(SL*ps);
void SLInsert(SL* ps,int pose, SLDatetype x);
void SLErase(SL* ps, int pose);
int SLFind(SL* ps, SLDatetype x);

3.检测 

#include"顺序表.h"
//头插/头删N个时间复杂度:O(N*N)
//尾插/尾删N个时间复杂度:O(N)
void TsetSeqList1()
{
	SL s;
	SeqInit(&s);
	SLPushBack(&s, 2);
	SLPushBack(&s, 3);
	SLPushBack(&s, 4);
	SLPrint(&s);
	SeqDestroy(&s);
}
int main(void)
{
	TsetSeqList1();

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值