[C/C++]C语言之队列(Queue)

概述

和栈相反,队列(queue)是一种先进先出(first in first out,缩写FIFO)的线性表。它只允许在表的一端进行插入,而在另一端删除元素。


1、抽象数据类型定义

  基本操作

InitQueue(&Q)
    操作结果:构建一个空队列Q
DestroyQueue(&Q)
    操作结果:队列Q被销毁,不再存在
ClearQueue(&Q)
    操作结果:将队列Q清空
QueueIsEmpty(Q)
    操作结果:判断队列是否为空
QueueLength(Q)
    操作结果:返回Q的元素个数,即队列长度
QueueHead(Q)
    操作结果:用e返回队列Q的头元素
EnQueue(&Q, e)
    操作结果:出入元素e为队列Q的新的队尾元素
DeQueue(&Q, &e)
    操作结果:删除Q的队头元素,并用e返回其值
QueueTraverse(Q, visit())
    操作结果:从队头到队尾,依次对Q的每个数据元素调用函数visit()。一旦visit()失败,则操作失败

2、循环队列 

    从上图3.13可看出,在C语言中不能用动态分配的一维数组来实现循环链表。如果用户的应用程序中设有循环队列,则必须为它设定一个最大队列长度;若用户无法预估所用队列的最大长度,则以采用链队列。

                                  

  队列的顺序存储结构

#define MAXQSIZE     100    //最大队列长度

typedef struct
{
    QElemType     *base;
    int    front;      //队首指针
    int    rear;       //队尾指针
}SqQueue;

  基本操作算法

Status InitQueue(SqQueue &Q)
{
    Q.base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType));
    if(!Q.base) exit(OVERFLOW);
    Q.rear = 0;
    Q.front = 0;
    return OK;
}

int isEmpty(SqQueue Q)
{
    return (Q.front == Q.rear ? 1 : 0);
}

int QueueLength(SqQueue Q)
{
    return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}

Status EnQueue(SqQueue &Q, QElemType e)
{
    if((Q.rear+1)%MAXQSIZE== Q.front)
    {
        return ERROR;
    }

    Q.base[Q.rear] = e;
    Q.rear = (Q.rearr+1)%MAXQSIZE;

    return OK;
}

Status DeQueue(SqQueue &Q, QElemType &e)
{
    if(Q.rear == Q.front)
    {
        return ERROR;
    }
    
    e = Q.base[Q.front];
    Q.front = (Q.front+1)%MAXQSIZE;
    
    return OK;
}

3、链队列

                       

#include "stdio.h"

#define QueueSize 100 /*假定预分配的队列空间最多为100个元素*/  
typedef char DataType ; /*假定队列元素的数据类型为字符*/

typedef struct node{
	DataType data;
	struct node *next;
}QueueNode;

typedef struct{
	QueueNode *front;  /*头指针*/
	QueueNode *rear;
}LinkQueue;


/* 置队列空*/
void Initial(LinkQueue *Q)
{    
    Q->front=Q->rear=NULL;/*将顺序队列置空*/
} 

/*判队列空*/
int IsEmpty(LinkQueue *Q)
{
    return Q->front==NULL&&Q->rear==NULL;
}

/*进队列*/
void Push(LinkQueue *Q,DataType x)
{
    /*将元素x插入链队列尾部*/
	QueueNode *p=(QueueNode *)malloc(sizeof(QueueNode));/*申请新结点*/
	p->data=x;
	p->next=NULL;
    if(IsEmpty(Q))
		Q->front=Q->rear=p;  /*将x插入空队列*/
	else 
	{   /*x插入非空队列的尾*/
		Q->rear->next=p;     /*p链到原队尾结点后*/
		Q->rear=p;           /*队尾指针指向新的尾*/
	}
}

/*出队列*/
DataType Pop(LinkQueue *Q)
{
	DataType x;
	QueueNode *p;
	if(IsEmpty(Q))
	{
		printf("队列为空");/*下溢*/
		exit(1);
	}
	p=Q->front;                   /*指向对头结点*/
	x=p->data;                    /*保存对头结点的数据*/
	Q->front=p->next;             /*将对头结点从链上摘下*/
    if(Q->rear==p)/*原队中只有一个结点,删去后队列变空,此时队头指针已为空*/
		Q->rear=NULL;
	free(p);   /*释放被删队头结点*/
	return x;  /*返回原队头数据*/
}

/* 取队列顶元素*/
DataType Front(LinkQueue *Q)
{
	if(IsEmpty(Q))
	{
		printf("队列为空"); /*下溢,退出运行*/
		exit(1);
	}
	return Q->front->data;
}

void main()
{
	LinkQueue s;
	DataType first,sec;
	Initial(&s);
	Push(&s,'a');
	Push(&s,'b');
	first=Front(&s);
	Pop(&s);
	sec=Front(&s);
	Pop(&s);
}

 

 

 

 

 

 

 

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在C语言中,标准库中并没有内置的队列queue)库。如果您使用了第三方队列库,那么具体的查看队列长度函数会依赖于您使用的库。 下面是一些常见的第三方队列库及其查看队列长度的函数: 1. C++ STL中的队列queue)库:如果您在C++中使用STL的队列库,可以通过 `size()` 函数查看队列中元素的个数,例如: ``` #include <queue> using namespace std; queue<int> q; int len = q.size(); // 获取队列长度 ``` 2. C 语言中的 `queue.h` 库:如果您使用的是C 语言中的 `queue.h` 库,可以通过 `queue_size()` 函数查看队列中元素的个数,例如: ``` #include <stdio.h> #include <stdlib.h> #include <queue.h> int main() { QUEUE *q = que_init(); que_push(q, 1); que_push(q, 2); que_push(q, 3); int len = queue_size(q); // 获取队列长度 printf("Queue size: %d\n", len); que_free(q); return 0; } ``` 请注意,以上示例代码仅供参考,具体实现可能会因库的版本或个人编写方式而有所不同。 ### 回答2: C语言中的queue库并不是标准库的一部分,它是一个由用户自己实现的队列库。因此,并没有一个固定的函数来查看队列的长度。 用户可以通过在队列结构体中添加一个表示队列当前长度的变量来实现此功能。在进行入队操作时,该变量加1;在出队操作时,该变量减1。这样,通过直接访问该变量即可得到队列的当前长度。 下面是一个示例代码: ``` #include <stdio.h> #define MAX_SIZE 100 // 定义队列的最大容量 typedef struct { int data[MAX_SIZE]; // 用数组来存储队列元素 int front; // 队首指针 int rear; // 队尾指针 int length; // 队列长度 } Queue; // 初始化队列 void initQueue(Queue *queue) { queue->front = 0; queue->rear = -1; queue->length = 0; } // 入队操作 void enqueue(Queue *queue, int element) { if (queue->length == MAX_SIZE) { printf("Queue is full!\n"); return; } queue->rear = (queue->rear + 1) % MAX_SIZE; queue->data[queue->rear] = element; queue->length++; } // 出队操作 int dequeue(Queue *queue) { if (queue->length == 0) { printf("Queue is empty!\n"); return -1; } int element = queue->data[queue->front]; queue->front = (queue->front + 1) % MAX_SIZE; queue->length--; return element; } // 查看队列长度 int getQueueLength(Queue *queue) { return queue->length; } int main() { Queue queue; initQueue(&queue); enqueue(&queue, 1); enqueue(&queue, 2); enqueue(&queue, 3); printf("Queue length: %d\n", getQueueLength(&queue)); dequeue(&queue); printf("Queue length: %d\n", getQueueLength(&queue)); return 0; } ``` 以上代码实现了一个队列的基本操作,并通过`getQueueLength`函数获取队列的长度。 ### 回答3: c中queue库的查看队列长度的函数是`size()`函数。 `size()`函数用于返回队列中的元素个数。调用方法如下: ``` #include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <queue.h> int main() { queue *q = queue_creare(); // 向队列中添加元素 queue_push(q, 1); queue_push(q, 2); queue_push(q, 3); // 查看队列的长度 int length = size(q); printf("队列长度为:%d\n", length); // 释放队列 queue_destroy(q); return 0; } ``` 在上述例子中,首先使用`queue_create()`函数创建了一个空队列`q`,然后使用`queue_push()`函数向队列中添加了三个元素,接着使用`size()`函数获取了队列的长度并存储在变量`length`中,最后使用`printf()`函数将队列长度输出到屏幕上。最后使用`queue_destroy()`函数释放了队列所占用的内存。 这样就可以通过`size()`函数获取队列的长度了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值