顺序队列和链式队列的实现及应用 VS2017 C++

顺序队列和链式队列的实现及应用 VS2017 C++,第1张

顺序队列和链式队列的实现及应用 VS2017 C++ 一、顺序队列的实现(LinearQueue.h)
#pragma once
#include
using namespace std;

template
class LinearQueue
{
public:
	LinearQueue(int LQMaxSize)//创建空队列
	{
		MaxSize = LQMaxSize;
		element = new T[MaxSize];
		size = 0;
		front = 0;
		rear = 0;
	}

	~LinearQueue()//删除队列
	{
		delete[]element;
	}

	bool IsEmpty()//判断队列是否为空,空返回true,非空返回false
	{
		return size == 0;
	}

	bool IsFull()//判断队列是否为满,满返回true,不满返回false
	{
		return size == MaxSize;
	}

	bool Insert(const T& x)//入队,在队列尾部插入元素x
	{
		if (IsFull())
			return false;
		else
		{
			element[rear] = x;
			rear = (rear + 1) % (MaxSize);
			size++;
			return true;
		}
	}

	bool GetElement(T& x)//求队头元素的值放入x中
	{
		if (IsEmpty())
			return false;
		else
		{
			x = element[front];
			return true;
		}
	}

	bool Delete(T& x)//出队,从队头删除一个元素,并将该元素的值放入x中
	{
		if (IsEmpty())
			return false;
		else
		{
			x = element[front];
			front = (front + 1) % (MaxSize);
			size--;
			return true;
		}

	}

	void OutPut(ostream& out)const//输出队列
	{
		int index;
		index = front;
		for (int i = 0; i < size; i++)
		{
			out << element[index] << endl;
			index = (index + 1) % MaxSize;
		}
	}

private:
	int size;//队列实际元素个数
	int MaxSize;//队列中最大元素个数
	int front, rear;//队列的队头和队尾指针
	T *element;//一维动态数组

};
//重载插入运算符<<
template
ostream& operator<<(ostream& out, const LinearQueue& x)
{
	x.OutPut(out);
	return out;
}
二、链接队列的实现(linkQueue.h)
#pragma once
#include
using namespace std;

template
class linkNode//节点类
{
	template
	friend class linkQueue;//将链接队列声明为友类
public:
	linkNode()//构造函数
	{
		next = NULL;
	}

private:
	T data;//节点元素
	linkNode *next;//指向下一个节点的指针
};

template
class linkQueue
{
public:
	linkQueue()//创建空队列
	{
		front = NULL;
		rear = NULL;
		size = 0;

	}

	~linkQueue()//删除队列
	{
		T x;
		while (front != NULL)//队列非空则元素依次出队 
			Delete(x);
	}

	bool IsEmpty()//判断队列是否为空
	{
		return size == 0;
	}

	bool Insert(const T&x)//入队,在队列尾插入元素x
	{
		linkNode *p = new linkNode;
		if (p == NULL)
			return false;
		else
		{
			p->data = x;
			if (front == NULL)
			{
				rear = p;
				front = p;
			}
			else
			{
				rear->next = p;//插入新结点
				rear = p;//指向新队尾
			}
			size++;
			return true;
		}
	}

	bool GetElement(T& x)//求队头元素的值放入x中
	{
		if (IsEmpty())
			return false;
		else
		{
			x = front->data;
			return true;
		}

	}

	bool Delete(T& x)//出队,从队头删除一个元素,并将该元素的值放入x中
	{
		linkNode *p;
		if (IsEmpty())
			return false;
		else
		{
			p = front;
			x = front->data;
			front = front->next;
			delete p;//删除头节点
			size--;
			return true;
		}
	}

	viod OutPut(ostream& out)const//输出队列
	{
		linkNode *p;
		p = front;
		for (int i = 0; i < size; i++)
		{
			out << p->data << end;
			p = p->next;
		}
	}

private:
	int size;//队列实际元素个数
	linkNode *front, *rear;//队列的队头和队尾指针
};
//重载插入运算符<<
template
ostream& operator<<(ostream& out, const linkQueue& x)
{
	x.OutPut(out);
	return out;
}
三、杨辉三角
#include
using namespace std;
#include"LinearQueue.h"

void PrintSpace(int n, int k)
{
	for (int i = 1; i <= n - k;i++)
	{
		cout << " ";
	}
}
//输出杨辉三角的前n行(n>0)
void YangHui(int n)
{
	LinearQueueQ(n + 2);
	int x, y;
	PrintSpace(n, 1);//输出第一行前面的空格
	cout << "1" << endl;//输出第一行的1
	Q.Insert(0);//添加行开始标识
	Q.Insert(1);//第二行入队
	Q.Insert(1);//第二行入队
	for (int i = 2; i <= n; i++)
	{
		Q.Insert(0);//添加行结束标识
		PrintSpace(n, i);//输出第i行数字前面的空格
		do
		{
			Q.Delete(x);
			Q.GetElement(y);
			if (y)
				cout << y << " ";
			else
				cout << endl;
			Q.Insert(x + y);
		} while (y);
	}
	cout << endl;
}

int main()
{
	int n;
	cout << "请输入要显示的杨辉三角的行数:" << endl;
	cin >> n;
	YangHui(n);
	return 0;
}

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5702987.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存