C++ data structure experiment one linear table

Experiment content:

1. Create a sequence table, randomly generate 10 integers within 100, and complete them as required:
(1) Write a display function to display 10 integers in the sequence table on the screen;
(2) Write a search function, enter any integer from the keyboard to search in the sequence table, if found, return the position of the element in the sequence table, otherwise it prompts that there is no such element;
(3) Write the insert function, input the element to be inserted and the insertion position from the keyboard, and output the sequence table after the insertion;
(4) Write the delete function, input the position of the element to be deleted from the keyboard, and output the sequence table after deleting the position element.
2. There are two ordered singly linked lists L1 and L2, which store 10 data elements respectively. The algorithm is designed to merge the two singly linked lists, and the merged singly linked lists are required to be sorted in order, and the merged result is output.

Experiment code:

// SequenceList.h
#pragma once
#include<iostream>

template<typename T>
class SequenceList
{
    
    
public:
	// 构造函数
	SequenceList() :
		mMaxSize(10), mLength(0), mData(nullptr)
	{
    
    
		mData = new T[mMaxSize]();
	}
	SequenceList(T* data, int length) :
		mMaxSize(length + 10), mLength(length), mData(nullptr)
	{
    
    
		// 申请一块比源数据元素多10个元素的堆空间
		mData = new T[mMaxSize]();
		for (size_t i = 0; i < length; i++)
		{
    
    
			mData[i] = data[i];
		}
	}
	// 析构函数
	~SequenceList()
	{
    
    
		delete[] mData;
	}

	// 显示函数
	void Display();
	// 查找函数
	int Find(T element);
	// 插入函数
	void Insert(T data, int pos);
	// 删除函数
	T Delete(int pos);

	// Getter/Setter
	int GetLength() {
    
     return mLength; }
	int GetMaxSize() {
    
     return mMaxSize; }

private:
	int mMaxSize;
	int mLength;
	T* mData;
};

template<typename T>
void SequenceList<T>::Display()
{
    
    
	for (size_t i = 0; i < mLength; i++)
	{
    
    
		std::cout << mData[i] << " ";
	}
	std::cout << std::endl;
}

template<typename T>
int SequenceList<T>::Find(T element)
{
    
    
	for (size_t i = 0; i < mLength; i++)
	{
    
    
		if (mData[i] == element)
		{
    
    
			return i;
		}
	}
	std::cout << "元素查找失败!" << std::endl;
	return -1;
}

template<typename T>
void SequenceList<T>::Insert(T data, int pos)
{
    
    
	if (mLength == mMaxSize)
	{
    
    
		std::cout << "顺序表在执行插入元素时错误:顺序表已满!" << std::endl;
		return;
	}
	if (pos < 0)
	{
    
    
		std::cout << "顺序表在执行插入元素时错误:非法的位置!" << std::endl;
		return;
	}
	if (pos >= mLength)
	{
    
    
		// pos如果超出范围则插入到顺序表尾部
		pos = mLength;
	}
	for (size_t i = mLength; i > 0; i--)
	{
    
    
		if (i + 1 == pos)
		{
    
    
			mData[i] = data;
			break;
		}
		mData[i] = mData[i - 1];
	}
	mLength++;
}

// 传入顺序表并返回插入数据后的该顺序表
template<typename T>
SequenceList<T>& InsertSeq(SequenceList<T>& seq, T data, int pos)
{
    
    
	seq.Insert(data, pos);
	return seq;
}


template<typename T>
T SequenceList<T>::Delete(int pos)
{
    
    
	T* temp1 = new T();
	T temp2 = *temp1;
	delete temp1;
	int index = pos - 1;
	if (mLength == 0)
	{
    
    
		std::cout << "顺序表在执行删除元素时错误:顺序表为空!" << std::endl;
		return temp2;
	}
	if (index < 0 || index >= mLength)
	{
    
    
		std::cout << "顺序表在执行删除元素时错误:非法的位置!" << std::endl;
		return temp2;
	}

	temp2 = mData[index];
	for (size_t i = index; i < mLength; i++)
	{
    
    
		if (i == mLength)
		{
    
    
			mData[i] = 0;
		}
		else
		{
    
    
			mData[i] = mData[i + 1];
		}
	}
	mLength--;
	return temp2;
}

// 传入顺序表并返回删除数据后的该顺序表
template<typename T>
SequenceList<T>& DeleteSeq(SequenceList<T>& seq, int pos)
{
    
    
	seq.Delete(pos);
	return seq;
}


// main.cpp
#include<iostream>
#include<cstdlib>
#include<ctime>
#include"SequenceList.h"

int* RandomDataGenerator(int* a, int length = 10);

int main()
{
    
    
	// 产生随机数据
	int randomData[10];
	RandomDataGenerator(randomData);

	SequenceList<int> seqList(randomData, 10);

	// 显示
	seqList.Display();
	// 查找
	int data;
	std::cout << "请输入要查找的元素:";
	std::cin >> data;
	int index = seqList.Find(data);
	if(index < 0)
	{
    
     
		std::cout << "没有查找到此元素!" << std::endl;
	}
	else
	{
    
    
		std::cout << "元素" << data << "在顺序表中的位置为" << index + 1 << std::endl;
	}
	// 插入
	std::cout << "在第6个位置插入42" << std::endl;
	InsertSeq(seqList, 42, 6);
	seqList.Display();
	// 删除
	std::cout << "删除第8个元素" << std::endl;
	DeleteSeq(seqList, 8);
	seqList.Display();

	return 0;
}

int* RandomDataGenerator(int *a, int length)
{
    
    
	std::srand((unsigned int)time(0));
	for (int i = 0; i < length; i++)
	{
    
    
		a[i] = rand() % 101;
	}

	return a;
}

to sum up:

There is nothing to say about the sequence table, the old basics are just a very common way of writing. When using random numbers, pay attention to whether the random number seed is the same every time.

Guess you like

Origin blog.csdn.net/qq_37856544/article/details/107389431