Data structure experiment 2 merging of non decreasing ordered tables based on sequential tables

Keywords: data structure

Name: Far_Rainbow
Student No.: 202505000X
Major: Software Engineering
Grade: 2020

Experiment Name: Experiment 2 merging of non decreasing ordered tables based on sequential tables

Experiment content:

(1) Experimental purpose
Through this experiment, we can deeply understand the logical structure, physical structure and other concepts of the sequence table, master the programming implementation of the basic operation of the sequence table, pay attention to the movement of data elements in the operation process of insertion and deletion of the sequence table, train students to consider the robustness of the program, comprehensively consider problems, and skillfully master the method of returning function results through function parameters.

(2) Experimental content
The basic operations of the linear table defined in Chapter 2 of the Textbook under the sequence table are realized by programming, and the combination of two non decreasing ordered linear tables is realized according to the realized basic operations. Note that if there are duplicate elements (duplicate elements within a table and between two tables), please keep one.

(3) Experimental requirements

  • Finding the precursor refers to entering an element value (rather than position) to find the direct precursor element value of the element in the sequence table. Seeking successor refers to: entering an element value (rather than position) to find the direct successor element value of the element in the sequence table;
  • In order to modify the type of data element, please use type redefinition to modify the type of data element in linear table;
  • The return result of most functions should be a status of whether the function execution is successful. Only when the execution is successful, the specific result value is returned;
  • When testing each function, it is required to test the illegal situation. See the following test cases for details;
  • The menu form is used to correspond to each operation and make it into a complete small software. The reference interface is as follows. Note: during the program operation, the menu shall not be made into the screen brushing effect, and the tested data shall not be cleared, so as to facilitate screenshots and viewing.

(4) Acceptance / test cases
Call each operation through the menu, and test points:

  • Whether the program can control other operations before initialization; That is, if the linear table is not initialized, other functions cannot be performed normally. If you choose to perform other operations, you should prompt to initialize first;
  • First select menu 1 and initialize a sequence table (initialization sequence table refers to initializing an empty linear table with 0 elements);
  • Select menu 10 and insert data (position, data). To test the illegal insertion position (0,1), (2,1), correctly insert three data (1,20), (1,10), (3,30);
  • Display the data in the sequence table and output 10, 20 and 30 on the screen;
  • Blank judgment, the screen output sequence table is not empty;
  • Output sequence table length, screen output 3;
  • Obtain the element of the specified position, and measure the situation that the specified position is outside and within the range of [1,3];
  • Positioning, input: 40, output: not present, input: 20, output position: 2;
  • To find the direct precursor, measure the precursor of the first element, the direct precursor of the element that does not exist in the sequence table, and the direct precursor of other elements; Input 10, output: the first element has no precursor, input 20, the output precursor is 10, input 40, the output element does not exist;
  • To find the direct successor, measure the successor of the last element, the direct successor of the element that does not exist in the sequence table, and the direct successor of other elements; The same as for the precursor;
  • Delete the situation where the position to be measured is outside and within the range of [1, 3];
  • Measure the length after emptying to determine whether it is empty; After clearing, test the functions of menus 6 to 11 to see if they can prompt correctly.
  • Destroy the sequence table. After destroying the linear table, you can also insert, delete and other operations. If you select other operations, you will be prompted that the linear table has been destroyed and does not exist;
  • Test the merging operation. The elements in the first linear table are (2,3,3,4,5) and the contents in the second linear table are (1,4,5,6,7). Please output the merged results.

Type of experiment: confirmatory

The key and difficult points of the experiment: the definition and implementation of sequential table, the difference between destruction and emptying, and the de duplication and merging of two non decreasing ordered tables

Experimental environment: tdm-gcc 4.9.2 64 bit

Experimental steps and completed tasks:
1, Design idea

  1. The template is used to realize generic programming, and the sequence table of any data type can be created;
  2. The capacity of the sequential table is managed by dynamic memory allocation. When the memory space of the sequential table is insufficient, the capacity is expanded by the expand() function;
  3. Experience that the insertion and deletion time complexity of sequence table is O ( n ) O(n) Characteristics of O(n)

2, Main source code

#include <iostream>
#define SEQUENCESIZE 1000
#define SEQUENCEINCREMENT 500
using std::cin;
using std::cout;
using std::endl;

template <typename T> class Sequence {
private:
	T* elem;
	int _length;
	int _capacity;
	void expand();
	
public:
	Sequence() {elem = NULL; _length = 0; }
	~Sequence() {destroy(); }
	void init();
	void destroy();
	void clear() {_length = 0; }
	T operator[](int pos) {return elem[pos]; }
	bool isempty() const {return _length > 0 ? 0 : 1; }
	bool exist() const {return elem > 0 ? 1 : 0; }
	int getlength() const {return _length; }
	bool getelem(int pos, T& e) const 
	{if(pos > _length + 1 || pos < 1) return false; e = elem[pos-1]; return true; }
	int elemlocate(T const& e) const 
	{for(int i = 0; i < _length; ++ i) if(elem[i] == e) return i+1; return -1;}
	bool priorelem(T const& e, int& pos);
	bool nextelem(T const& e, int& pos);
	bool insert(T const& e, int pos);
	bool remove(T& e, int pos);
	void push(T const& e);
	T pop() {-- _length; return _length + 1; }
	void traverse() const 
	{for(int i = 0; i < _length; i ++) cout<<elem[i]<<' '; }
	void merge(Sequence<T> Sa, Sequence<T> Sb);
};

template <typename T> void Sequence<T>::init()
{
	elem = new T[SEQUENCESIZE];
	_capacity = SEQUENCESIZE; 
}

template <typename T> void Sequence<T>::destroy()
{
	delete[] elem;
	elem = NULL;
	_length = 0;
	_capacity = 0;
}

template <typename T> void Sequence<T>::expand()
{
	T* newelem = new T[_capacity+SEQUENCEINCREMENT];
	for(int i = 0; i < _length; ++ i)	newelem[i] = elem[i];
	delete[] elem;
	elem = newelem;
}

template <typename T> bool Sequence<T>::priorelem(T const& e, int& pos)
{
	int i = elemlocate(e);
	if (i < 2 || i > _length)	return false;
	pos = i - 1;
	return true;
}

template <typename T> bool Sequence<T>::nextelem(T const& e, int& pos)
{
	int i = elemlocate(e);
	if (i < 1 || i > _length - 1)	return false;
	pos = i + 1;
	return true;
}

template <typename T> bool Sequence<T>::insert(T const& e, int pos) 
{
	if (pos < 1 || pos > _length + 1)	return false;
	if (_capacity < _length + 1)	expand();
	for(int j = _length - 1; j >= pos - 1; -- j) // At first, it was written as J > I-1. It was debug ged for several hours. It was a bloody lesson 
		elem[j+1] = elem[j];
	elem[pos-1] = e;
	++ _length;
	return true;
}

template <typename T> bool Sequence<T>::remove(T& e, int pos)
{
	if(pos < 1 || pos > _length)	return false;
	e = elem[pos-1];
	for(int j = pos - 1; j < _length - 1; ++ j)
		elem[j] = elem[j+1];
	-- _length;
	return true;
} 

template <typename T> void Sequence<T>::push(T const& e)
{
	if (_capacity < _length + 1)	expand();
	elem[_length] = e;
	++ _length;
} 

template <typename T> void Sequence<T>::merge(Sequence<T> Sa, Sequence<T> Sb)
{
	return;
}

int main()
{
	cout<<"\n   Merging of non decreasing ordered tables based on sequential tables\n\n";
	cout<<"1---Initialize a linear table\n";
	cout<<"2---Destroy linear table\n";
	cout<<"3---Empty linear table\n";
	cout<<"4---Judge whether the linear table is empty\n";
	cout<<"5---Find the length of linear table\n";
	cout<<"6---Gets the element at the specified position in the linear table\n";
	cout<<"7---Gets the location of the linear table element\n";
	cout<<"8---Seeking precursor\n";
	cout<<"9---Succ \n";
	cout<<"10---Inserts an element at the specified position in the linear table\n";
	cout<<"11---Deletes the element at the specified position in the linear table\n";
	cout<<"12---Show linear table\n";
	cout<<"13---Merge two non decreasing ordered linear tables\n"; 
	cout<<"\n\t Exit, enter a negative number!\n\n";
	Sequence<int> Se;
	while(true)
	{
		int myOption;
		cout<<"\n Please enter the operation serial number:";
		cin>>myOption;
		switch(myOption){
			case 1:{
				if(Se.exist()){
					cout<<"Initialization failed, sequence table already exists!\n";
					break; 
				}
				Se.init();
				cout<<"Initialization succeeded!\n";
				break;
			}
			case 2:{
				if (!Se.exist()){
					cout<<"Failed to destroy. The sequence table does not exist\n";
					break; 
				}
				Se.destroy();
				cout<<"Destroyed successfully!\n";
				break;
			} 
			case 3:{
				if (!Se.exist()){
					cout<<"Emptying failed, sequence table does not exist\n";
					break; 
				}
				if (Se.isempty()) ; 
				else	Se.clear();
				cout<<"Empty successfully!\n"; 
				break;
			}
			case 4:{
				if (!Se.exist()){
					cout<<"Empty judgment failed. The sequence table does not exist\n";
					break; 
				}
				if (Se.isempty())	cout<<"Sequence table is empty!\n";
				else	cout<<"Sequence table is not empty!\n"; 
				break;
			}
			case 5:{
				if (!Se.exist()){
					cout<<"Please create the sequence table first!\n";
					break; 
				}
				cout<<"The length of the sequence table is"<<Se.getlength()<<endl; 
				break;
			}
			case 6:{
				if (!Se.exist()){
					cout<<"Sequence table does not exist, please create sequence table first!\n";
					break; 
				}
				if (Se.isempty()){
					cout<<"The sequence table is empty, please insert elements first!\n";
					break;
				}
				cout<<"Please enter the location of the element you want to get:";
				int pos, e;
				cin>>pos;
				if (!Se.getelem(pos, e)) {
					cout<<"No element exists at this location\n";
					break;
				}
				cout<<"The first"<<pos<<"Bit element is "<<e<<endl; 
				break;
			}
			case 7:{
				if (!Se.exist()){
					cout<<"Sequence table does not exist, please create sequence table first!\n";
					break; 
				}
				if (Se.isempty()){
					cout<<"The sequence table is empty, please insert elements first!\n";
					break;
				}
				cout<<"Please enter the element for which you want to get the location:";
				int e;
				cin>>e;
				if (!Se.elemlocate(e)){
					cout<<"The element does not exist in the sequence table!\n";
					break;
				}
				cout<<"element"<<e<<"The location is"<<Se.elemlocate(e)<<endl; 
				break;
			}
			case 8:{
				if (!Se.exist()){
					cout<<"Sequence table does not exist, please create sequence table first!\n";
					break; 
				}
				if (Se.isempty()){
					cout<<"The sequence table is empty, please insert elements first!\n";
					break;
				}
				cout<<"Please enter the element you want to get the precursor:";
				int pos, e;
				cin>>e;
				if (!Se.priorelem(e, pos)){
					cout<<"Failed to get. The element does not exist or has no precursor!";
					break; 
				}
				cout<<"element"<<e<<"The precursor element of is"<<Se[pos-1]<<endl;
				break;
			}
			case 9:{
				if (!Se.exist()){
					cout<<"Sequence table does not exist, please create sequence table first!\n";
					break; 
				}
				if (Se.isempty()){
					cout<<"The sequence table is empty, please insert elements first!\n";
					break;
				}
				cout<<"Please enter the following elements you want to get:";
				int pos, e;
				cin>>e;
				if (!Se.nextelem(e, pos)){
					cout<<"Failed to get. The element does not exist or has no successor!";
					break; 
				}
				cout<<"element"<<e<<"The successor element of is"<<Se[pos-1]<<endl;
				break;
			}
			case 10:{
				if (!Se.exist()){
					cout<<"Sequence table does not exist, please create sequence table first!\n";
					break; 
				}
				cout<<"Please enter the specified location:";
				int pos;
				cin>>pos;
				cout<<"Please enter the value you want to insert:";
				int e;
				cin>>e;
				if (!Se.insert(e, pos)){
					cout<<"Insertion failed, please try again!\n";
					break;
				}
				else	cout<<"Insertion complete!\n";
				break;
			}
			case 11:{
				if (!Se.exist()){
					cout<<"Sequence table does not exist, please create sequence table first!\n";
					break; 
				}
				cout<<"Please enter the location of the element you want to delete:";
				int pos;
				cin>>pos;
				int e;
				if (!Se.remove(e, pos)){
					cout<<"Deletion failed, please try again!\n";
					break;
				}
				else	cout<<"Delete completed!"<<"The deleted element is"<<e<<endl;;
				break;
			}
			case 12:{
				if (!Se.exist()){
					cout<<"Sequence table does not exist, please create sequence table first!\n";
					break; 
				}
				if (Se.isempty()){
					cout<<"Sequence table is empty!\n";
					break;
				}
				cout<<"All elements of the sequence table are:";
				Se.traverse();
				cout<<endl;
				break;
			} 
//			case 13:{
//				
//				break;
//			}
		}
		if(myOption >13)	cout<<"\n Please enter a positive integer less than 13\n\n";
		if(myOption < 0){
			cout<<"\n Exit the program!\n";
			break;
		}
	} 
	return 0;
}

Posted by MBDesktop on Tue, 26 Oct 2021 08:11:17 -0700