Data structure - sequence table (3)

data structure

1. What is a sequence table

insert image description hereThe 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.
We know that the data in the array is 0, 1, 2, 3, it is impossible to appear 0, 1, 2, 4, there is no 4 without 3

Sequence tables can generally be divided into:

  1. Static sequence table: use a fixed-length array to store elements.
    When the address book was first implemented, 1000 were defined.
  2. Dynamic sequence table: use dynamically opened array storage.
    dynamic address book

Second, the creation of sequence table

Address, capacity, length
define a structure variable

1. Static sequence table

The static sequence table uses a fixed-length array to store elements. If the space is small, it is not enough, and if the space is too large, it is too wasteful, so the static sequence table is not practical.

struct SeqListInit
{
    
    
int *data;//地址
int length;//容量
};
#define N 100
typedef int SeqListDataType;//定义为SeqListDataType方便使用
typedef struct SeqList
{
    
    
	SeqListDataType a[N];//存储数据的数组
	int size;//顺序表中当前有效数据的个数
}SeqList;

2. Dynamic data table

typedef int SeqListDataType;
typedef struct SeqList
{
    
    
	SeqListDataType  *a;//存储数据的数组(动态开辟)
	int size;//顺序表中当前有效数据的个数
	int capacity;//顺序表中最大存储的个数
}SeqList;

3. Initialization and destruction of the sequence table

The dot (.) is used to access members of structure variables, and the arrow (->) is used to access members of structure pointers.

//初始化
void SeqListInit(SeqList *p)
{
    
    
	assert(p);//用assert来防止传入空指针使程序崩掉
	p->a = NULL;//将指向动态开辟的数组置为NULL。
	p->size = 0;
	p->capacity = 0;
}
//销毁
void SeqListDestroy(SeqList *p)
{
    
    
	assert(p != NULL);
	free(p->a);//释放动态开辟的空间
	p->a = NULL;
	p->size = 0;
	p->capacity = 0;
}

Fourth, the insertion of the sequence table

Judge before inserting

//判断线性表是否已满
void CheckCapacity(SeqList* p)
{
    
    
	if (p->size == p->capacity)
	{
    
    
		int newcapacity = (p->capacity == 0) ? 4 : (p->capacity * 2);
		//如果容量为0(第一次插入数据),默认开辟4个SeqListDataType的空间
		//如果是在插入数据过程中顺序表已满,则将空间变为2倍(将空间变为2倍也可能会出现空间浪费的情况)
		//这里扩容的倍数需要具体情况具体分析,此处以2倍为例
		SeqListDataType* newA = (SeqListDataType*)realloc(p->a, sizeof(SeqListDataType)*newcapacity);
		if (newA == NULL)//开辟失败
		{
    
    
			printf("newcapacity fail\n");
			return;
		}
		p->a = newA;
		p->capacity = newcapacity;
	}
}

1. Tail plug

//尾插
void SeqListPushBack(SeqList* p, SeqListDataType x)
{
    
    
	assert(p);
	CheckCapacity(p);//当尾部空间不够时,就会自动扩容。
	p->a[p->size] = x;
	//p->size表示当前顺序表中有效的数据个数
	//a[p->size]访问当前最后一个数据的下一个位置
	p->size++;//注意此处要让有效数据的个数+1
}

2. Plug

Before inserting data at the head, all data needs to be moved back one bit to free up the position of the first data to insert new data.
Note: The data must be moved sequentially from the back to the front, otherwise the data of the previous bit will overwrite the data of the next bit, resulting in data loss.

void SeqListPushFront(SeqList* p, SeqListDataType x)
{
    
    
	assert(p);
	CheckCapacity(p);
	int end = p->size;
	for (; end > 0; end--)
	{
    
    
		p->a[end] = p->a[end - 1];//从后向前挪动数据
	}
	p->a[0] = x;
	p->size++;//注意此处要让有效数据的个数+1
}

3. Arbitrary insertion

//中间插入数据
void SeqListInsert(SeqList* p, int pos, SeqListDataType x)
{
    
    
	assert(p);
	assert(pos >= 0 && pos <= p->size);
	CheckCapacity(p);

	int i = p->size;
	for (; i > pos; i--)
		p->a[i] = p->a[i - 1];//向后挪动数据
	p->a[i] = x;
	p->size++;
}

Summarize

I used DEV for simple writing, and the standard operation is to write source files and .h files

insert image description here
code show as below

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define N 10
typedef int SeqListDataType;
typedef struct SeqList
{
    
    
	SeqListDataType  *a;//存储数据的数组(动态开辟)
	int size;//顺序表中当前有效数据的个数
	int capacity;//顺序表中最大存储的个数
}SeqList;

int SeqListInit(SeqList *p)
{
    
    
	assert(p);//用assert来防止传入空指针使程序崩掉
	p->a = (int *)malloc(sizeof(int) * N);//将指向动态开辟的数组置为NULL。
	p->size = 0;
	p->capacity = 0;
	return 1;
}
void SeqListDestroy(SeqList *p)
{
    
    
	assert(p != NULL);
	free(p->a);//释放动态开辟的空间
	p->a = NULL;
	p->size = 0;
	p->capacity = 0;
}
void CheckCapacity(SeqList* p)
{
    
    
	if (p->size == p->capacity)
	{
    
    
		int newcapacity = (p->capacity == 0) ? 4 : (p->capacity * 2);
		//如果容量为0(第一次插入数据),默认开辟4个SeqListDataType的空间
		//如果是在插入数据过程中顺序表已满,则将空间变为2倍(将空间变为2倍也可能会出现空间浪费的情况)
		//这里扩容的倍数需要具体情况具体分析,此处以2倍为例
		SeqListDataType* newA = (SeqListDataType*)realloc(p->a, sizeof(SeqListDataType)*newcapacity);
		if (newA == NULL)//开辟失败
		{
    
    
			printf("newcapacity fail\n");
			return;
		}
		p->a = newA;
		p->capacity = newcapacity;
	}
}
void SeqListPushBack(SeqList* p, SeqListDataType x)
{
    
    
	assert(p);
	CheckCapacity(p);//当尾部空间不够时,就会自动扩容。
	p->a[p->size] = x;
	//p->size表示当前顺序表中有效的数据个数
	//a[p->size]访问当前最后一个数据的下一个位置
	p->size++;//注意此处要让有效数据的个数+1
}
void SeqListPushFront(SeqList* p, SeqListDataType x)
{
    
    
	assert(p);
	CheckCapacity(p);
	int end = p->size;
	for (; end > 0; end--)
	{
    
    
		p->a[end] = p->a[end - 1];//从后向前挪动数据
	}
	p->a[0] = x;
	p->size++;//注意此处要让有效数据的个数+1
}
void SeqListInsert(SeqList* p, int pos, SeqListDataType x)
{
    
    
	assert(p);
	assert(pos >= 0 && pos <= p->size);
	CheckCapacity(p);

	int i = p->size;
	for (; i > pos; i--)
		p->a[i] = p->a[i - 1];//向后挪动数据
	p->a[i] = x;
	p->size++;
}
void SeqListprintf(SeqList* p)
{
    
    
	assert(p);
	int i=0;
	for(i=0;i<p->size;i++)
	printf("%d\n",p->a[i]);
}
int main()
{
    
    
	SeqList sl;
	int ret;
    ret=SeqListInit(&sl);
	if(1==ret)
	printf("顺序表创建成功\n");
	SeqListPushBack(&sl,1);
	SeqListPushBack(&sl,2);
	SeqListPushBack(&sl,3);
	SeqListPushBack(&sl,4);
	printf("尾插4321成功\n"); 
	SeqListprintf(&sl);
	SeqListPushFront(&sl,5);
	printf("头插5成功\n");
	SeqListprintf(&sl);
	SeqListInsert(&sl,2,99); 
	printf("第三个位置插入99成功\n");
	SeqListprintf(&sl);
	SeqListDestroy(&sl);
	printf("销毁\n");
	SeqListprintf(&sl);
	printf("销毁成功");
}

Guess you like

Origin blog.csdn.net/qq_51963216/article/details/128785298