Sequence table of data structure

Posted by MartiniMan on Mon, 01 Nov 2021 14:43:33 +0100

catalogue

What is a linear table?

Sequence table

Static sequence table

Dynamic sequence table

text.c

Seqlist.h

Seqlist.c

What is a linear table?

A linear list is a finite sequence of n data elements with the same characteristics. Linear table is a data structure widely used in practice. Common linear tables: sequential table, linked list, stack, queue, string

A linear table is logically a linear structure, that is, a continuous straight line. However, the physical structure is not necessarily continuous. When the linear table is stored physically, it is usually stored in the form of array and chain structure.

 

 

Today we mainly introduce the sequence table.

Sequence table

Sequential table is a linear structure in which data elements are stored in sequence with a storage unit with continuous physical addresses. Generally, array storage is used. Complete the addition, deletion, query and modification of data on the array.

Sequence table is divided into static sequence table and dynamic sequence table

Static sequence table

Static sequential table: uses a fixed length array to store elements.

//Static storage of sequential tables
#define N 7
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType array[N];//Fixed length array
	size_t size;        //Number of valid data
}SeqList;

Static sequence table is to open up a certain size of array space in advance, and change the size of the array by modifying the value of N defined by the macro in the program. In fact, it is the array we usually use. But it is reproduced by structure.

Let me focus on the dynamic sequence table.

Dynamic sequence table

Dynamic sequential table: use dynamic array storage.

//Dynamic storage of sequential tables
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* array;   //Point to a dynamic array
	size_t size;         //Number of valid data
	size_t capicity;     //Size of capacity space
}SeqList;

The static sequence table is only applicable to the scenario where you know how much data needs to be stored. The fixed length array of static sequence table leads to large N, waste of space and insufficient space. Therefore, in reality, dynamic sequential tables are basically used to dynamically allocate space according to needs, so let's implement dynamic sequential tables below.

typedef int SLDataType;
struct SeqList
{
	SLDataType* data;
	unsigned size;
	unsigned capacity;
};
// Data management: addition, deletion, query and modification
//Check the space. When the space is full, increase the space
void SeqCheckAdd(struct SeqList* ps);
//Initialization of sequence table
void SeqListInit(struct SeqList* ps);
//Destruction of sequence table
void SeqListDestory(struct SeqList* ps);
//Print sequence table
void SeqListPrint(struct SeqList* ps);
//Tail insertion
void SeqListPushBack(struct SeqList* ps, SLDataType x);
//Head insert
void SeqListPushFront(struct SeqList* ps, SLDataType x);
//Header deletion
void SeqListPopFront(struct SeqList* ps);
//Tail deletion
void SeqListPopBack(struct SeqList* ps);

// Sequential table lookup
int SeqListFind(struct SeqList* ps, SLDataType x);
// The sequence table inserts an x at the pos position
void SeqListInsert(struct SeqList* ps, size_t pos, SLDataType x);
// The sequence table deletes the value of pos position
void SeqListErase(struct SeqList* ps, size_t pos);

The SeqListInit function initializes the members of the structure variable. The pointer data points to NULL, and the member size and capacity are equal to 0;

The SeqCheckAdd function is used to check the sequential table space. If the sequence table has not opened up space, open up four SLDataType spaces first. If the sequence table has opened up space, check whether the size is equal to capacity, that is, whether the current number of data has reached the upper limit of the sequence table, and use realloc to open up space.

Other functions of the sequence table will not be introduced one by one, because the sequence table is not much different from the array except for dynamically opening up space.

Next, I'll post the code of the dynamic sequence table below.

text.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "Seqlist.h"
//Addition, deletion, query and modification of sequence table
enum
{
	Destory,
	PushBack,
	PushFront,
	PopBack,
	PopFront,
	ListFind,
	ListInsert,
	ListErase,
	ListPrint
};
void menu()
{
	printf("****1,PushBack   2,PushFront****\n");
	printf("****3,PopBack    4,PopFront ****\n");
	printf("****5,ListFind   6,ListInsert***\n");
	printf("****7,ListErase  8,ListPrint****\n");
	printf("****0,Destory                ****\n");
}
int main()
{
	int input = 0;
	int x = 0;
	int pos;
	struct SeqList ps;
	SeqListInit(&ps);
	do
	{
		menu();
		printf("Please select:>");
		scanf("%d", &input);
		switch (input)
		{
		case PushBack:
			printf("Please enter the number to add:>");
			scanf("%d", &x);
			SeqListPushBack(&ps, x);
			break;
		case PushFront:
			printf("Please enter the number to add:>");
			scanf("%d", &x);
			SeqListPushFront(&ps, x);
			break;
		case PopBack:
			SeqListPopBack(&ps);
			break;
		case PopFront:
			SeqListPopFront(&ps);
			break;
		case ListFind:
			printf("Please enter the number you want to find:>");
			scanf("%d", &x);
			if (SeqListFind(&ps, x) != 0)
			{
				printf("Yes, the location is%d\n", SeqListFind(&ps, x) + 1);
			}
			else
			{
				printf("Can't find!");
			}
			break;
		case ListInsert:
			printf("Please enter the location:>");
			scanf("%d", &pos);
			printf("Please enter the number to add:>");
			scanf("%d", &x);
			SeqListInsert(&ps, pos, x);
			break;
		case ListErase:
			printf("Please enter the location:>");
			scanf("%d", &pos);
			SeqListErase(&ps, pos);
			break;
		case ListPrint:
			SeqListPrint(&ps);
			break;
		case Destory:
			SeqListDestory(&ps);
			break;
		default:
			printf("Input error, please re-enter!\n");
			break;
		}
	} while (input);
	return 0;
}

Seqlist.h

typedef int SLDataType;
struct SeqList
{
	SLDataType* data;
	unsigned size;
	unsigned capacity;
};
// Data management: addition, deletion, query and modification
void SeqCheckAdd(struct SeqList* ps);
void SeqListInit(struct SeqList* ps);
void SeqListDestory(struct SeqList* ps);

void SeqListPrint(struct SeqList* ps);
void SeqListPushBack(struct SeqList* ps, SLDataType x);
void SeqListPushFront(struct SeqList* ps, SLDataType x);
void SeqListPopFront(struct SeqList* ps);
void SeqListPopBack(struct SeqList* ps);

// Sequential table lookup
int SeqListFind(struct SeqList* ps, SLDataType x);
// The sequence table inserts an x at the pos position
void SeqListInsert(struct SeqList* ps, size_t pos, SLDataType x);
// The sequence table deletes the value of pos position
void SeqListErase(struct SeqList* ps, size_t pos);

Seqlist.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "Seqlist.h"
void SeqCheckAdd(struct SeqList* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = (ps->capacity == 0) ? 4 : 2 * ps->capacity;
		int* newdata = realloc(ps->data, sizeof(SLDataType) * newcapacity);
		if (newdata == NULL)
		{
			perror("realloc Failed");
			return;
		}
		ps->data = newdata;
		ps->capacity = newcapacity;
	}
}
//Initialization sequence table
void SeqListInit(struct SeqList* ps)
{
	ps->data = NULL;
	ps->size = 0;
	ps->capacity = 0;
}
void SeqListDestory(struct SeqList* ps)
{
	ps->data = 0;
	ps->capacity = 0;
	free(ps->data);
	ps->data = NULL;
}

void SeqListPrint(struct SeqList* ps)
{
	unsigned i = 0;
	for (i = 0; i < (ps->size); i++)
	{
		printf("%d ", ps->data[i]);
	}
	printf("\n");
}
void SeqListPushBack(struct SeqList* ps, SLDataType x)
{
	SeqCheckAdd(ps);
	ps->data[ps->size] = x;
	ps->size++;
}
void SeqListPushFront(struct SeqList* ps, SLDataType x)
{
	SeqCheckAdd(ps);
	unsigned i = 0;
	for (i = 0; i < ps->size; i++)
	{
		ps->data[ps->size - i] = ps->data[ps->size - 1 - i];
	}
	ps->data[0] = x;
	ps->size++;
}
void SeqListPopFront(struct SeqList* ps)
{
	unsigned i = 0;
	if (ps->size == 0)
	{
		printf("Pop Front Failed!\n");
	}
	else
	{
		for (i = 0; i < ps->size - 1; i++)
		{
			ps->data[i] = ps->data[i + 1];
		}
		ps->size--;
	}
}
void SeqListPopBack(struct SeqList* ps)
{
	if (ps->size == 0)
	{
		printf("Pop Back Failed!\n");
	}
	else
	{
		ps->size--;
	}
}

// Sequential table lookup
int SeqListFind(struct SeqList* ps, SLDataType x)
{
	unsigned i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->data[i] == x)
		{
			return i;
		}
	}
	return 0;
}
// The sequence table inserts an x at the pos position
void SeqListInsert(struct SeqList* ps, size_t pos, SLDataType x)
{
	if (pos<1 || pos>ps->size + 1)
	{
		printf("List Insert Failed!\n");
	}
	else
	{
		SeqCheckAdd(ps);
		unsigned i = 0;
		for (i = 0; i < ps->size - pos + 1; i++)
		{
			ps->data[ps->size - i] = ps->data[ps->size - 1 - i];
		}
		ps->data[pos - 1] = x;
		ps->size++;
	}
}
// The sequence table deletes the value of pos position
void SeqListErase(struct SeqList* ps, size_t pos)
{
	if (pos<1 || pos>ps->size)
	{
		printf("List Erase Failed!\n");
	}
	else
	{
		unsigned i = 0;
		for (i = 0; i < ps->size - pos; i++)
		{
			ps->data[pos - 1 + i] = ps->data[pos + i];
		}
		ps->size--;
	}
}

Topics: data structure linked list list