数据结构-非循环链队的c++实现

#pragma once
//非循环链队
#ifndef My_Head_H
	#define My_Head_H
	#include "G://code/c++/myhead.h"
#endif // !My_Head_H

template <typename ElemType>
class LinkQueue
{
private:
	class Queue_Node
	{
	public:
		ElemType data;
		Queue_Node* next;
	};
	typedef Queue_Node* Nodepointer;

public:
	//把队列置空
	void clear();

	//出队列(删除链队队头结点)
	Status deQueue(ElemType& e);

	//进队列(在队尾加入结点)
	void inQueue(ElemType e);

	//求链队中结点个数
	int Get_Length();

	//以随机数填充i个结点
	void Random_Fill_Queue(int i);

	//打印所有结点
	void Display_Queue();

	LinkQueue();
	~LinkQueue();

protected:
	Nodepointer rear; //队尾指针
	Nodepointer front;//队首指针

};

template<typename ElemType>
void LinkQueue<ElemType>::clear()
{
	Nodepointer q;
	Nodepointer p = front;
	while (p)
	{
		q = p;
		p = p->next;
		delete q;
	}
	front = NULL;
	rear = NULL;
}

template<typename ElemType>
Status LinkQueue<ElemType>::deQueue(ElemType& e)
{
	if (!front) return ERROR;
	Nodepointer p = front;
	e = p->data;
	front = front->next;
	delete p;
	if (!front) rear = NULL;
	return OK;
}

template<typename ElemType>
void LinkQueue<ElemType>::inQueue(ElemType e)
{
	Nodepointer p = new Queue_Node;
	p->data = e;
	p->next = NULL;
	if (!front)
	{
		front = p;
		rear = p;
	}
	else
	{
		rear->next = p;
		rear = p;
	}
}

template<typename ElemType>
int LinkQueue<ElemType>::Get_Length()
{
	int length = 0;
	Nodepointer p = front;
	while (p)
	{
		length++;
		p = p->next;
	}
	return length;
}

template<typename ElemType>
void LinkQueue<ElemType>::Random_Fill_Queue(int i)
{
	for (int count = 0; count < i; count++)
	{
		inQueue(random(100));
	}
}

template<typename ElemType>
void LinkQueue<ElemType>::Display_Queue()
{
	cout << "Here is all Queue data:" << endl;
	Nodepointer p = front;
	while (p)
	{
		cout << "->" << p->data;
		p = p->next;
	}
	cout << endl;
}

template <typename ElemType>
LinkQueue<ElemType>::LinkQueue()
{
	front = NULL;
	rear = NULL;
}

template <typename ElemType>
LinkQueue<ElemType>::~LinkQueue()
{
	clear();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值