写一个链队列的入队出队,初始化的代码
时间: 2023-06-12 17:07:19 浏览: 80
链队列的定义:
```c++
typedef struct QNode{
ElemType data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
```
链队列的初始化:
```c++
void InitQueue(LinkQueue &Q){
Q.front = Q.rear = new QNode;
Q.front->next = nullptr;
}
```
链队列的入队:
```c++
void EnQueue(LinkQueue &Q, ElemType e){
QueuePtr p = new QNode;
p->data = e;
p->next = nullptr;
Q.rear->next = p;
Q.rear = p;
}
```
链队列的出队:
```c++
bool DeQueue(LinkQueue &Q, ElemType &e){
if(Q.front == Q.rear) return false;
QueuePtr p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if(Q.rear == p) Q.rear = Q.front;
delete p;
return true;
}
```
阅读全文