数据结构题型12-链式队列

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 5
#define ERROR 0
#define OK    1

typedef struct LinkNode
{
	Elemtype data;
	struct LinkNode* next;
}LinkNode;

typedef struct
{
	LinkNode* front;
	LinkNode* rear;
}LinkQueue;

void InitQueue(LinkQueue& Q)  //初始化
{
	Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));//建立头节点
	Q.front->next = NULL;
}

bool IsEmpty(LinkQueue Q) //判断队空
{
	if (Q.front == Q.rear)
	{
		cout << "队空"<<endl;
		return OK;
	}
	else
	{
		cout << "队不空" << endl;
		return ERROR;
	}
}

void EnQueue(LinkQueue& Q, Elemtype x)  //入栈
{
	LinkNode *s = (LinkNode*)malloc(sizeof(LinkNode));
	s->data = x;
	s->next = NULL;
	Q.rear->next = s;
	Q.rear = s;
}

bool DeQueue(LinkQueue& Q, Elemtype& x)  //出栈
{
	if (Q.front == Q.rear) return ERROR;
	LinkNode* p = Q.front->next;
	x = p->data;
	Q.front->next = p->next;
	if (Q.rear == p)
		Q.rear = Q.front;
	free(p);
	return OK;
}

int main(void)
{
	LinkQueue Q;
	InitQueue(Q);
	EnQueue(Q, 1);
	EnQueue(Q, 2);
	EnQueue(Q, 3);
	EnQueue(Q, 4);
	EnQueue(Q, 5);
	int x = 0;
	DeQueue(Q, x);
	cout << "出栈数据为:" << x << endl;
	DeQueue(Q, x);
	cout << "出栈数据为:" << x << endl;
	DeQueue(Q, x);
	cout << "出栈数据为:" << x << endl;
	DeQueue(Q, x);
	cout << "出栈数据为:" << x << endl;
	DeQueue(Q, x);
	cout << "出栈数据为:" << x << endl;
	return 0;
}

数据结构题型12-链式队列_第1张图片

你可能感兴趣的:(算法与数据结构,数据结构)