05 -1队列-链式队列

/*
	2008年3月26日16:31:56
	链式队列
*/

# include <iostream>
using namespace std;

typedef struct node
{
	int data;
	struct node *pNext;
}NODE, *PNODE;

class Queue
{
public:
	Queue()
	{
		this->pHead = this->pTail = new NODE;
		this->pHead->pNext = NULL;
	}
	
	void InQueue(int val)
	{
		PNODE pNew = new NODE;

		pNew->data = val;
		pNew->pNext = NULL;
		
		pTail->pNext = pNew; //将pNew挂到队列尾部 
		pTail = pNew; //注意是尾指针上移
	
		return;
	}

	bool Empty() const
	{
		if (this->pHead == pTail)
			return true;
		else
			return false;
	}

	int OutQueue()
	{
		if (Empty())
		{
			cout <<"队列为空,无法出队!" << endl;
		}
		else
		{
			PNODE pTemp = pHead->pNext; //pHead不是要删除的队首元素,pHead->pNext所指向的元素才是要删除的元素,
			pHead->pNext = pTemp->pNext;
			int val = pTemp->data;

			delete pTemp;

			if (NULL == pHead->pNext) //如果队列为空
			{
				pTail = pHead; //尾指针也指向无用的头结点
			}
			
			return val;
		}
	}

	//遍历队列
	void Travers(void) const
	{
		PNODE pTemp = pHead->pNext;

		while (pTemp != NULL)
		{
			cout << pTemp->data << "  ";
			pTemp = pTemp->pNext;
		}
		cout << endl;
	}

	void Clear()
	{
		while (! this->Empty())
		{
			OutQueue();
		}
	}
	
	~Queue()
	{
		this->Clear(); 
		delete pHead;
	}

private:
	PNODE pHead, pTail; //pHead指向无用的头结点 pHead->pNext才是指向队首元素, pTail指向队尾元素
};

int main(void)
{
	Queue Q;

	for (int i=0; i<5; ++i)
		Q.InQueue(i+1);

	Q.Travers();

	Q.OutQueue();
	Q.OutQueue();

	Q.Travers();

	Q.Clear();
	Q.OutQueue();

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值