[Data structure] - linear table + sequence table


foreword

All failures are God's test of whether you really love
this chapter is about sequential lists and linked lists in data structures


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

1. Linear table

A linear list is a finite sequence of n data elements with the same properties. A linear table is a data structure widely used in practice. Common linear tables: sequence table, 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.

2. Sequence table

2.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.
Sequence tables can generally be divided into:

  • Static sequence table: use fixed-length array to store elements
    insert image description here
  • Dynamic sequence table: use dynamically opened array storage
    insert image description here

2.2 Interface implementation

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

#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#define N 10
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 SLPushBack(SL* ps,SLDatatype x);
// 顺序表头插
void SLPushFornt(SL* ps, SLDatatype x);
// 顺序表尾删
void SLPopBack(SL* ps);
// 顺序表头删
void SLPopFornt(SL* ps);
// 顺序表在pos位置插入x
void SLInsert(SL* ps, int pos, SLDatatype x);//中间插入在pos位置插入
// 顺序表删除pos位置的值
void SLErase(SL* ps, int pos);//中间删除在pos位置删除
// 顺序表查找
int SLFind(SL* ps, SLDatatype x);//查找 找到返回下标,没有找到返回-1
// 顺序表修改
void SLModity(SL* ps, int pos, SLDatatype x);

2.3 Specific implementation

For specific analysis, please refer to the detailed explanation of the address book~
link

// 顺序表初始化
void SLInit(SL* ps)
{
    
    
	ps->a = (SLDatatype*)malloc(sizeof(SLDatatype) * 4);
	if (ps->a == NULL)
	{
    
    
		perror("malloc fail");
		return;
	}
	ps->capacity = 4;
	ps->size = 0;
}
// 检查空间,如果满了,进行增容
void SLCheckCapacity(SL* ps)
{
    
    
	if (ps->size  == ps->capacity)
	{
    
    
		SLDatatype* tmp = (SLDatatype*)realloc(ps->a, sizeof(SLDatatype) * ps->capacity * 2);
		if (tmp == NULL)
		{
    
    
			perror("realloc fail");
			return;
		}
		ps->a =tmp;
		ps->capacity *= 2;
	}
}
// 顺序表打印
void SLPrint(SL* ps)
{
    
    
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
    
    
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
// 顺序表尾插
void SLPushBack(SL* ps, SLDatatype x)//尾插
{
    
    
	SLCheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
	//ps->a[ps->size++] = x;
}
// 顺序表头插
void SLPushFornt(SL* ps, SLDatatype x)//头插
{
    
    
	SLCheckCapacity(ps);
	//依次从后往前移动数据
	int end = ps->size -1;
	while (end >= 0)
	{
    
    
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}
// 顺序表尾删
void SLPopBack(SL* ps)
{
    
    
	assert(ps->size>0);
	ps->size--;
}
// 顺序表头删
void SLPopFornt(SL* ps)
{
    
    
	assert(ps->size > 0);
	//从前往后移动防止覆盖
	int start = 0;
	while (start < ps->size - 1)
	{
    
    
		ps->a[start] = ps->a[start + 1];
		start++;
	}
	ps->size--;
}
// 顺序表在pos位置插入x
void SLInsert(SL* ps, int pos, SLDatatype x)
{
    
    
	assert(0 <= pos && 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(0 <= pos && pos <= ps->size);
	int start = pos + 1;
	while (start < ps->size)
	{
    
    
		ps->a[start - 1] = ps->a[start];
		start++;
	}
	ps->size--;
}
// 顺序表查找
int SLFind(SL* ps, SLDatatype x)
{
    
    
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
    
    
		if (ps->a[i] == x)
			return i;
	}
	return -1;
}
// 顺序表修改
void SLModity(SL* ps, int pos, SLDatatype x)
{
    
    
	assert(0 <= pos && pos <= ps->size);
	ps->a[pos] = x;
}
// 顺序表销毁
void SLDestroy(SL* ps)
{
    
    
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}

Summarize

Ending, this is the end of today's linear table + sequence table (Part 1). If you want to know more in the future, please follow me. One-click three links~

Guess you like

Origin blog.csdn.net/ljq_up/article/details/130114810