使用链栈和链队列的基本操作将一个队列中的元素倒置(C++)

(1)实现节点类模板,放在node.h文件中;
(2)实现链栈类模板,放在lk_stack.h文件中;
(3)实现链队列类模板,放在lk_queue.h文件中;
(4)实现队列元素的倒置,放在alg.h文件中。

main.cpp

#include <iostream>					// 标准流操作
using namespace std;				// 标准库包含在命名空间std中
#include "lk_stack.h"				// 链栈
#include "lk_queue.h"				// 链队列
#include "alg.h"					// 算法
template <class ElemType>
void Show(const ElemType& e)
// 操作结果:显示e
{
	cout << e << " ";				// 显示e
}

int main()							// 主函数main()
{
	int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	LinkQueue<int> lq;				// 队列
	for (int i = 0; i < 9; i++)
		lq.InQueue(a[i]);			// 入队
	cout << "倒置前:";
	lq.Traverse(Show);				// 显示lq
	cout << endl;					// 换行
	Reverse(lq);					// 倒置lq
	cout << "倒置后:";
	lq.Traverse(Show);				// 显示lq
	cout << endl;					// 换行
	return 0;						// 返回值0, 返回操作系统
}

node.h

#pragma once
#pragma once
template<class ElemType>
struct Node
{
	//数据成员
	ElemType data;//数据成分
	Node<ElemType>* next;//指针成分

	//构造函数模板
	Node();//无参数构造函数模板
	Node(const ElemType& e, Node<ElemType>* link = NULL);//已知数据元素值和指针建立节点
};

//节点类模板的实现部分
template<class ElemType>
Node<ElemType>::Node()
//构造指针成分为空的节点
{
	next = NULL;
}

template<class ElemType>
Node<ElemType>::Node(const ElemType& e, Node<ElemType>* link)
//构造一个数据成分为e和指针成分为link的节点
{
	data = e;
	next = link;
}

lk_stack.h

#pragma once
#pragma once
#include"node.h"
template<class ElemType>
class LinkStack   //链栈
{
protected:
	//数据成员
	Node<ElemType>* top;
	int count;

public:
	LinkStack();
	virtual ~LinkStack();
	int Length()const;
	bool Push(const ElemType& e);
	bool Pop(ElemType& e);
	bool Pop();
	void Clear();
	bool Empty()const;

};

template<class ElemType>
LinkStack<ElemType>::LinkStack()
{
	top = NULL;
	count = 0;
}

template<class ElemType>
void LinkStack<ElemType>::Clear()
{
	while (!Empty())
	{
		Pop();
	}
}
template<class ElemType>
LinkStack<ElemType>::~LinkStack()
{
	Clear();
}

template<class ElemType>
int LinkStack<ElemType>::Length()const
{
	return count;
}

template<class ElemType>
bool LinkStack<ElemType>::Push(const ElemType& e)
{
	Node<ElemType>* newTop = new Node<ElemType>(e, top);
	if (newTop == NULL)
	{
		return false;
	}
	else
	{
		top = newTop;
		count++;
		return true;
	}
}

template<class ElemType>
bool LinkStack<ElemType>::Pop(ElemType& e)
{
	if (Empty())
	{
		return false;
	}
	else
	{
		Node<ElemType>* oldTop = top;
		e = oldTop->data;
		top = oldTop->next;
		delete oldTop;
		count--;
		return true;
	}
}

template<class ElemType>
bool LinkStack<ElemType>::Pop()
{
	if (Empty())
	{
		return false;
	}
	else
	{
		Node<ElemType>* oldTop = top;
		top = oldTop->next;
		delete oldTop;
		count--;
		return true;
	}
}

template<class ElemType>
bool LinkStack<ElemType>::Empty()const
{
	return count == 0;
}

lk_queue.h

#pragma once
#pragma once
#include"node.h"

template<class ElemType>
class LinkQueue   //链队列
{
protected:
	//数据成员
	Node<ElemType>* front, * rear;//队头队尾指针
	int count;

public:
	LinkQueue();
	virtual ~LinkQueue();
	int Length()const;
	bool InQueue(const ElemType& e);
	bool OutQueue(ElemType& e);
	bool OutQueue();
	void Clear();
	bool Empty()const;
	void Traverse(void(*show)(const ElemType&))const;
};


template<class ElemType>
LinkQueue<ElemType>::LinkQueue()
{
	rear = front = new Node<ElemType>;
	count = 0;
}

template<class ElemType>
void LinkQueue<ElemType>::Traverse(void(*show)(const ElemType&))const
{
	for (Node<ElemType>* temPtr = front->next; temPtr != NULL; temPtr = temPtr->next)
	{
		(*show)(temPtr->data);
	}

}

template<class ElemType>
void LinkQueue<ElemType>::Clear()
{
	while (!Empty())
	{
		OutQueue();
	}
}

template<class ElemType>
bool LinkQueue<ElemType>::Empty()const
{
	return count == 0;
}

template<class ElemType>
LinkQueue<ElemType>::~LinkQueue()
{
	Clear();
	delete front;
}

template<class ElemType>
int LinkQueue<ElemType>::Length()const
{
	return count;
}

template<class ElemType>
bool LinkQueue<ElemType>::InQueue(const ElemType& e)
{
	Node<ElemType>* temPtr = new Node<ElemType>(e);
	if (temPtr == NULL)
	{
		return false;
	}
	else
	{
		rear->next = temPtr;
		rear = temPtr;
		count++;
		return true;
	}

}

template<class ElemType>
bool LinkQueue<ElemType>::OutQueue(ElemType& e)
{
	if (!Empty())
	{
		Node<ElemType>* temPtr = front->next;
		e = temPtr->data;
		front->next = temPtr->next;
		if (rear == temPtr)
		{
			rear = front;
		}
		delete temPtr;
		count--;
		return true;
	}
	else
	{
		return false;
	}
}

template<class ElemType>
bool LinkQueue<ElemType>::OutQueue()
{
	if (!Empty())
	{
		Node<ElemType>* temPtr = front->next;
		front->next = temPtr->next;
		if (rear == temPtr)
		{
			rear = front;
		}
		delete temPtr;
		count--;
		return true;
	}
	else
	{
		return false;
	}
}

alg.h

#pragma once
template <class ElemType>
void Reverse(LinkQueue<ElemType>& lq)
{
	LinkStack<ElemType> ls;
	for (int i = 0; i < 9; i++)
	{
		ElemType e;
		lq.OutQueue(e);
		ls.Push(e);
	}

	for (int i = 0; i < 9; i++)
	{
		ElemType e;
		ls.Pop(e);
		lq.InQueue(e);
	}
}

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GT-一二

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值