[Algorithm and Data Structure (C Language)] Linear Table ----- Sequential Table

Table of contents

foreword

1. Types and definitions of linear tables

Second, the sequence table of the linear table

1. Concept and structure

2. Algorithm implementation 

1) Static sequence table:

2) Dynamic sequence table:

3) Interface implementation

at last


foreword

The linear table is divided into two parts. The content of this article first describes the type and definition of the sequence table, as well as the conceptual structure, classification and function declaration of the linear table, and the algorithm implementation. The conceptual structure, classification and function declaration of the linked list are due to the space The problem will be reflected in detail in the next article.

The following content is for reference only, and you are welcome to criticize and correct me~


提示:以下是本篇文章正文内容,下面案例可供参考

1. Types and definitions of linear tables

A linear list is a finite sequence of n data elements with the same properties.

Common linear tables are: sequence list, linked list, stack, queue, string... A linear table is logically a linear structure, that is to say, a continuous straight line. However, the physical structure is not necessarily continuous . When the linear table is physically stored, it is usually stored in the form of an array and a chain structure.

Second, the sequence table of the linear table

1. Concept and structure

The sequence table is a linear structure in which data elements are sequentially stored in a storage unit with continuous physical addresses , and is generally stored in an array . Add, delete, check and modify data on the array.

2. Algorithm implementation 

1) Static sequence table:

Use a fixed-length array to store elements.

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

typedef struct SeqList
{
    SLDataType array[N];    //定长数组,只能开辟宏定义中N大小的空间
    size_t size;    //有效数据的个数
}SeqList;

2) Dynamic sequence table:

Use dynamically allocated array storage. (Insufficient space can be expanded)

//顺序表的动态存储
typedef struct SeqList
{
    SLDataType* array;    //指向动态开辟的数组
    size_t size;    //有效数据的个数
    size_t capacity;    //空间容量的大小
}SeqList;

The static sequence table is only suitable for scenarios where you know how much data needs to be stored. The fixed-length array of the static sequence table causes N to be fixed, and the space is wasted if it is opened too much, and it is not enough if it is opened too little. Therefore, in reality, the dynamic sequence table is basically used, and the space is allocated dynamically according to the needs.

3) Interface implementation

typedef int SLDataType;
// 顺序表的动态存储
typedef struct SeqList
{
  SLDataType* array;  // 指向动态开辟的数组
  size_t size ;       // 有效数据个数
  size_t capicity ;   // 容量空间的大小
}SeqList;

// 基本增删查改接口

// 顺序表初始化
void SeqListInit(SeqList* ps);

// 检查空间,如果满了,进行增容
void CheckCapacity(SeqList* ps);

// 顺序表尾插
void SeqListPushBack(SeqList* ps, SLDataType x);

// 顺序表尾删
void SeqListPopBack(SeqList* ps);

// 顺序表头插
void SeqListPushFront(SeqList* ps, SLDataType x);

// 顺序表头删
void SeqListPopFront(SeqList* ps);

// 顺序表查找
int SeqListFind(SeqList* ps, SLDataType x); 

// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDataType x);

// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos);

// 顺序表销毁
void SeqListDestory(SeqList* ps);

// 顺序表打印
void SeqListPrint(SeqList* ps);

void SeqListInit(SL* ps);

void SeqListInit(SeqList* ps)
{
	assert(ps);

	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

void CheckCapacity(SeqList* ps)

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

	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity*sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}

		ps->a = tmp;
		ps->capacity = newCapacity;
	}
}

void SeqListPushBack(SeqList* ps,SLDataType x);

void SeqListPushBack(SeqList* ps, SLDataType x)
{
	assert(ps);

	// 扩容 : 21:20
	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity*sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}

		ps->a = tmp;
		ps->capacity = newCapacity;
	}

	ps->a[ps->size] = x;
	ps->size++;
}

void SeqListPopBack(SeqList* ps)

void SeqListPopBack(SeqList* ps)
{
	assert(ps);

	// 温柔的检查
	/*if (ps->size == 0)
	{
	return;
	}*/

	// 暴力的检查
	assert(ps->size > 0);

	//ps->a[ps->size - 1] = 0;
	ps->size--;
}

void SeqListPushFront(SeqList* ps)

void SeqListPushFront(SeqList* 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++;

	SeqListInsert(ps, 0, x);
}

void SeqListPopFront(SeqList* ps)

void SeqListPopFront(SeqList* ps)
{
	/*assert(ps);
	assert(ps->size > 0);

	int begin = 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}

	ps->size--;*/

	SeqListErase(ps, 0);
}

int SeqListFind(SeqList* ps, SLDataType x, int begin)

int SeqListFind(SeqList* ps, SLDataType x, int begin)
{
	assert(ps);

	for (int i = begin; i < ps->size; ++i)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}

	return -1;
}

void SLInsert(SL* ps, int pos, SLDataType x)

void SeqListInsert(SeqList* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0);
	assert(pos <= ps->size);

	CheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}

	ps->a[pos] = x;
	ps->size++;
}

void SeqListErase(SeqList* ps, int pos)

void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	assert(pos >= 0);
	assert(pos < ps->size);
	//assert(ps->size > 0);
	
	// 挪动数据覆盖
	int begin = pos + 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}

	ps->size--;
}

void SeqListDestroy(SeqList* ps)

void SeqListDestroy(SeqList* ps)
{
	assert(ps);

	//if (ps->a != NULL)
	if (ps->a)
	{
		free(ps->a);
		ps->a = NULL;
		ps->size = ps->capacity = 0;
	}
}

void SeqListPrint(SeqList* ps)

void SeqListPrint(SeqList* ps)
{
	assert(ps);

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

at last

Happy time is always short. The above is what I want to talk about today. This article continues to briefly introduce Comrade Xiao Zhao's preliminary understanding of algorithms and data structures (C language) and linear tables. Family members are welcome to criticize and correct. Comrade Xiao Zhao continues to update, the motivation for continuous learning is the support of Baozi with one button and three consecutive links~

 

Guess you like

Origin blog.csdn.net/weixin_70411664/article/details/127939887