Data structure -> Input and output of sequence table, search, delete, destroy, quick sort

Table of contents

"Not much to say" Code: Definition

"Input" of sequence table

"Output" of sequence table

"Search" in sequence table

"Delete" of sequence table

"Destruction" of the sequence list

"Quick sort" of sequence list


Sequence table , the full name of sequence storage structure , is a type of linear table .

When a sequence table stores data, it will apply for a physical space of sufficient size in advance, and then store the data in sequence without leaving any gaps between data elements.

Sequence table_Baidu Encyclopedia

Personal understanding: The mode of storing data in a sequence table is very similar to that of an array, but (of course there are buts) the specific meaning of each data element in the sequence table is different in different situations. It can be a number or a character, or is a page book, or even other more complex information.

"Not much to say" Code: Definition

#pragma once
#include<iostream>
using namespace std;
typedef int Sbt;
#define Size 1              //线性表储存空间的增量
typedef struct Sqlist
{
	Sbt* date = nullptr;    //储存空间的基址
	int legth = 0;          //当前长度
	int size = 0;           //当前分配的储存容量(以sizeof(Sbt)为单位)
}Sqlist;

During initialization, set the date to blank and set the current length of the linear table to "0".

"Input" of sequence table

//输入
void Sq_push(Sqlist& s, int x)
{
	if (s.legth <= s.size)
	{
		Sbt* temp;
		temp = (int *)realloc(s.date, (s.legth + Size) * sizeof(int));
		if (temp == nullptr)
		{
			cout << "内存分配失败" << endl;
			exit(0);
		}
		s.date = temp;
		s.size += Size;
	}
	s.date[s.legth] = x;
	++s.legth;
}

Warm reminder: It is recommended not to allocate memory directly to s.date . In Microsoft Visual Studio Community 2019 (this is the one I use) , it will have the following warning:

The official error message link is here --->     C6308 | Microsoft Docs

"Output" of sequence table

This is relatively simple, so no more nonsense, let’s get to the code.

void Sq_outall(Sqlist& s)
{
	for (int i = 0; i < s.legth; i++)
	{
		cout << s.date[i] << "  ";
	}
}

"Search" in sequence table

Up code

//查找
int Sq_found(Sqlist& s, int x)
{
	for (int i = 0; i < s.legth; i++)
	{
		if (s.date[i] == x)
		{
			cout << "位于第" << i << "号节点   (下标)" << endl;
			return i;
		}
	}
	cout << "ERROR:" << x << "不在指定范围内" << endl;
	return -1;
}

"Delete" of sequence table

There are three cases of deletion: front-end deletion, middle-end deletion, and back-end deletion. All three can be solved at once.

Obtain the subscript of a certain number from the main function, replace it with the number behind it, then replace the number behind it with the number behind it, and so on.

//删除
void Sq_dele(Sqlist& s, int x)
{
	for (int i = 0; i < s.legth; i++)
	{
		if (s.date[i] == x)
		{
			for (; i < s.legth; i++)
			{
				s.date[i] = s.date[i + 1];//将后方的数值替换掉前方的
			}
			s.legth--;
			break;
		}
	}
	cout<<"ERROR: " << x << " 不在指定范围内" << endl;
}

"Don't blame me for not reminding you."  Every time a number is deleted, s.legth will be reduced by one; s.size does not matter, because you have already allocated memory space for next use.

"Destruction" of the sequence list

//销毁
void Sq_destroy(Sqlist& s)
{
	s.legth = 0;
	s.size = 0;
	free(s.date);
	s.date = nullptr;
}

"Quick sort" of sequence list

//快速排序
int Sq_quickcollate(Sqlist& s, int a, int b)
{
	int l = a, r = b,center;
	if (l < r)
	{
		while (l < r)
		{
			while (l < r && s.date[l] >= s.date[r])
				r--;
			if (l < r)
			{
				center = s.date[l];//                     ------ center <--
				s.date[l] = s.date[r];//                /                   \  
				s.date[r] = center;//                   -> s.date[r]------>s.date[l]
				l++;
				center = NULL;
			}
			while (l < r && s.date[r] <= s.date[r])
				l++;
			if (l < r)
			{
				center = s.date[l];
				s.date[l] = s.date[r];
				s.date[r] = center;
				r--;
				center = NULL;
			}
		}
		Sq_quickcollate(s, a, l);
		l++;
		Sq_quickcollate(s, l, b);
	}
	return 0;
}

References: Data structure; C language version/edited by Yan Weimin and Hao Weimin. 1Beijing: Tsinghua University Press, 2007 (reprinted in 2019.1 (Tsinghua University Computer Series Textbooks)
 iSBN 978-7-302-14751-0

I'm a novice, I'm hoping for some advice.

I'm a novice, I'm hoping for some advice.

I'm a novice, I'm hoping for some advice.

Guess you like

Origin blog.csdn.net/MeOutsider/article/details/122387890