【数据结构初阶】顺序表

顺序表

    • 1.线性表
    • 2.顺序表
    • 2.1概念以及结构
    • 2.1.1静态顺序表
    • 2.1.2动态顺序表
    • 3.顺序表的实现
      • 3.1初始化内容
      • 3.2初始化函数
      • 3.3销毁函数
      • 3.4打印函数
      • 3.5扩容函数
      • 3.6尾插
      • 3.6尾删函数
      • 3.7头插函数
      • 3.8头删函数
      • 3.9查找函数
      • 3.10插入函数
      • 3.11删除函数
      • 3.12修改函数
    • 4. 顺序表的问题
    • 5.相关题目
    • 全部码源

1.线性表

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

2.顺序表

2.1概念以及结构

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般可以分为:

2.1.1静态顺序表

静态顺序表:使用定长数组存储元素。

// 静态顺序表
#define N 7
typedef int SLDataType;

struct SeqList
{
	SLDataType a[N];
	int size;
}SeqList;

2.1.2动态顺序表

动态顺序表:使用动态开辟的数组存储

// 动态顺序表
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* a;
	int size;        // 存储有效数据个数
	int capacity;    // 空间大小
}SL;

3.顺序表的实现

3.1初始化内容

顺序表包含文件:

  • SeqList.h—函数的声明和库函数的包含
  • SeqList.c—用来测试你写的顺序表能不能用
  • test.c—用来测试你写的顺序表能不能用

函数的声明和库函数的包含及接口实现:

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
// 动态顺序表
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* a;
	int size;        // 存储有效数据个数
	int capacity;    // 空间大小
}SL;
// 管理数据 -- 增删查改
void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SLPrint(SL* ps);
void SLCheckCapacity(SL* ps);
// 头插头删 尾插尾删
void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);

// 返回下标,没有找打返回-1
int SLFind(SL* ps, SLDataType x);

// 在pos位置插入x
void SLInsert(SL* ps, int pos, SLDataType x);
// 删除pos位置的值
void SLErase(SL* ps, int pos);

void SLModify(SL* ps, int pos, SLDataType x);

3.2初始化函数

SeqList.h

void SLInit(SL* ps);

SeqList.c

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

用malloc开辟数组a的空间大小,对size,capacity进行初始化

3.3销毁函数

SeqList.h

void SLDestroy(SL* ps);

SeqList.c

void SLDestroy(SL* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}

3.4打印函数

SeqList.h

void SLPrint(SL* ps);

SeqList.c

void SLPrint(SL* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

3.5扩容函数

SeqList.h

void SLCheckCapacity(SL* ps);

SeqList.c

void SLCheckCapacity(SL* ps)
{
	assert(ps);
	// 满了要扩容
	if (ps->size == ps->capacity)//先检查空间是否够用
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2(sizeof(SLDataType)));//一般2倍扩容
		if (tmp == NULL)
		{
			perror("realloc failed");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity *= 2;
	}
}

补:

3.6尾插

SeqList.h

void SLPushBack(SL* ps, SLDataType x);

SeqList.c

void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
	//SLInsert(ps, ps->size, x);
}

3.6尾删函数

SeqList.h

void SLPopBack(SL* ps);

SeqList.c

void SLPopBack(SL* ps)
{
	assert(ps);
	// 温柔的检查
	//if (ps->size == 0)
		//return;
	// 暴力的检查
	assert(ps->size > 0);
	//ps->a[ps->size - 1] = 0;
	ps->size--;
	//SLErase(ps, ps->size - 1);
}

3.7头插函数

SeqList.h

void SLPushFront(SL* ps, SLDataType x);

SeqList.c

void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	// 挪动数据
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[0] = x;
	ps->size++;
	//SLInsert(ps, 0, x);
}

3.8头删函数

SeqList.h

void SLPopFront(SL* ps);

SeqList.c

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--;
	//SLErase(ps, 0);
}

3.9查找函数

SeqList.h

// 返回下标,没有找打返回-1
int SLFind(SL* ps, SLDataType x);

SeqList.c

int SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}

3.10插入函数

SeqList.h

// 在pos位置插入x
void SLInsert(SL* ps, int pos, SLDataType x);

SeqList.c

// 在pos位置插入x
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);//插入数字是否在数组内
	SLCheckCapacity(ps);检查是否需要扩容
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[pos] = x;
	ps->size++;
}

3.11删除函数

SeqList.h

// 删除pos位置的值
void SLErase(SL* ps, int pos);

SeqList.c

// 删除pos位置的值
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);//插入数字是否在数组内
	int begin = pos + 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		++begin;
	}
	ps->size--;
}

3.12修改函数

SeqList.h

void SLModify(SL* ps, int pos, SLDataType x);

SeqList.c

void SLModify(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	ps->a[pos] = x;
}

4. 顺序表的问题

  1. 中间/头部的插入删除,时间复杂度为O(N)
  2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
  3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。

5.相关题目

  1. 原地移除数组中所有的元素val,要求时间复杂度为O(N),空间复杂度为O(1)。OJ
  2. 删除排序数组中的重复项。OJ链接
  3. 合并两个有序数组。OJ链接

全部码源

SeqList.h

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

 静态顺序表
//#define N 1000
//typedef int SLDataType;
//
//struct SeqList
//{
//	SLDataType a[N];
//	int size;
//};
// 动态顺序表
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* a;
	int size;        // 存储有效数据个数
	int capacity;    // 空间大小
}SL;
// 管理数据 -- 增删查改
void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SLPrint(SL* ps);
void SLCheckCapacity(SL* ps);
// 头插头删 尾插尾删
void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);
// 返回下标,没有找打返回-1
int SLFind(SL* ps, SLDataType x);
// 在pos位置插入x
void SLInsert(SL* ps, int pos, SLDataType x);
// 删除pos位置的值
void SLErase(SL* ps, int pos);
void SLModify(SL* ps, int pos, SLDataType x);

SeqList.c

#include"SeqList.h"
void SLInit(SL* ps)
{
	assert(ps);
	ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (ps->a == NULL)
	{
		perror("malloc failed");
		exit(-1);
		//return;
	}
	ps->size = 0;
	ps->capacity = 4;
}
void SLDestroy(SL* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}
void SLPrint(SL* ps)
{
	assert(ps);

	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
void SLCheckCapacity(SL* ps)
{
	assert(ps);

	// 满了要扩容
	if (ps->size == ps->capacity)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDataType)));
		if (tmp == NULL)
		{
			perror("realloc failed");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity *= 2;
	}
}
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);

	ps->a[ps->size] = x;
	ps->size++
	//SLInsert(ps, ps->size, x);
}

void SLPopBack(SL* ps)
{
	assert(ps);

	// 温柔的检查
	//if (ps->size == 0)
		//return;
	//暴力的检查
	assert(ps->size > 0);
	//ps->a[ps->size - 1] = 0;
	ps->size--;
	//SLErase(ps, ps->size - 1);
}

void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	// 挪动数据
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[0] = x;
	ps->size++;
	SLInsert(ps, 0, x);
}

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--;
	//SLErase(ps, 0);
}

int SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}

// 在pos位置插入x
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[pos] = x;
	ps->size++;
}
// 删除pos位置的值
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	int begin = pos + 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		++begin;
	}
	ps->size--;
}
void SLModify(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	ps->a[pos] = x;
}

test.c

#include"SeqList.h"

void TestSeqList1()
{
	SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPushBack(&sl, 5);
	SLPushBack(&sl, 6);
	SLPushBack(&sl, 6);
	SLPushBack(&sl, 0);
	SLPushBack(&sl, 0);
	SLPrint(&sl);

	SLPopBack(&sl);
	SLPopBack(&sl);
	SLPrint(&sl);

	SLPopBack(&sl);
	SLPopBack(&sl);
	SLPopBack(&sl);
	SLPopBack(&sl);
	SLPopBack(&sl);
	SLPopBack(&sl);
	SLPopBack(&sl);
	//SLPopBack(&sl);
	//SLPopBack(&sl);
	/*SLPopBack(&sl);
	SLPopBack(&sl);
	SLPopBack(&sl);
	SLPopBack(&sl);*/
	SLPrint(&sl);

	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPrint(&sl);

	SLDestroy(&sl);
}

void TestSeqList2()
{
	SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPushBack(&sl, 5);
	SLPrint(&sl);

	SLPushFront(&sl, 10);
	SLPushFront(&sl, 20);
	SLPushFront(&sl, 30);
	SLPushFront(&sl, 40);
	SLPrint(&sl);
	SLDestroy(&sl);
}

void TestSeqList3()
{
	SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPushBack(&sl, 5);
	SLPrint(&sl);
	SLPopFront(&sl);
	SLPopFront(&sl);
	SLPrint(&sl);
	SLPopFront(&sl);
	SLPopFront(&sl);
	SLPopFront(&sl);
	//SLPopFront(&sl);
	SLPrint(&sl);
	SLPushBack(&sl, 4);
	SLPushBack(&sl, 5);
	SLPrint(&sl);
	SLDestroy(&sl);
}

void TestSeqList4()
{
	SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPushBack(&sl, 5);
	SLPushFront(&sl, -1);
	SLPushFront(&sl, -2);
	SLPrint(&sl);
	SLInsert(&sl, 3, 40);
	SLPrint(&sl);
	int x;
	scanf("%d", &x);
	int pos = SLFind(&sl, x);
	if (pos != -1)
	{
		SLInsert(&sl, pos, x * 10);
	}
	SLPrint(&sl);
	SLDestroy(&sl);

}

void TestSeqList5()
{
	SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPushBack(&sl, 5);
	SLPrint(&sl);
	SLErase(&sl, 2);
	SLPrint(&sl);

	int x;
	scanf("%d", &x);
	int pos = SLFind(&sl, x);
	if (pos != -1)
	{
		SLErase(&sl, pos);
	}
	SLPrint(&sl);
	SLDestroy(&sl);
}

void TestSeqList6()
{
	SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPushBack(&sl, 5);
	SLPrint(&sl);
	SLModify(&sl, 2, 20);
	sl.a[2] = 20;
	SLPrint(&sl);
	/*int x;
	scanf("%d", &x);
	int pos = SLFind(&sl, x);
	if (pos != -1)
	{
		SLModify(&sl, pos, x*10);
	}
	SLPrint(&sl);*/
	int pos, x;
	scanf("%d%d", &pos, &x);
	//sl.a[pos] = x;
	SLModify(&sl, pos, x);
	SLPrint(&sl);
	SLDestroy(&sl);
}

void TestSeqList7()
{
	/*SL* sl = NULL;
	SLInit(sl);
	SLPushBack(sl, 1);
	SLPushBack(sl, 2);
	SLPushBack(sl, 3);
	SLPushBack(sl, 4);
	SLPushBack(sl, 5);
	SLPrint(sl);*/
	SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPushBack(&sl, 5);
	SLPrint(&sl);
	SLPopFront(&sl);
	SLDestroy(&sl);
}
//int main()
//{
//	TestSeqList7();
//
//	return 0;
//}
	return 0;
}

💘不知不觉,【数据结构初阶】顺序表以告一段落。通读全文的你肯定收获满满,让我们继续为数据结构学习共同奋进!!!

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年12月12日
下一篇 2023年12月12日

相关推荐