造轮子。。

1树

//node.h
#pragma once
#include<iostream>
using namespace std;
class Node
{
public:
	Node()
	{
		data = 0;
		index = 0;
		plChild = nullptr;
		prChild = nullptr;
		pParent = nullptr;
	}
	/*
	~Node()
	{
		DeleteNode();
	}
	*/
	Node* SearchNode(int nodeindex)
	{
		Node* temp = nullptr;
		if (this->index == nodeindex)
		{
			return  this;
		}
		if (this->plChild != nullptr)
		{
			if (this->plChild->index == nodeindex)
				return this->plChild;
			else
			{
				temp = this->plChild->SearchNode(nodeindex);
				if (temp != nullptr)
					return temp;
			}
		}
		if (this->prChild != nullptr)
		{
			if (this->prChild->index == nodeindex)
				return this->prChild;
			else
			{
				temp = this->prChild->SearchNode(nodeindex);
				if (temp != nullptr)
					return temp;
			}
		}
		return nullptr;
	}
	void DeleteNode()
	{
		if (this->plChild != nullptr)
			this->plChild->DeleteNode();
		if (this->prChild != nullptr)
			this->prChild->DeleteNode();
		if (this->pParent != nullptr)
		{
			if (this->pParent->plChild == this)
				this->pParent->plChild = nullptr;
			if (this->pParent->prChild == this)
				this->pParent->prChild = nullptr;
		}
		delete this;
	}
	void  PreordTraversal()
	{
		cout << this->data << "#" << index<<endl;
		//if(this->pParent!=nullptr)
			//cout << this->pParent->data << "  ";
		if (this->plChild != nullptr)
		{
			//cout << this->plChild->data << endl;
			this->plChild->PreordTraversal();
		}
		if (this->prChild != nullptr)
		{
			//cout << this->prChild->data << endl;
			this->prChild->PreordTraversal();
		}
	}
	int data;
	int index;
	Node* plChild;
	Node* prChild;
	Node* pParent;
};












//tree.h
class  Tree
{
public:
	Tree()
	{
		mpRoot = new  Node();
	}
	~Tree()
	{
		DeleteNode(0, nullptr);
	}
	Node* SearchNode(int nodeindex)		//搜索结点
	{
		return mpRoot->SearchNode(nodeindex);
	}
	bool AddNode(int nodeindex, int direction, Node* pNode)		//添加结点
	{
		Node* temp = SearchNode(nodeindex);
		if (temp == nullptr)
			return  false;
		Node* node = new  Node();
		if (node == nullptr)
			return false;
		node->data = pNode->data;
		node->index = pNode->index;
		if (direction == 0)
		{
			temp->plChild = node;
			node->pParent=temp;
		}
		if (direction == 1)
		{
			temp->prChild = node;
			 node->pParent=temp;
		}
		return true;
	}
	bool DeleteNode(int nodeindex, Node* pNode)		//删除
	{
		Node* temp = SearchNode(nodeindex);
		if (temp == nullptr)
			return  false;
		if (pNode != nullptr)
		{
			pNode->data = temp->data;
		}
		temp->DeleteNode();
		return  true;
	}
	void  PreordTraversal()		//前序遍历
	{
		mpRoot->PreordTraversal();
	}
	void InordTraversal();//中序
	void  PostTraversal();//后序

private:
	Node* mpRoot;
};

2环形队列

//cirqueue.h
#pragma once
#ifndef MYQUEUE_H

#define MYQUEUE_H
class MyQueue
{
public:
	MyQueue(int queueCapacity);
	~MyQueue();
	void ClearQueue();
	bool QueueEmpty() const;
	bool QueueFull() const;
	int QueueLength() const;
	bool EnQueue(int element);
	bool DeQueue(int& element);
	void QueueTraverse();
private:
	int* m_pQqueue;
	int m_iQueueLen;
	int m_iQueueCapacity;
	int m_iHead;
	int m_iTail;

};
#endif 










//cirqueue2.h
#include "cirqueue.h"
#include <iostream>
using namespace std;


MyQueue::MyQueue(int queueCapacity)
{
	m_iQueueCapacity = queueCapacity;
	ClearQueue();
	m_pQqueue = new int[m_iQueueCapacity];

}
MyQueue::~MyQueue()
{
	delete[]m_pQqueue;
}
void MyQueue::ClearQueue()
{
	m_iHead = 0;
	m_iTail = 0;
	m_iQueueLen = 0;
}
bool MyQueue::QueueEmpty() const
{
	if (m_iQueueLen == 0)
	{
		return true;
	}
	else
		return false;
}
bool MyQueue::QueueFull() const
{
	if (m_iQueueLen == m_iQueueCapacity)
	{
		return true;
	}
	else
		return false;
}
int MyQueue::QueueLength() const
{
	cout << m_iQueueLen << endl;
	return m_iQueueLen;
}
bool MyQueue::EnQueue(int element)
{
	if (!QueueFull())
	{
		m_pQqueue[m_iTail] = element;
		m_iQueueLen++;
		m_iTail++;
		m_iTail = m_iTail % m_iQueueCapacity;
		return true;
	}
	else
	{
		cout << "此队列已满,无法添加!" << endl;
		return false;
	}


}
bool MyQueue::DeQueue(int& element)
{
	if (!QueueEmpty())
	{
		element = m_pQqueue[m_iHead];
		cout << element << "退队列成功!" << endl;
		m_iQueueLen--;
		m_iHead++;
		m_iHead = m_iHead % m_iQueueCapacity;
		return true;
	}
	else
	{
		cout << "此队列已空,无法删除!" << endl;
		return false;
	}

}

void MyQueue::QueueTraverse()
{
	if (!QueueEmpty())
	{
		int i;
		for (i = m_iHead; i < m_iQueueLen + m_iHead; i++)
		{
			cout << "第" << i % m_iQueueCapacity << "元素,值为" << m_pQqueue[i % m_iQueueCapacity] << endl;
		}
	}
	else
		cout << "此队列已空,无法遍历!" << endl;
}

3图

//Node.h
#pragma once
class  Node
{
public:
	Node(char data=0)
	{
		mData = 0;
		mIsVisited = false;
	}
	~Node();
	char mData;
	bool mIsVisited;
};










//cmap.h
#pragma once
#include<iostream>
#include<vector>
#include"Node.h"
using  namespace std;
class cMap
{
public:
	cMap(int capacity)
	{
		mCatacity = capacity;
		mNodeCount = 0;
		pNodeArray = new Node[mCatacity];
		pMatrix = new int[mCatacity*mCatacity];
		for (int i = 0; i <= mCatacity * mCatacity; i++)
			pMatrix[i] = 0;
	}
	~cMap()
	{
		delete[]pNodeArray;
		delete[]pMatrix;
	}
	bool Addnode(Node* pnode)			//添加顶点
	{
		if (pnode == nullptr)
			return false;
		pNodeArray[mNodeCount].mData = pnode->mData;
		mNodeCount++;
		return true;
	}
	void Resetnode()												//重置顶点
	{
		for (int i = 0; i <= mNodeCount; i++)
			pNodeArray[i] = false;
	}
	bool setVTMFDG(int row, int col, int val = 1)					//为无向图设置邻接矩阵
	{
		if (row < 0 || row >= mCatacity)
			return false;
		if (col < 0 || col >= mCatacity)
			return false;
		pMatrix[row * mCatacity + col] = val;
		return true;
	}	
	bool setVTMFUDG(int row, int col, int val = 1)				//为有向图图设置邻接矩阵
	{
		if (row < 0 || row >= mCatacity)
			return false;
		if (col < 0 || col >= mCatacity)
			return false;
		pMatrix[row * mCatacity + col] = val;
		pMatrix[col * mCatacity + row] = val;
		return true;
	}
	void printfMatrix()										//输出矩阵
	{
		for(int i=0;i<=mCatacity;i++)
		{
			for (int j = 0; j <= mCatacity; j++)
				cout << pMatrix[i * mCatacity + j] << " ";
			cout << endl;
		}
	}
	void depthTraverse(int nodeIndex = 0)										//深度优先遍历
	{
		int value = 0;
		cout << pNodeArray[nodeIndex].mData << " ";
		pNodeArray[nodeIndex].mIsVisited = true;
		for (int i = 0; i <= mCatacity; i++)
		{
			getValue(nodeIndex, i, value);
			if (value == 1)
				{
				if (pNodeArray[i].mIsVisited)
				{
					continue;
				}
				else
				{
					depthTraverse(i);
				}
				}
			else
			{
				continue;
			}
		}
	}
	void breadthTraverse(int nodeIndex)										//广度优先遍历	
	{
		cout << pNodeArray[nodeIndex].mData << " ";
		pNodeArray[nodeIndex].mIsVisited = true;
		vector<int>curVec;
		curVec.push_back(nodeIndex);
		breadthTI(curVec);
	}
private:
	bool getValue(int row,int col,int&val)						//获取权重
	{
		if (row < 0 || row >= mCatacity)
			return false;
		if (col < 0 || col >= mCatacity)
			return false;
		val = pMatrix[row * mCatacity + col];
		return true;
	}
	void breadthTI(vector<int>preVec)						//广度度优先遍历实现函数
	{
		int value = 0;
		vector<int>curVec;
		for (int j = 0; j < preVec.size; j++)
		{
			for (int i = 0; i < mCatacity; i++)
			{
				getValue(preVec[j], i, value);
				if (value != 0)
				{
					if (pNodeArray[i].mIsVisited)
						continue;
					else
					{
						cout << pNodeArray[i].mData << " ";
						pNodeArray[i].mIsVisited = true;
						curVec.push_back(i);
					}
				}
			}
		}
		if (curVec.size() == 0)
		{
			return;
		}
		else
		{
			breadthTI(curVec);
		}
	}


private:
	int mCatacity;							//最大顶点数
	int mNodeCount;							//已存顶点数
	Node* pNodeArray;						//顶点数组
	int* pMatrix;							//存邻接矩阵
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值