假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点,试编写相应的置空队、判队空、入队和出队算法
时间: 2024-04-30 11:02:06 浏览: 5
好的,以下是对应的算法:
1. 置空队:
```
void InitQueue(LinkQueue &Q) {
Q.front = new QueueNode;
Q.rear = Q.front;
Q.front->next = Q.rear;
}
```
2. 判队空:
```
bool QueueEmpty(LinkQueue Q) {
return Q.front == Q.rear;
}
```
3. 入队:
```
void EnQueue(LinkQueue &Q, ElemType x) {
QueueNode *p = new QueueNode;
p->data = x;
p->next = Q.rear->next;
Q.rear->next = p;
Q.rear = p;
}
```
4. 出队:
```
bool DeQueue(LinkQueue &Q, ElemType &x) {
if (QueueEmpty(Q)) {
return false;
}
QueueNode *p = Q.front->next;
x = p->data;
Q.front->next = p->next;
if (p == Q.rear) {
Q.rear = Q.front;
}
delete p;
return true;
}
```
小笑话:为什么大象会选择在冬天跳入河里呢?因为那样比起从飞机上跳伞来,更容易把鼻子藏起来!
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)