[Data Structure Experiments] Linear Table DEMO

I. Experimental Requirements
To construct a linear table, insertion and deletion operations are required, data of all items can be output, and two linear tables can be merged into the third empty table.

II. Reference Code
This code completes the demonstration by constructing some input linear tables of random numbers. After importing random header file, construct random uniform integer distribution

std::uniform_int_distribution<unsigned int> u(a, b);

To obtain a uniform integer distribution, the range is discrete closed interval [a, b].
In addition, you need to declare a random engine

std::default_random_engine d;

To get random numbers. By calling u(d), a random integer in the range of [a, b] can be returned.
Random engines need different seeds to obtain different random number sequences. Here, clock() function is used to obtain milliseconds since program start as random number seeds.

In order to train the reader's ability to read the code, the code will not be explained in detail, just mention some of the more error-prone areas:
1. Functions

void *__cdecl malloc(size_t _Size)

The _Size parameter is the number of bytes, not the length of the array. An error will occur if insufficient allocated space causes writing to the end of the allocated (granted) memory. Of course, this error often does not terminate the program immediately and prompt, but only after executing a number of statements. Cross-border access may prompt Access Violation or 0xC0000005 or 0xC0000374 or simply "trigger a breakpoint" without any error code.

#include<cstdio>
#include<algorithm>
#include<random>
#include<ctime>
#pragma warning(disable:4996)
unsigned n, p;
struct list { unsigned* data = nullptr, len = 0; };
inline void ins(list &L, const unsigned &pos, const unsigned &num){
	for (unsigned i = L.len; i > p; --i){ L.data[i] = L.data[i - 1]; }
	L.data[p + 1] = num; ++L.len;
}
inline void del(list &L, const unsigned &pos){
	for (unsigned i = pos; i < L.len; ++i){ L.data[i] = L.data[i + 1]; }
	--L.len;
}
int main(){
	for (;;){
		std::uniform_int_distribution<unsigned> u(1, 32), v(0, 62), w(0, 99);
		std::default_random_engine d; d.seed(clock());
		puts("Establishing Linear Tables L");
		list L; L.data = (unsigned*)malloc(256); 
		n = u(d); printf("Total input data %u One:\n", n);
		for (unsigned i = 0; i < n; ++i){ L.data[i] = w(d); printf("%u ", L.data[i]); ++L.len; }
		printf("\n Linear table L Length = %u\n", L.len);
		n = u(d);
		for (unsigned h = 0; h < n && L.len < 64; ++h) {
			p = v(d); if (p >= L.len) { --h; continue; }
			ins(L, p, v(d)); printf("stay L Location %u Post-insert data %u\n", p, L.data[p + 1]);
			puts("list L All items:");
			for (unsigned i = 0; i < L.len; ++i){ 
				printf("%u ", L.data[i]);
				if (i == p)putchar('*');
			}
			printf("\n Linear table L Length = %u\n", L.len);
		}
		n = u(d); putchar('\n');
		for (unsigned h = 0; h < n && L.len > 0; ++h) {
			p = v(d); if (p >= L.len) { --h; continue; }
			del(L, p); printf("delete L Location %u Data\n", p);
			puts("list L All items:");
			for (unsigned i = 0; i < L.len; ++i){
				printf("%u ", L.data[i]);
				if (i == p)putchar('*');
			}
			printf("\n Linear table L Length = %u\n", L.len);
		}
		puts("\n Establishing Linear Tables M");
		list M; M.data = (unsigned*)malloc(256);
		n = u(d); printf("Total input data %u One:\n", n);
		for (unsigned i = 0; i < n; ++i){ M.data[i] = w(d); printf("%u ", M.data[i]); ++M.len; }
		printf("\n Linear table M Length = %u\n\n", M.len);
		n = u(d);
		for (unsigned h = 0; h < n && M.len < 64; ++h) {
			p = v(d); if (p >= M.len) { --h; continue; }
			ins(M, p, v(d)); printf("stay M Location %u Post-insert data %u\n", p, M.data[p + 1]);
			puts("list M All items:");
			for (unsigned i = 0; i < M.len; ++i){
				printf("%u ", M.data[i]);
				if (i == p)putchar('*');
			}
			printf("\n Linear table M Length = %u\n", M.len);
		}
		n = u(d); putchar('\n');
		for (unsigned h = 0; h < n && M.len > 0; ++h) {
			p = v(d); if (p >= M.len) { --h; continue; }
			del(M, p); printf("delete M Location %u Data\n", p);
			puts("list M All items:");
			for (unsigned i = 0; i < M.len; ++i){
				printf("%u ", M.data[i]);
				if (i == p)putchar('*');
			}
			printf("\n Linear table M Length = %u\n", M.len);
		}
		puts("Will table L And table M Merge into tables N\n");
		puts("list L All items:");
		for (unsigned i = 0; i < L.len; ++i) { printf("%u ", L.data[i]); }
		printf("\n Linear table L Length = %u\n", L.len);
		puts("list M All items:");
		for (unsigned i = 0; i < M.len; ++i) { printf("%u ", M.data[i]); }
		printf("\n Linear table M Length = %u\n", M.len);
		list N; N.len = L.len + M.len; N.data = (unsigned*)malloc(4 * N.len);
		std::copy(&L.data[0], &L.data[L.len], &N.data[0]);
		std::copy(&M.data[0], &M.data[M.len], &N.data[L.len]);
		puts("\n list N All items:");
		for (unsigned i = 0; i < N.len; ++i) { printf("%u ", N.data[i]); }
		printf("\n Linear table N Length = %u\n", N.len);
		free(L.data); free(M.data); free(N.data);
		puts("\n To continue, enter any character; otherwise, enter EOF");
		if (getchar() == EOF)return 0;
	}
}

Posted on Mon, 09 Sep 2019 02:47:02 -0700 by bdamod1