数据结构【顺序表】与基本操作

目录

一.顺序表的概念及结构

1.概念

2.顺序表的分类

3.顺序表的结构

 ①.静态顺序表的结构

②.动态顺序表的结构

二.顺序表的基本操作(动态为例)

1. 顺序表的初始化

2.顺序表的尾插和扩容函数

 3.顺序表的打印

4.顺序表的尾删

 5.顺序表的头插

 6.顺序表的头删

7.顺序表的查找

8.顺序表的插入 

9.顺序表的删除 

10.顺序表的销毁

 三.顺序表的全部代码

1.test.c

2.Seqlist.h 

3.Seqlist.c 


一.顺序表的概念及结构

1.概念

顺序表:是用一段连续的物理空间依次存储数据元素的线性结构,常用数组存储。

2.顺序表的分类

①.静态顺序表:用定长数组存储数据,该存储方式定开辟的空间不够灵活,容易造成浪费和空间不够的尴尬情况。

②.动态顺序表::使用动态开辟的数组存储数据,该方式使得顺序表的空间更加灵活,提高了空间利用率。

3.顺序表的结构

 ①.静态顺序表的结构

②.动态顺序表的结构

二.顺序表的基本操作(动态为例)

1. 顺序表的初始化

void initseq(sl* L)
{
	assert(L);

	L->arr = (datatype*)malloc(sizeof(datatype) * INIT);
	L->capacity = INIT;
	L->size = 0;
}

我们给顺序表初始化一个INIT个大小的空间

2.顺序表的尾插和扩容函数

void pushback(sl* L,datatype x)
{
	assert(L);

	check(L);

	L->arr[L->size] = x;
	(L->size)++;

}

 需要注意的是,插入数据的时候,如果空间满了,需要增容

void check(sl* L)
{
	assert(L);

	if (L->capacity == L->size)
	{
		sl* new = realloc(L->arr, sizeof(datatype) * 2 * L->capacity);
		if (new)
		{
			L->arr = new;
			L->capacity*=2;
			printf("Add Finish\n");
		}
		else
			return;
	}
}

 3.顺序表的打印

void print(sl* L)
{
	assert(L);

	int i = 0;
	for (i = 0; i < L->size; i++)
	{
		printf("%d ", L->arr[i]);
	}
	printf("\n");
}

4.顺序表的尾删

void popback(sl* L)
{
	assert(L);
	assert(L->size>0);

	if (L->size == 0)
		return;
	L->size--;
}

需要注意的是:如果表内没有元素,是删不了的! 

 5.顺序表的头插

void pushfront(sl* L,datatype x)
{
	assert(L);

	check(L);

	int end = L->size - 1;
	while (end>=0)
	{
		L->arr[end + 1] = L->arr[end];
		end--;
	}

	L->arr[0] = x;
	L->size++;
}

 6.顺序表的头删

void popfront(sl* L)
{
	assert(L->size>0);
	assert(L);


	int begin = 0;
	while (begin<L->size)
	{
		L->arr[begin] = L->arr[begin+1];
		begin++;
	}
	L->size--;
}

7.顺序表的查找

int slfind(sl* L,datatype x)
{
	assert(L);

	int i = 0;
	for (i = 0; i < L->size; i++)
	{
		if (L->arr[i] == x)
			return 1;
	}
	return -1;
}

8.顺序表的插入 

void insert(sl* L, int pos, datatype x)
{
	assert(L);
	assert(pos >= 0&&pos<=L->size);

	check(L);

	int end = L->size - 1;
	while (end >= pos)
	{
		L->arr[end+1] = L->arr[end];
		end--;
	}
	L->arr[pos] = x;
	L->size++;
}

9.顺序表的删除 

void pop(sl* L, int pos)
{
	assert(L);
	assert(pos >= 0 && pos <= L->size);
	assert(L->size > 0);

	int begin = pos;

	while (begin < L->size)
	{
		L->arr[begin] = L->arr[begin + 1];
			begin++;
	}

	L->size--;
}

10.顺序表的销毁

void slDestroy(sl* L)
{
	assert(L);

	free(L->arr);
	L->arr = NULL;
	L->capacity = L->size = 0;
}

 三.顺序表的全部代码

1.test.c

#include"seqlist.h"

int main()
{
	sl L;
	initseq(&L);
	pushback(&L,1);
	pushback(&L,2);
	print(&L);
	pushback(&L,3);
	pushback(&L,4);
	print(&L);
	pushback(&L,5);
	pushback(&L,6);
	print(&L);
	pushback(&L,7);
	pushback(&L,8);
	print(&L);
	pushback(&L,9);
	print(&L);
	//popback(&L);
	//print(&L);
	pushfront(&L,-1);
	pushfront(&L,-2);
	pushfront(&L,-3);
	pushfront(&L,-4);
	pushfront(&L,-5);
	pushfront(&L,-6);
	pushfront(&L,-7);
	pushfront(&L,-8);
	print(&L);
	popfront(&L);
	print(&L);
	int ret=slfind(&L,20);
	insert(&L, 3, 99);
	print(&L);
	pop(&L, 0);
	print(&L);
    slDestroy(&L);


}

2.Seqlist.h 

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int datatype;
#define INIT 2
typedef struct seqlist
{
	datatype* arr;
	int capacity;
	int size;
}sl;

void initseq(sl* L);

void pushback(sl* L,datatype x);

void print(sl* L);

void popback(sl* L);

void pushfront(sl* L,datatype x);

void popfront(sl* L);

void 

int slfind(sl* L,datatype x);

void insert(sl* L, int pos, datatype x);

void pop(sl* L, int pos);

void slDestroy(sl*L);

3.Seqlist.c 

#include"seqlist.h"

void initseq(sl* L)
{
	assert(L);

	L->arr = (datatype*)malloc(sizeof(datatype) * INIT);
	L->capacity = INIT;
	L->size = 0;
}

void check(sl* L)
{
	assert(L);

	if (L->capacity == L->size)
	{
		sl* new = realloc(L->arr, sizeof(datatype) * 2 * L->capacity);
		if (new)
		{
			L->arr = new;
			L->capacity*=2;
			printf("Add Finish\n");
		}
		else
			return;
	}
}


void pushback(sl* L,datatype x)
{
	assert(L);

	check(L);

	L->arr[L->size] = x;
	(L->size)++;

}

void print(sl* L)
{
	assert(L);

	int i = 0;
	for (i = 0; i < L->size; i++)
	{
		printf("%d ", L->arr[i]);
	}
	printf("\n");
}

void popback(sl* L)
{
	assert(L);
	assert(L->size>0);

	if (L->size == 0)
		return;
	L->size--;
}


void pushfront(sl* L,datatype x)
{
	assert(L);

	check(L);

	int end = L->size - 1;
	while (end>=0)
	{
		L->arr[end + 1] = L->arr[end];
		end--;
	}

	L->arr[0] = x;
	L->size++;
}

void popfront(sl* L)
{
	assert(L->size>0);
	assert(L);


	int begin = 0;
	while (begin<L->size)
	{
		L->arr[begin] = L->arr[begin+1];
		begin++;
	}
	L->size--;
}

int slfind(sl* L,datatype x)
{
	assert(L);

	int i = 0;
	for (i = 0; i < L->size; i++)
	{
		if (L->arr[i] == x)
			return 1;
	}
	return -1;
}

void insert(sl* L, int pos, datatype x)
{
	assert(L);
	assert(pos >= 0&&pos<=L->size);

	check(L);

	int end = L->size - 1;
	while (end >= pos)
	{
		L->arr[end+1] = L->arr[end];
		end--;
	}
	L->arr[pos] = x;
	L->size++;
}

void pop(sl* L, int pos)
{
	assert(L);
	assert(pos >= 0 && pos <= L->size);
	assert(L->size > 0);

	int begin = pos;

	while (begin < L->size)
	{
		L->arr[begin] = L->arr[begin + 1];
			begin++;
	}

	L->size--;
}

void slDestroy(sl* L)
{
 assert(L)
 
 L->size=0;
 L->capacity=0;
 free(L->arr);
 L->arr=NULL;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JJY_s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值