Chapter 2 - sequence table of linear table

Posted by fugix on Fri, 04 Mar 2022 17:48:36 +0100

1, Definition and basic operation of linear table

1. Definition of linear table

2. Basic operation of linear table

"Bring it back" explanation:

x=1 is passed in the main function, but it is changed to 1024 in the test function. In fact, another x is equal to 1024 in the memory. When it is output again in the main function, it is still 1, because the value of x=1 in the memory has not been changed at all

2, Definition of sequence table

1. Definition of sequence table

2. Implementation of sequence table

(1) . static distribution

When executing SqList L, it will allocate the required space to the sequence table

If you don't set the default value, the last two numbers will have strange values

The reason for the strange value is that other data is stored before the storage space is allocated to the sequence table, so there will be dirty data. However, why do you omit the step of setting the default value in the postgraduate entrance examination? This is because the fo cycle printing all memory used here is illegal and should be printed according to the length of the sequence table.

At the beginning of the declaration, the value of length must be set to 0.

(2) Dynamic distribution

*The data pointer points to the position of the first data element in the sequence table

malloc function applies for a whole piece of continuous storage space, and free function is used to free space.
The malloc function returns a pointer to the start address of the entire storage space.

Analyze the process behind dynamic implementation:

The data element type of the declaration order table is int type, and data points to the first element in the data table.
Then an InitList function is implemented to initialize a sequence table implemented by dynamic allocation

Then implement a function to dynamically increase the length of the sequence table.

Finally, call the relevant operations in the main function.
After executing this sentence, the computer will open up such a small space in memory.

After executing the second sentence of code, data should point to the first location of the space requested by malloc function

Then the main function written here omits the process of filling the data into the sequence table
Then execute to increase the length of the dynamic array.

Then opened up a new space, now you can save five more data

Because malloc has opened up a new space, the data previously stored is not in the new space, so a for loop is needed to store the data in the new space.

As five space is added, the maximum space will be increased by 5. Finally, the free function will be called to release the previously created p space which has been replaced by the new space.

Code demonstration:

#include<stdlib. h> / / header file of malloc and free functions
#define InitSize 3 / / the default maximum length
typedef int ElemType;
//Dynamic allocation
typedef struct {
	//data is a pointer to dynamically allocate an array, pointing to the position of the first element in the sequence table
	ElemType* data;
	int MaxSize;//Maximum capacity of sequence table
	int length;//Current length of sequence table
}SeqList;//Type definition of sequence table

//Initialize a sequence table implemented by dynamic allocation
void InitList(SeqList& L) {
	  Apply for a piece of continuous storage space with malloc function
	  malloc The function returns a pointer to the start address of the entire storage space
	  The returned pointer needs to be forcibly converted to the pointer of the data element type you defined
	  malloc The parameters of the function need to indicate how much contiguous memory space is allocated
	  InitSize Equal to 10, the allocated memory space is 10 times sizeof(int)=10 × 4=40
	*/ = (int*)malloc(InitSize * sizeof(int));
	L.length = 0;
	L.MaxSize = InitSize;

 Implement a function to dynamically increase the length of the sequence table
 &L Get the address of L
void IncreaseSize(SeqList& L, int len) {
	int* p =; = (int*)malloc((L.MaxSize + len) * sizeof(int));
	for (int i = 0; i < L.length; i++)
	{[i] = p[i];//Copy data to New Area
	L.MaxSize = L.MaxSize + len;//Increase the maximum length of sequence table by len
	free(p);//Free up the original memory space

//Then call the relevant operations in the main function
int main() {
	SeqList L;//Declare a sequence table
	InitList(L);//Initialization sequence table
	//Add a few random elements to the sequence table[0] = 1;[1] = 2;[2] = 3;
	L.length = 3;
	//Expand the capacity of the sequence table
	IncreaseSize(L, 5);
	//Traversal sequence table
	for (int i = 0; i < L.length; i++)
	return 0;

3. Insertion and deletion of sequence table

(1) , insert

To insert C, you have to move all the back one space back.

When inserting 3 in position 9, after running the code, 3 will be inserted into data[8], but positions 7 and 8 are empty,
So this code is not robust enough

Therefore, the inserted code should be written as:

Code demonstration:

#define MaxSize 3 / / define the maximum length
typedef struct {
	int data[MaxSize]; //Use a static "array" to store data elements
	int length; //Power length of sequence table
}SqList; //Type definition of property sheet
bool ListInsert(SqList& L, int i, int e) {
		if (i<1 || i>L.length+1) {//Judge whether the range of i is valid
			printf("because i Invalid range, failed to insert!\n");
			return false;
		if (L.length >= MaxSize) //The current storage space is full
			printf("Insert failed because the storage space is full!\n");
			return false;
		for (int j = L.length; j >= i; j--) //Move the i th element and subsequent elements backward
		{[j] =[j - 1];
		}[i - 1] = e;//Place e at position i
		L.length++;//Length plus 1
		return true;
void main() {
	SqList L;[0] = 0;[1] = 1;[2] = 2;
	L.length = 3;

	printf("The contents of the current sequence table are:\n");
	for (int i = 0; i < L.length; i++)

Time complexity of insert operation:

(2) , delete

After deletion, you need to move all the following forward by one bit, and reduce the value of length by one

Code implementation:

&50: Which sequence table is to be deleted
i: Which data element do you want to delete
&e: Use this element to return the deleted data element

Code demonstration:

#define MaxSize 10
typedef struct {
	int data[MaxSize];
	int length;
void InitList(SqList &L) {
	L.length = 0;
bool ListDelete(SqList& L, int i, int& e) {
	if (i<1||i>L.length) //Judge whether the range of i is valid
		return false;
	e =[i - 1]; //Assign the deleted element to e
	for (int j = i; j < L.length; j++)//Move the i th element forward
	{[j - 1] =[j];
	L.length--; //Linear table length minus one
	return true;
int main() {
	SqList L; //Declare a sequence table
	InitList(L); //Initialization sequence table
	//Insert several elements[0] = 1;[1] = 2;[2] = 3;[3] = 4;
	L.length = 4;
	int e = -1; //Bring back the deleted element with variable e
	if (ListDelete(L, 3, e)) {
		printf("The 3rd element has been deleted. The value of the deleted element is=%d\n",e);
		printf("Bit order i Illegal, deletion failed\n");
	for (int i = 0; i < L.length; i++)

Time complexity of delete operation:

4. Search of sequence table

1. Search by bit

(1) Static allocation

(2) Dynamic allocation

Time complexity of bit search:

2. Search by value

Data elements of structure type cannot be like this

Time complexity to find by value:

Topics: Java data structure