sequence table|data structure

Know the data structure

1. What is a data structure?

Data structure is a way for computers to store and organize data, and refers to a collection of data elements that have one or more specific relationships with each other.

2. What is an algorithm?

Algorithm: It is a well-defined calculation process that takes one or a set of values ​​as input and produces one or a set of values ​​as output
. Simply put, an algorithm is a series of computational steps used to transform input data into output results.

Getting Started with Data Structures

1. Sequence table

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

Sequence tables can generally be divided into:
1. Static sequence table: use fixed-length arrays to store elements.
insert image description here
It is a relatively limited size, and it is relatively limited for later use.
2. Dynamic sequence table: use dynamically opened array storage.

insert image description here

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. So in reality, the dynamic sequence table is basically used, and the space is dynamically allocated according to the needs, so we implement the dynamic sequence table below.

2. Realize the dynamic sequence table

For the data structure, it is used more than adding, deleting, inserting and searching . These interfaces can be implemented one by one.

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
//
typedef int SLDataType;

typedef struct SeqList
{
    
    
	SLDataType* a;
	int size;       // 记录存储多少个有效数据
	int capacity;   // 空间容量大小 
}SL;

//打印
void SLPrint(SL* ps);
//初始化
void SLInit(SL* ps);
//销毁
void SLDestroy(SL* ps);
//尾插
void SLPushBack(SL* ps, SLDataType x);
//尾删
void SLPopBack(SL* ps);
// 头插头删
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);

function implementation

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

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

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


void SLPushBack(SL* ps, SLDataType x)
{
    
    
	assert(ps);
	//检查空间是否足够
	if (ps->size == ps->capacity)
	{
    
    
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* temp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		if (temp == NULL)
		{
    
    
			perror("realloc fail");
			exit(-1);
		}
		ps->a = temp;
		ps->capacity=newcapacity;
	}
	ps->a[ps->size] = x;
	ps->size++;
}

void SLPopBack(SL* ps)
{
    
    
	assert(ps);
	if (ps->size>0)
		ps->size--;
}

void SLPushFront(SL* ps, SLDataType x)
{
    
    
	int end = ps->size - 1;
	while (end>=0)
	{
    
    
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}

void SLPopFront(SL* ps)
{
    
    
	assert(ps->size>0);
	//头删需要注意大小,空表头出现
	int begin = 1;
	while (ps->size > begin)
	{
    
    
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}
	ps->size--;
}

Summarize

Sequence tables and arrays are inseparable, which means that it has relatively large space requirements. With these expansions, it is necessary to apply for new space, copy data, and release old space. There will be a lot of consumption.
The capacity increase is generally a 2-fold increase, and there is bound to be a certain amount of space wasted. For example, the current capacity is 100, and when it is full, it will be increased to 200. We continue to insert 5 more data, and no data will be inserted later, so 95 data spaces will be wasted. More space wasted.

Guess you like

Origin blog.csdn.net/github_73587650/article/details/128096563