链队相关操作
- 初始化队列
- 入队
- 出队
#include "stdafx.h"
#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int ELEMTYPE;
typedef int Status;
typedef struct LNode
{
ELEMTYPE data;
struct LNode *next;
}LNode,*Queueptr;
typedef struct
{
Queueptr front;
Queueptr rear;
}LinkQueue;
Status initLinkQueue(LinkQueue &Q)
{
Q.front = Q.rear = new LNode;
if(!Q.front) return OVERFLOW;
Q.front->next = NULL;
}
bool isEmpty(LinkQueue &Q)
{
if (Q.front->next == NULL)
return true;
else
return false;
}
void EnQueue(LinkQueue &Q,ELEMTYPE e)
{
LNode *p = (LNode *)malloc(sizeof(LNode));
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
}
void ExQueue(LinkQueue &Q,ELEMTYPE &e)
{
if(isEmpty(Q))
return;
LNode *p = Q.front->next;
e = p->data;
Q.front->next = Q.front->next->next;
delete (p);
if (isEmpty(Q))
Q.rear = Q.front;
}
void Visit(LinkQueue &Q)
{
LNode *p = Q.front->next;
while(p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}