Data structure experiment 1

Posted by Spikey on Thu, 23 May 2019 00:19:11 +0200



Basic Operation Realization and Application of Experimental Linearity

 

 

 

 

I. Experimental Purpose

 

1. Proficiency in the structure of linear tables and basic operation of sequential tables.

 

2. Consolidate C++ related programming methods and techniques.

 

3. Learn to use sequence table to solve practical problems.

 

II. EXPERIMENTAL CONTENTS

 

1. Establishment of Sequence Table and Operation Implementation Establishment of Sequence Table of n Elements (the size of N and the data in the table are determined by themselves), and implementation of related operations: output, interpolation

 

Input, deletion, search and other functions. Write a complete program to achieve, the program language is not limited, the use of technical form is uncertain. 2. Solution of Practical Problems (*)

The sequence table is used to implement Joseph ring problem.

 

III. EXPERIMENTAL STEPS

 

1. Explain the definition of data type used in the experiment program according to the experiment content.

 

2. Algorithmic expression of related operations;

 

3. Complete procedures;

 

4. Summarizing, operating results and analysis.

 

5. Overall gains and shortcomings, doubts and so on.

 

IV. EXPERIMENTAL REQUIREMENTS

 

1. Preview and prepare the experiment in advance according to the data structure experiment task book.

 

2. Add "*" as the selected topic. Doing well can add points.

 

3. Complete the experiment report in time strictly according to the template and specification of data structure experiment report.

 

4. Publish articles on personal homepage

#include<iostream>
using namespace std;
const int MaxSize = 100;
class SeqList
{
private:
	int data[MaxSize];
	int length;
	int i,j;
public:
	SeqList() { length = 0; }
	SeqList(int a[], int n);
	~SeqList() { }
	int Length() { return length; }
	int Get(int i);
	int Locate(int x);
	void Insert(int i, int x);
	int Delete(int i);
	void PrintList();

};
SeqList::SeqList(int a[], int n)
{
	if (n > MaxSize) throw "invalid parameter";
		for (i = 0; i < n; i++)
			data[i] = a[i];
	length = n;
}
int SeqList ::Get(int i)
{
	if (i<1 && i>length) throw "invalid parameter";
	else cout<<data[i - 1]<<endl;
	return data[i - 1];
}
int SeqList::Locate(int x)
{
	for (i = 0; i < length; i++)
		if (data[i] == x) cout<< i + 1<<endl;
	return 0;
}
void SeqList::Insert(int i, int x)
{
	if (length >= MaxSize) throw"Overflow";
	if (i < 1 || i >= length + 1) throw"position";
	for (j = length; j >= i; j--)
		data[j] = data[j - 1];
	data[i - 1] = x;
	length++;
}
int SeqList::Delete(int i)
{
	int x;
	if (length == 0) throw"Underflow";
	if (i<1 || i>length) throw"position";
	x = data[i - 1];
	for (j = i; j < length; j++)
		data[j - 1] = data[j];
	length--;
	return x;
}
void SeqList::PrintList()
{
	for (i = 0; i < length; i++)
		cout << data[i];
	cout << endl;
}
int main()
{
	int a[10] = { 6,5,4,3,2,1 };
	SeqList  student1();
	SeqList stu2(a, 6);
	stu2.Get(2);
	stu2.Locate(1);
	stu2.Insert(2, 9);
	stu2.PrintList();
	stu2.Delete(1);
	stu2.PrintList();
}

The experimental results are as follows:


Conclusion:

After a whole summer vacation, it seems that the code and grammar are quite new to reuse c++. Now the only thing we can do is to use the word "diligent". "

Basic Operation Realization and Application of Experimental Linearity

 

 

 

 

I. Experimental Purpose

 

1. Proficiency in the structure of linear tables and basic operation of sequential tables.

 

2. Consolidate C++ related programming methods and techniques.

 

3. Learn to use sequence table to solve practical problems.

 

II. EXPERIMENTAL CONTENTS

 

1. Establishment of Sequence Table and Operation Implementation Establishment of Sequence Table of n Elements (the size of N and the data in the table are determined by themselves), and implementation of related operations: output, interpolation

 

Input, deletion, search and other functions. Write a complete program to achieve, the program language is not limited, the use of technical form is uncertain. 2. Solution of Practical Problems (*)

The sequence table is used to implement Joseph ring problem.

 

III. EXPERIMENTAL STEPS

 

1. Explain the definition of data type used in the experiment program according to the experiment content.

 

2. Algorithmic expression of related operations;

 

3. Complete procedures;

 

4. Summarizing, operating results and analysis.

 

5. Overall gains and shortcomings, doubts and so on.

 

IV. EXPERIMENTAL REQUIREMENTS

 

1. Preview and prepare the experiment in advance according to the data structure experiment task book.

 

2. Add "*" as the selected topic. Doing well can add points.

 

3. Complete the experiment report in time strictly according to the template and specification of data structure experiment report.

 

4. Publish articles on personal homepage

#include<iostream>
using namespace std;
const int MaxSize = 100;
class SeqList
{
private:
	int data[MaxSize];
	int length;
	int i,j;
public:
	SeqList() { length = 0; }
	SeqList(int a[], int n);
	~SeqList() { }
	int Length() { return length; }
	int Get(int i);
	int Locate(int x);
	void Insert(int i, int x);
	int Delete(int i);
	void PrintList();

};
SeqList::SeqList(int a[], int n)
{
	if (n > MaxSize) throw "invalid parameter";
		for (i = 0; i < n; i++)
			data[i] = a[i];
	length = n;
}
int SeqList ::Get(int i)
{
	if (i<1 && i>length) throw "invalid parameter";
	else cout<<data[i - 1]<<endl;
	return data[i - 1];
}
int SeqList::Locate(int x)
{
	for (i = 0; i < length; i++)
		if (data[i] == x) cout<< i + 1<<endl;
	return 0;
}
void SeqList::Insert(int i, int x)
{
	if (length >= MaxSize) throw"Overflow";
	if (i < 1 || i >= length + 1) throw"position";
	for (j = length; j >= i; j--)
		data[j] = data[j - 1];
	data[i - 1] = x;
	length++;
}
int SeqList::Delete(int i)
{
	int x;
	if (length == 0) throw"Underflow";
	if (i<1 || i>length) throw"position";
	x = data[i - 1];
	for (j = i; j < length; j++)
		data[j - 1] = data[j];
	length--;
	return x;
}
void SeqList::PrintList()
{
	for (i = 0; i < length; i++)
		cout << data[i];
	cout << endl;
}
int main()
{
	int a[10] = { 6,5,4,3,2,1 };
	SeqList  student1();
	SeqList stu2(a, 6);
	stu2.Get(2);
	stu2.Locate(1);
	stu2.Insert(2, 9);
	stu2.PrintList();
	stu2.Delete(1);
	stu2.PrintList();
}

The experimental results are as follows:


Conclusion:

After a whole summer vacation, it seems that the code and grammar are quite new to reuse c++. Now the only thing we can do is to use the word "diligent". "

Topics: Programming