实验3:间接寻址

#include <iostream>

using namespace std;
const int MaxSize = 100;

template<class DataType>
struct Node
{
	DataType data;
	Node<DataType> *next;
};



template<class DataType>
class IndirectAddress 
{
public:
	IndirectAddress();
	IndirectAddress(DataType a[], int n);
	~IndirectAddress();
	int Length();
	DataType Get(int i);
	DataType Locate(DataType x);
	void Insert(int i, DataType x);
	DataType Delete(int i);
	void PrintList();
private:
	Node<DataType> *first;
	int length = 0;
	Node<DataType> *address[MaxSize];

};

//无参构造

template<class DataType>
IndirectAddress<DataType>::IndirectAddress()
{
	first = new Node<DataType>;
	first->next = NULL;
}

//有参构造  尾插法

template<class DataType>
IndirectAddress<DataType>::IndirectAddress(DataType a[], int n)
{
	if (n <= 0 && n>MaxSize)throw "插入数据错误";
	Node<DataType> *r, *s;
	first = new Node<DataType>;//初始化头指针
	r = first;          //初始化
	for (int j = 0; j<n; j++) 
	{
		s = new Node<DataType>;
		s->data = a[j];    //给s赋值
		r->next = s;       //把开头开头指针指向s
		r = s;
		address[j] = s;    //把s值address
		length++;
	}
	r->next = NULL; //创建结束,把指针下一位置空
}

template<class DataType>
int IndirectAddress<DataType>::Length()
{
	return length;
}

template<class DataType>
DataType IndirectAddress<DataType>::Get(int i)
{
	return address[i]->data;
}



template <class DataType>
void IndirectAddress<DataType>::Insert(int i, DataType x)
{
	Node<DataType> *p = first;
	for (int j = 1; j <= i - 1; j++)
	{
		p = p->next;
	}
	Node<DataType> *TN;
	TN = new Node<DataType>;
	TN->data = x;
	TN->next = p->next;
	p->next = TN;
	length++;

	// 顺序表的插入

	if (length >= MaxSize) {

		throw "溢出";

	}



	for (int b = length - 1; b > i - 1; b--) {

		address[b] = address[b - 1];

	}

	address[i - 1] = TN;

}



template<class DataType>

DataType IndirectAddress<DataType>::Delete(int i)
{
	DataType x;
	// 单链表操作
	Node<DataType> *p;
	p = first;
	for (int k = 1; k < i; k++)
	{
		p = p->next;
	}
	Node<DataType> *TN;
	TN = new Node<DataType>;
	TN = p->next;
	x = TN->data;
	p->next = TN->next;
	delete TN;
	length--;

	// 顺序表操作

	address[i - 1] = NULL;
	for (int j = i - 1; j <= length; j++) 
	{
		address[j] = address[j + 1];
	}
	return x;
}



template<class DataType>
void IndirectAddress<DataType>::PrintList()
{

	for (int i = 0; i<length; i++)
	{
		std::cout << address[i]->data << " ";
	}
	std::cout << "\n";
}

template<typename DataType>
IndirectAddress<DataType>::~IndirectAddress()
{
	while (first != NULL)
	{
		Node<DataType> *q = first;
		first = first->next;
		delete q;
	}
	length = 0;
}

int main(int argc, const char * argv[])
{
	int a[5] = { 1,2,3,4,5 };
	IndirectAddress<int> dd(a, 5);
	cout << "所有节点信息为";
	dd.PrintList();
	std::cout << "输出第三个节点信息" << dd.Get(3) << "\n";
	std::cout << "插入节点为6的节点在第三位";
	dd.Insert(3, 6);
	cout << "所有节点信息为";
	dd.PrintList();
	std::cout << "删除第三个节点" << dd.Delete(3);
	cout << "所有节点信息为";
	dd.PrintList();
	cout << "第五个节点的信息" << dd.Get(4) << "\n";
	return 0;
}

猜你喜欢

转载自blog.csdn.net/u011633428/article/details/80559849