隊列的鏈隊子函數

#include<iostream>
#include<stdlib.h>

typedef int ElemType;

struct LNode
{
	ElemType data;
	LNode* next;
};

struct LinkQueue
{
	LNode* front;
	LNode* rear;
};

void InitQueue(LinkQueue& HQ)
{
	HQ.front = HQ.rear = NULL;
}

void EnQueue(LinkQueue& HQ, ElemType item)
{
	LNode* newptr = new LNode;
	newptr->data = item;
	newptr->next = NULL;
	if (HQ.rear == NULL)
		HQ.front = HQ.rear = newptr;
	else
		HQ.rear = HQ.rear->next = newptr;
}

ElemType OutQueue(LinkQueue& HQ)
{
	if (HQ.front == NULL)
	{
		std::cerr << "鏈隊爲空,無法刪除!" << std::endl;
		exit(1);
	}
	ElemType temp = HQ.front->data;
	LNode* p = HQ.front;
	HQ.front = p->next;
	if (HQ.front == NULL)
		HQ.rear = NULL;
	delete p;
	return temp;
}

ElemType PeekQueue(LinkQueue& HQ)
{
	if (HQ.front == NULL)
	{
		std::cerr << "鏈隊爲空無隊首元素!" << std::endl;
		exit(1);
	}
	return HQ.front->data;
}

bool EmptyQueue(LinkQueue& HQ)
{
	return HQ.front == NULL;
}

void ClearQueue(LinkQueue& HQ)
{
	LNode* p = HQ.front;
	while (p != NULL)
	{
		HQ.front = HQ.front->next;
		delete p;
		p = HQ.front;
	}
	HQ.rear = NULL;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章