Technology Sharing

Data Structure: Implementing Sequential Tables in C

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Sequence table

A sequential list is a type of linear list, a data structure used to store a finite sequence of n data elements with the same characteristics.
The logical structure of the sequential table storage format is continuous, and the physical structure is also continuous. The underlying layer is an array to complete the data storage.

1. Static sequence table

The space of a static sequence table is fixed, the size of the array is fixed, and the number of data stored is limited, so the space set for static data will be too large. If it is too small, it will not be enough, but if it is too large, space will be wasted.

2. Dynamic sequence table

The dynamic sequence table adjusts the space size according to the memory space requirements, which greatly reduces the waste of space. Therefore, the sequence table is implemented in the form of a dynamic sequence.

2. Dynamic Sequence Table Implementation

To implement a dynamic sequence table, three files will be created:

  • 1. The header file of squence_list.h, declares functions and defines
  • 2. The source file of sequence_list.c implements the function
  • 3. The source file test.c is used to test the function of the sequence table

insert image description here

1. Create custom types

Once created, implement it step by step
1. In the file of sequence_list.h, first create the custom type of structure used by the sequence list

#pragma once
//sequence_list.h文件中

#include<stdio.h>

//顺序表存放数据的类型
typedef int SLDATETYPE;

//顺序表的类型创建
typedef struct SequenceList
{
	//存放指定类型数据的指针
	SLDATETYPE* a;

	//存放数据的有效个数
	int count;

	//属性表空间大小
	int size;
}SL;//重命名一个简单的名字
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
2. Complete the creation of the sequence table and test the functional requirements

2. Create a sequence table in the test.c file, then see what functions are needed, and then implement the functions of each corresponding function.

//在test.c文件中

//1、首先包含头文件
#include"sequence_list.h"

//3、测试函数test1的任务
void test1()
{
	//4、创建一个顺序表
	SL s;

	//5、顺序表没有初始化需要初始化
	SLInitialize(&s);//传址调用

	//6、最后不使用时要完成销毁
	SLDestroy(&s);
}

//2、创建主函数
int main()
{
	//调用测试函数
	test1();
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
3. Complete the initialization and destruction functions of the sequence table

3. Complete the corresponding function declaration and implementation of the header files of squence_list.h and squence_list.c according to the needs

  • (1) Sequence table initialization
  • (2) Sequence table destruction
#pragma once
//sequence_list.h文件中
//包含检查头文件
#include<assert.h>

//动态内存开辟函数对应的头文件
#include<stdlib.h>

//顺序表的初始化
void SLInitialize(SL* ps);

//顺序表的销毁
void SLDestroy(SL* ps);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
//在squence_list.c文件中

//包含头文件
#include"sequence_list.h"

//顺序表的初始化
void SLInitialize(SL* ps)
{
	//对顺序表检查
	//要添加对应的<assert.h>头文件
	//在sequence_list.h头文件包含就可以了
	assert(ps != NULL);
	ps->a = NULL;
	ps->count = ps->size = 0;
}

//顺序表的销毁
void SLDestroy(SL* ps)
{
	assert(ps != NULL);
	
	//动态内存开辟的空间要free
	//动态内存开辟函数要添加头文件<stdlib.h>

	free(ps->a);
	ps->a = NULL;
	ps->count = ps->size = 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
4. Insert data and print data in the sequence table

4. Continue testing the test.c file
The function required is a function to add data
There are three ways to add data:

  • (1) Add at the end
  • (2) Header addition
  • (3) Add at any location

Need to observe data, use the print function to achieve

//在test.c文件中

//1、首先包含头文件
#include"sequence_list.h"

//3、测试函数test1的任务
void test1()
{
	//4、创建一个顺序表
	SL s;

	//5、顺序表没有初始化需要初始化
	SLInitialize(&s);//传址调用

	//添加数据
	//7、尾部添加数据
	SLPutEnd(&s, 1);
	SLPutEnd(&s, 2);
	SLPutEnd(&s, 3);

	//8、打印数据
	SLPrint(&s);

	//9、头部插入
	SLPutFirst(&s, 5);
	SLPutFirst(&s, 4);

	SLPrint(&s);

	//10、任意位置前插入数据
	SLInsert(&s, 0, 3);
	SLInsert(&s, s.count-1, 6);
	SLInsert(&s, 1, 6);
	SLPrint(&s);

	
	//6、最后不使用时要完成销毁
	SLDestroy(&s);
}

//2、创建主函数
int main()
{
	//调用测试函数
	test1();
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
//sequence_list.h文件中

//判断空间是否足够
void SLEnough(SL* ps)
{
	//空间满了申请空间
	if (ps->count == ps->size)
	{
		int n;
		n = (ps->size == 0 ? 4 : ps->size * 2);
		SLDATETYPE* tmp = (SLDATETYPE*)realloc(ps->a,n * sizeof(SLDATETYPE));
		if (tmp == NULL)
		{
			//开辟失败退出
			exit(1);
		}
		ps->a = tmp;
		tmp = NULL;
		ps->size = n;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

//顺序表末尾放置数据
void SLPutEnd(SL* ps, SLDATETYPE x)
{
	assert(ps != NULL);
	//判断空间是否足够容纳数据
	SLEnough(ps);
	ps->a[ps->count++] = x;
}


//打印顺序表中的数据
void SLPrint(SL* ps)
{
	assert(ps != NULL);
	int i = 0;
	for (i = 0; i < ps->count; i++)
	{
		printf("%d", ps->a[i]);
	}
	printf("n");
}

//顺序表头部插入数据
void SLPutFirst(SL* ps, SLDATETYPE x)
{
	assert(ps != NULL);
	SLEnough(ps);
	for (int i = ps->count; i > 0; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[0] = x;
	ps->count++;
}

//顺序表任意位置前插入数据
void SLInsert(SL* ps, int pos, SLDATETYPE x)
{
	assert(ps != NULL);
	assert(pos >= 0 && pos <= ps->count);
	SLEnough(ps);
	for (int i = ps->count; i > pos; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[pos] = x;
	ps->count++;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
5. Delete data

(1) Tail deletion
(2) Header deletion
(3) Delete at any position
(4) Find the corresponding data subscript

3. Sequence table completes the final code

Code in the test.c file: used to test the feasibility of the program
//在test.c文件中

//1、首先包含头文件
#include"sequence_list.h"

//3、测试函数test1的任务
void test1()
{
	//4、创建一个顺序表
	SL s;

	//5、顺序表没有初始化需要初始化
	SLInitialize(&s);//传址调用

	//添加数据
	//7、尾部添加数据
	SLPutEnd(&s, 1);
	SLPutEnd(&s, 2);
	SLPutEnd(&s, 3);


	//8、打印数据
	//SLPrint(&s);

	//9、头部插入
	SLPutFirst(&s, 4);

	//SLPrint(&s);

	//10、任意位置前插入数据
	SLInsert(&s, 0, 5);
	SLInsert(&s, 0, 6);
	
	SLPrint(&s);

	//11、尾部删除数据
	SLDeleteEnd(&s);

	//l2、头部删除数据
	SLDeleteFirst(&s);

	//13、任意位置删除数据
	SLDelete(&s, 0);

	SLPrint(&s);
	//14、查找数据对应下标
	int ret=SLFind(&s, 6);
	if (ret >= 0)
	{
		printf("找到了,下标为%dn",ret);
	}
	else
		printf("没有找到");
	//6、最后不使用时要完成销毁
	SLDestroy(&s);
}

//2、创建主函数
int main()
{
	//调用测试函数
	test1();
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
sequence_list.h header file:
#pragma once
//sequence_list.h文件中

#include<stdio.h>

//包含检查头文件
#include<assert.h>

//动态内存开辟函数对应的头文件
#include<stdlib.h>


//顺序表存放数据的类型
typedef int SLDATETYPE;

//顺序表的类型创建
typedef struct SequenceList
{
	//存放指定类型数据的指针
	SLDATETYPE* a;

	//存放数据的有效个数
	int count;

	//属性表空间大小
	int size;
}SL;//重命名一个简单的名字


//顺序表的初始化
void SLInitialize(SL* ps);

//顺序表的销毁
void SLDestroy(SL* ps);

//判断空间是否足够
void SLEnough(SL* ps);

//顺序表末尾放置数据
void SLPutEnd(SL* ps, SLDATETYPE x);

//打印顺序表中的数据
void SLPrint(SL* ps);

//顺序表头部插入数据
void SLPutFirst(SL* ps, SLDATETYPE x);

//顺序表任意位置前插入数据
void SLInsert(SL* ps, int pos, SLDATETYPE x);

//顺序表尾删
void SLDeleteEnd(SL* ps);

//顺序表头删
void SLDeleteFirst(SL* ps);

//顺序表任意位置删除数据
void SLDelete(SL* ps, int pos);

//顺序表中寻找对应数据下标
int SLFind(SL* ps, SLDATETYPE x);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
sequence_list.c file:
#define _CRT_SECURE_NO_WARNINGS 1

//在squence_list.c文件中

//包含头文件
#include"sequence_list.h"

//顺序表的初始化
void SLInitialize(SL* ps)
{
	//对顺序表检查
	//要添加对应的<assert.h>头文件
	//在sequence_list.h头文件包含就可以了
	assert(ps != NULL);
	ps->a = NULL;
	ps->count = ps->size = 0;
}

//顺序表的销毁
void SLDestroy(SL* ps)
{
	assert(ps != NULL);
	
	//动态内存开辟的空间要free
	//动态内存开辟函数要添加头文件<stdlib.h>

	free(ps->a);
	ps->a = NULL;
	ps->count = ps->size = 0;
}

//判断空间是否足够
void SLEnough(SL* ps)
{
	//空间满了申请空间
	if (ps->count == ps->size)
	{
		int n;
		n = (ps->size == 0 ? 4 : ps->size * 2);
		SLDATETYPE* tmp = (SLDATETYPE*)realloc(ps->a,n * sizeof(SLDATETYPE));
		if (tmp == NULL)
		{
			//开辟失败退出
			exit(1);
		}
		ps->a = tmp;
		tmp = NULL;
		ps->size = n;
	}
}

//顺序表末尾放置数据
void SLPutEnd(SL* ps, SLDATETYPE x)
{
	assert(ps != NULL);
	//判断空间是否足够容纳数据
	SLEnough(ps);
	ps->a[ps->count++] = x;
}


//打印顺序表中的数据
void SLPrint(SL* ps)
{
	assert(ps != NULL);
	int i = 0;
	for (i = 0; i < ps->count; i++)
	{
		printf("%d", ps->a[i]);
	}
	printf("n");
}

//顺序表头部插入数据
void SLPutFirst(SL* ps, SLDATETYPE x)
{
	assert(ps != NULL);
	SLEnough(ps);
	for (int i = ps->count; i > 0; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[0] = x;
	ps->count++;
}

//顺序表任意位置前插入数据
void SLInsert(SL* ps, int pos, SLDATETYPE x)
{
	assert(ps != NULL);
	assert(pos >= 0 && pos <= ps->count);
	SLEnough(ps);
	for (int i = ps->count; i > pos; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[pos] = x;
	ps->count++;
}

//顺序表尾删
void SLDeleteEnd(SL* ps)
{
	assert(ps != NULL);
	if (ps->count == 0)
	{
		printf("数据已经删完了n");
	}
	else
	{
		ps->count--;
		printf("删除成功n");
	}
}

//顺序表头删
void SLDeleteFirst(SL* ps)
{
	assert(ps != NULL);
	if (ps->count == 0)
	{
		printf("数据已经删完了n");
	}
	else
	{
		for (int i = 0; i < ps->count - 1; i++)
		{
			ps->a[i] = ps->a[i + 1];
		}
		ps->count--;
		printf("删除成功n");
	}
}

//顺序表任意位置删除数据
void SLDelete(SL* ps, int pos)
{
	assert(ps != NULL);
	assert(pos >= 0 && pos <= ps->count);
	if (ps->count == 0)
	{
		printf("数据已经删完了n");
	}
	else
	{
		for (int i = pos; i < ps->count - 1; i++)
		{
			ps->a[i] = ps->a[i + 1];
		}
		ps->count--;
		printf("删除成功n");
	}
}

//顺序表中寻找对应数据下标
int SLFind(SL* ps, SLDATETYPE x)
{
	assert(ps != NULL&&ps->a != NULL);
	for (int i = 0; i < ps->count; i++)
	{
		if (ps->a[i] == x)
		{
			return 1;
		}
	}
	return -1;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167