super顺序表

增删查改

1顺序表

1.1静态数据表

开少了不够用,开多了浪费

1.2动态顺序表

顺序表缺陷

#define _CRT_SECURE_NO_WARNINGS 1
#include"seqlist.h"

void SLInit(SL* ps)
{
	assert(ps);
	 ps->a = (SLDataType*)malloc(sizeof(SLDataType) * int_capacity);
	 if (ps->a == NULL)
	 {
		 perror("malloc");
		 return;
	 }
	 ps->size = 0;
	 ps->capacity=int_capacity;

}
void SLDestroy(SL* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->size = 0;
	ps->capacity=0;
}
void SLPrint(SL* ps)
{
	int i;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	puts("");
}
void SLcapacitycheck(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * ps->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc");
			return;
		}
		ps->a = tmp;
		ps->capacity *= 2;

	}
 }

void SLPushBack(SL* ps, SLDataType x)
{
	//扩容
	SLcapacitycheck(ps);
	ps->a[ps->size] = x;
	ps->size++;
}
void SLPopBack(SL* ps)
{
	// 暴力检查
	assert(ps->size > 0);

	// 温柔的检查
	//if (ps->size == 0)
		//return;= 0)
	ps->size--;
}
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLcapacitycheck(ps);
	for (int i = ps->size; i > 0; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[0] = x;
	ps->size++;
}//可用memmove 
//等差数列时间复杂度O(N^2)
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size>0);
	for (int i = 0; i <ps->size-1; i--)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLcapacitycheck(ps);
	SLDataType i = ps->size;
	while (i >pos )
	{
		ps->a[i] = ps->a[i - 1];
		i--;
	}
	ps->a[i] = x;
	ps->size++;
}
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	int i = pos;
	while (i < (ps->size)-1)
	{
		ps->a[i] = ps->a[i + 1];
		i++;
	}
	ps->size--;
}
int SLFind(SL* ps, SLDataType x)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
			return i;
	}
	return -1;
}

在这里插入图片描述

#pragma once
#include
#include
#include

//typedef int SLDataType;
//#define N 1000
静态顺序表 开少了不够用,开多了浪费
//struct Seqist
//{
//	SLDataType a[N];
//	int size;
//};

typedef int SLDataType;
#define int_capacity 4
//动态顺序表--按需申请
typedef struct
{
	SLDataType* a;
	int size;  //有效数据个数
	int capacity;//空间容量
}SL;
void SLInit(SL* ps);//初始化
void SLDestroy(SL* ps);//摧毁
void SLPrint(SL* ps);
void SLcapacitycheck(SL* ps);
void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
int SLFind(SL* ps, SLDataType x);

你可能感兴趣的:(一个月从数据结构小白到大师,数据结构,c语言)