数据结构与算法编程题19

单链表队列

#include 
using namespace std;

typedef int Elemtype;
#define ERROR 0
#define OK    1
typedef struct LNode
{	
	Elemtype data;
	struct LNode* next;
}LNode,*LinkList;

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

bool Init_Queue(LinkQueue& Q)
{
	Q.front = Q.rear = (LinkList)malloc(sizeof(LNode));
	if (Q.front == NULL)
	{
		cout << "内存分配失败" << endl;
		return ERROR;
	}
	Q.front->next = NULL;
	return OK;
}

bool Empty_Queue(LinkQueue& Q)
{
	if (Q.front == Q.rear)
	{
		cout << "队列为空!!!" << endl;
		return ERROR;
	}
	return OK;
}
//入队
bool Enter_Queue(LinkQueue&Q, int x)
{
	LNode* s = NULL;
	s = (LinkList)malloc(sizeof(LNode));
	if (s == NULL)
	{
		cout << "内存分配失败" << endl;
		return ERROR;
	}
	s->data = x;
	s->next = NULL;
	Q.rear->next = s;
	Q.rear = s;
	return OK;
}
//出队
bool Leave_Queue(LinkQueue& Q, int& x)
{
	if (Q.rear == Q.front)
	{
		cout << "队列为空,出队失败!!!" << endl;
		return ERROR;
	}
	LNode* p = NULL;
	p = Q.front->next;
	x = p->data;
	Q.front->next = p->next;
	if (Q.rear == p)
	{
		Q.rear = Q.front;
	}
	delete p;
	return OK;
}

int main(void)
{
	LinkQueue Q;
	Init_Queue(Q);
	return 0;
}

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