Data structure C language implementation sequence table -- detailed explanation of addition, deletion, query and modification operation

Keywords: C Algorithm data structure

Sequence table

What is the sequence table?

Sequence table is to store elements in a continuous storage area in sequence, and the sequence relationship between elements is naturally represented by their storage order. Realize the function of adding, deleting, checking and modifying.

Header file required for sequence table:

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

catalogue

Sequence table

Static sequence table

Dynamic sequence table

Dynamic sequence table

Creation of sequence table

Initialization of sequence table

Destruction of sequence table

Print sequence table

Capacity expansion

Tail insertion

Head insert

Tail deletion

Header deletion

Delete anywhere

Insert anywhere

Element lookup

Element modification

Test code and menu

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

Static sequence table

The space of the static sequential table cannot be changed, and the size of the array is used to determine the elements.

Creation of static sequence table:

typedef struct SeqList
{
	int arr[512];
	int size;        //Number of elements in the array
};

Dynamic sequence table

The space of the dynamic sequential table can be changed, and the dynamic memory function is used to open up the array to store elements

Creation of dynamic sequence table:

typedef struct SeqList
{
	int* a;
	int size;            //How many elements are there
	int capaclity;       //capacity
};

The difference between static sequence table and dynamic sequence table is whether the capacity is variable.

We introduce dynamic sequence tables in this section.

Dynamic sequence table

Creation of sequence table

We use a pointer to open up a space, that is, the space of sequential table elements. Since our pointer is not necessarily of int type, we define a data type for preciseness:

typedef int SEQ;

  Then you can create a complete sequence table:

typedef struct SeqLisrt
{
	SEQ* a;
	int size;       //How many elements are in the sequence table
	int capaciti;   //Space size of sequential table
}SL;

Initialization of sequence table

The sequence table needs to be initialized before being used for the first time:

void Init(SL* ps)
{
	ps->a = NULL;
	ps->size = 0;
	ps->capaciti = 0;
}

Destruction of sequence table

The sequence table needs to be destroyed after use to ensure safety:

void Destory(SL* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->size = 0;
	ps->capaciti = 0;
}

Use the free function to free up space and set it to the NULL pointer NULL.

Print sequence table

In order to visually see the elements in the sequence table, we need a function to print the sequence table:

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

Capacity expansion

When size > = capaciti in the sequence table needs to be expanded, and the space of the sequence table is insufficient, how can we expand it?

Since the data table is initially used, the sequence table is initialized, capaciti is 0, and the capacity of capiaciti is given to  * two

Secondly, give a new space. Since a is NULL at this time, a warning will be given if a new space is directly opened. We use an intermediate variable tmp to open a space of capaciti element * 2 and give this space to a.

Implementation code:

void CheckCapacity(SL* ps)
{
	if (ps->size == ps->capaciti)
	{
		int newcapaciti = ps->capaciti == 0 ? 4 : ps->capaciti * 2;   
		SEQ* tmp = (SEQ*)realloc(ps->a, newcapaciti * sizeof(SEQ)*2);
		
		ps->a = tmp;
		ps->capaciti = newcapaciti;
	}
}

After realizing the above preparations, we enter the key points of the sequence table and realize the function of addition, deletion, query and modification.

Tail insertion

Size is the number of elements in the sequence table. Whenever a new element is inserted, size + +;

Tail insertion: put the new element after the last element in the sequence table:  

Code implementation:

void PushBack(SL* ps, SEQ x)
{
	CheckCapacity(ps);  //User defined capacity expansion function

	ps->a[ps->size] = x;
	ps->size++;
}

When inserting new elements into the sequence table, use the expansion function written by yourself to judge.

Head insert

 

Header insertion: shift the elements of the sequence table backward and put the new elements at the front of the sequence table:

void PushFront(SL* ps, SEQ x)
{
	CheckCapacity(ps);

	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end+1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}

Tail deletion

Delete the last element:

Operation method: set size --;

Code implementation:

void PopBack(SL* ps)
{
	ps->size--;
}

Header deletion

Delete the first element of the sequence table.

Operation method: move the element after the first element of the sequence table forward by one bit, size --;

Code implementation:

void PopFront(SL* ps)
{

	int start = 1;
	while (start < ps->size)
	{
		ps->a[start - 1] = ps->a[start];
		start++;
	}
	ps->size--;
}

Delete anywhere

pos is the subscript where the element is to be deleted.

  The implementation method is similar to header deletion. The following elements overwrite the previous elements.

Code implementation:

void Erase(SL* ps, int pos)
{
	int start = pos;
	while (start < ps->size - 1)
	{
		ps->a[start] = ps->a[start + 1];
		start++;
	}
	ps->size--;
}

Insert anywhere

  Similar to header insertion, move the element back into the element.

Code implementation:

void Insert(SL* ps, int pos, SEQ x)
{

	int end = ps->size - 1;
	while (end > +pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}

Element lookup

Use a loop to find until an element is found or there is no such element in the sequence table

The index is returned when the element is found, and - 1 is returned when it is not found;

Code implementation:

int Find(SL* ps, int pos)
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == pos)
		{
			return i;
		}
	}
	return -1;
}

Element modification

Find the location you want to modify and modify it directly.

Code implementation:

void At(SL* ps, int pos, SEQ x)
{

	ps->a[pos] = x;
}

The above is the whole process of adding, deleting, querying and modifying.

Next, let's test the code

Test code and menu

Test results:

  Test menu code:

void menu()
{
	printf("********************************\n");
	printf("1.Tail interpolation data            2.Header data\n");
	printf("3.Tail deletion data            4.Header deletion data\n");
	printf("5.Arbitrary insertion            6.Arbitrary deletion\n");
	printf("7.Modify data            8.Find data\n");
	printf("9.print data           -1.sign out\n");
	printf("********************************\n");
	printf("Please enter the options for your operation>:");
}

int main()
{
	int option = 0;
	int x = 0;
	int i = 0;
	int pos = 0;
	SL s;
	Init(&s);     //Initialize the sequence table before using it

	while (option != -1)
	{
		menu();
		scanf("%d", &option);
		switch (option)
		{
		case 1:
			printf("Please enter the data you want to insert,with-1 end\n");
			do
			{
				scanf("%d", &x);
				if (x != -1)
				{
					PushBack(&s, x);
				}
			} while (x != -1);
			break;
		case 2:
			printf("Please enter the data you want to insert,with-1 end\n");
			do
			{
				scanf("%d", &x);
				if (x != -1)
				{
					PushFront(&s, x);
				}
			} while (x != -1);
			break;
		case 3:
			printf("Please enter the number of elements you want to delete\n");
			scanf("%d", &x);
			for (i = 0; i < x; i++)
			{
				PopBack(&s);
			}
			break;
		case 4:
			printf("Please enter the number of elements you want to delete\n");
			scanf("%d", &x);
			for (i = 0; i < x; i++)
			{
				PopFront(&s);
			}
			break;
		case 5:
			printf("Please enter the location to insert and the data to insert\n");
			scanf("%d%d", &pos, &x);
			Insert(&s, pos, x);
			break;
		case 6:
			printf("Please enter the location to delete\n");
			scanf("%d", &pos);
			Erase(&s, pos);
			break;
		case 7:
			printf("Please enter the location and modified data\n");
			scanf("%d%d", &pos, &x);
			At(&s, pos, x);
			break;
		case 8:
			printf("Please enter the data you want to find\n");
			scanf("%d", &x);
			int ret = Find(&s, x);
			if (ret != -1)
			{
				printf("Found subscript:>%d\n", ret);
			}
			else
				printf("can't find\n");
			break;
		case 9:
			Print(&s);
			printf("\n");
			break;
		default:
			break;
		}
	}
	Destory(&s);//Destroy after use
	return 0;
}

 

The above is the content of this knowledge point.

Ask for a favor!!!!!!

Posted by iRock on Tue, 21 Sep 2021 14:46:02 -0700