Title C Language Implementation Order Table
Definition of Linear Table
A linear table is a finite sequence of n data elements with the same characteristics. Linear table is a data structure that is widely used in practice. Common linear tables are order table, chain table, stack, queue, string
A linear table is logically a linear structure, that is, a straight line. However, a linear table is not necessarily a continuous space in physical structure. When a linear table is physically stored, it is usually stored in the form of an array of explanations and a chain structure.
Definition of Sequential Table
Sequential tables use a storage unit with a continuous physical address to store the linear structure of the data elements in turn, generally using arrays. Complete data addition, deletion, and alteration on the array. They can be divided into static and dynamic order tables
1. Static Sequential Table: Store elements using a fixed-length array
2. Dynamic Sequence Table: Using Dynamically Opened Array Storage
Implementation of Sequential Table
The sequence table implements the following functions, where we implement the dynamic sequence table
#pragma once #include<stdio.h> #include<stdlib.h> #include<assert.h> #define INIT_SIZE 4 // Define data types for sequential tables typedef int SeqType; // Define the structure of a sequential table typedef struct Seqlist { SeqType* list; int size; int capacity; } Seqlist; // Initialization Sequence Table void SeqInit(Seqlist* sl); // End Interpolation Data void SeqListPushBack(Seqlist* sl, SeqType x); // Head Interpolation Data void SeqListPushFront(Seqlist* sl, SeqType x); // Censoring data void SeqListPopBack(Seqlist* sl); // Head Delete Data void SeqListPopFront(Seqlist* sl); // Clear Sequence Table void SeqListDistory(Seqlist* sl); // Sequence table lookup int SeqListFind(Seqlist* sl, SeqType x); // Sequence table inserts x at pos position void SeqListInsert(Seqlist* sl, size_t pos, SeqType x); // Sequence table deletes the value of pos position void SeqListErase(Seqlist* sl, size_t pos); // Show Order Table void SeqListPrint(Seqlist* sl); // Sequence Table Expansion void SeqListDilation(Seqlist* sl); // Determine if the order table is empty int SeqListIsEmpty(Seqlist* sl);
When we want to use a sequential table, we first create a variable Seqlist sl for the sequential table. With the variable, we have to initialize the sequential table, which is to initialize the members of the structure Seqlist.
// Initialization Sequence Table void SeqInit(Seqlist* sl) { sl->list = NULL; sl->size = 0; sl->capacity = 0; }
When working with a sequence table, you have to decide if it is empty
// Determine if the order table is empty int SeqListIsEmpty(Seqlist* sl) { assert(sl); if (sl->size == 0) { return 1; } else { return 0; } }
We know that the main operation of the sequence table is to add, delete, change, check, we will implement one by one below. Because the length of our order table is uncertain. That is, we first apply for a space in memory for it when we start using it. When we run out of space, we need to expand the sequence table with the following code.
// Sequence Table Expansion void SeqListDilation(Seqlist* sl) { assert(sl); int sz = sl->capacity == sl->size && sl->capacity != 0 ? sl->capacity * 2 : INIT_SIZE; // First application space is 4, not twice capacity if (sl->size == sl->capacity) { // Determine if capacity expansion is required SeqType* tmp = (SeqType*)realloc(sl->list, sizeof(SeqType) * sz); sl->list = tmp; } sl->capacity = sz; }
Solving the problem of expanding the order table, we started to add, delete and change the check
There are three ways to insert data into a sequential table - head, tail, and random
The tail insertion method is simple, which is equivalent to adding data after the array, and the position of size in the order table is the last position of the last data.
// Add data to a sequential table (tail insertion) void SeqListPushBack(Seqlist* sl, SeqType x) { assert(sl); SeqListDilation(sl); sl->list[sl->size] = x; sl->size++; }
The Avatar has to move the array one bit back after each insertion of data
// Add data to a sequential table (header insert) void SeqListPushFront(Seqlist* sl, SeqType x) { assert(sl); SeqListDilation(sl); int i = 0; for (i = sl->size; i > 0; i--) { sl->list[i] = sl->list[i - 1]; } sl->list[0] = x; sl->size++; }
Random insertion means that we specify a location to insert data into
// Sequence table inserts xvoid SeqListInsert(Seqlist* sl, size_t pos, SeqType x) at POS { assert(sl); SeqListDilation(sl); int index = pos > sl->size ? sl->size : pos; int i = 0; for (i = sl->size; i > index; i--) { sl->list[i] = sl->list[i - 1]; } sl->list[pos] = x; sl->size++; }
There are also three ways to delete data - header deletion, tail deletion, random deletion
A tail deletion means that the last element is not accessed, and that's it
// Delete a data from a sequential table (tail) void SeqListPopBack(Seqlist* sl) { assert(sl); sl->size--; }
Header deletion moves all but the first element one bit forward, and the latter overwrites the previous data
// Delete a data from the order table (header deletion) void SeqListPopFront(Seqlist* sl) { assert(sl); int i = 0; for (i = 0; i < sl->size; i++) { sl->list[i] = sl->list[i + 1]; } sl->size--; }
To delete randomly is to delete the data at the location we specified
// Sequence table deletes the value of pos position void SeqListErase(Seqlist* sl, size_t pos) { assert(sl); if (SeqListIsEmpty(sl) == 1) { exit(-1); } int index = pos > sl->size ? sl->size - 1 : pos; int i = 0; for (i = index; i < sl->size; i++) { sl->list[i] = sl->list[i + 1]; } sl->size--; }
Find data from a sequential table
The method here is a convenient sequence table from which we can find the value we want. Return its subscript if found, otherwise return -1
// Sequence table lookup int SeqListFind(Seqlist* sl, SeqType x) { assert(sl); if (SeqListIsEmpty(sl) == 0) { exit(-1); } int i = 0; for (i = 0; i < sl->size; i++) { if (sl->list[i] == x) { return i; } } return -1; }
Print the order table and print it once
// Show Order Table void SeqListPrint(Seqlist* sl) { assert(sl); int i = 0; for (i = 0; i < sl->size; i++) { printf("%d ", sl->list[i]); } }
We'll clean up the sequence table after we've used it
// Clear Sequence Table void SeqListDistory(Seqlist* sl) { assert(sl); free(sl->list); sl->list = NULL; sl->size = 0; sl->capacity = 0; }
—end