队列 基本操作 链队列 C语言实现 数据结构

2.队列

2.1队列的概念及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out)
入队列:进行插入操作的一端称为队尾
出队列:进行删除操作的一端称为队头

2.2链队列的实现  

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType data;
}QueueNode;

typedef struct Queue
{
	QueueNode* head;
	QueueNode* tail;
}Queue;

1)初始化队列

void QueueInit(Queue* q)//初始化队列
{
	assert(q!=NULL);
	q->head = q->tail = NULL;
}

2)销毁队列

void QueueDestroy(Queue* q)//销毁队列
{
	assert(q != NULL);
	QueueNode* p = q->head;
	while (p != NULL)
	{
		QueueNode* s = p->next;
		free(p);
		p = s;
	}
}

3)队尾入队列

void QueuePush(Queue* q, QDataType x)//队尾入队列
{
	assert(q!=NULL);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)exit(1);
	newnode->data = x;
	newnode->next = NULL;
	if (q->head == NULL)
	{
		q->head=q->tail= newnode;
	}
	else
	{
		q->tail->next = newnode;
        q->tail = q->tail->next;
	}	
}

4)队头出队列

void QueuePop(Queue* q)//队头出队列
{
	assert(q != NULL);
	assert(!QueueEmpty(q));
	if (q->head->next == NULL)
	{
		free(q->head);
		q->head = q->tail = NULL;
	}
	else
	{
		QueueNode* s = q->head;
		q->head = q->head->next;
		free(s);
	}
}

5)获取队列头部元素

QDataType QueueFront(Queue* q)//获取队列头部元素
{
	assert(q != NULL);
	assert(!QueueEmpty(q));
	return q->head->data;
}

6)获取队列队尾元素

QDataType QueueBack(Queue* q)//获取队列队尾元素
{
	assert(q != NULL);
	assert(!QueueEmpty(q));
	return q->tail->data;
}

7)检测队列是否为空

bool QueueEmpty(Queue* q)//检测队列是否为空
{
	assert(q != NULL);
	return q->head == NULL;
}

8)获取队列中有效元素个数

int QueueSize(Queue* q)//获取队列中有效元素个数
{
	assert(q != NULL);
	int i = 0;
	QueueNode* s = q->head;
	while (s != NULL)
	{
		i++;
		s = s->next;
	}
	return i;
}

队列的应用场景

1.排队,保持绝对公平性

2.广度优先遍历

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
顺序队列实现: ``` #include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 typedef struct{ int data[MAXSIZE]; int front,rear; // front,rear分别指向队头和队尾位置 }SqQueue; // 初始化队列 void InitQueue(SqQueue *q){ q->front = q->rear = 0; } // 判断队列是否为空 int IsEmpty(SqQueue q){ if(q.front == q.rear) // 队列为空的条件 return 1; else return 0; } // 入队操作 int EnQueue(SqQueue *q, int x){ if((q->rear+1) % MAXSIZE == q->front) // 队列满的条件 return 0; q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXSIZE; return 1; } // 出队操作 int DeQueue(SqQueue *q, int *x){ if(q->front == q->rear) // 队列为空的条件 return 0; *x = q->data[q->front]; q->front = (q->front + 1) % MAXSIZE; return 1; } int main(){ SqQueue q; int i,x; InitQueue(&q); for(i=0; i<5; i++){ scanf("%d",&x); EnQueue(&q,x); } while(!IsEmpty(q)){ DeQueue(&q,&x); printf("%d ",x); } return 0; } ``` 链队列实现: ``` #include <stdio.h> #include <stdlib.h> typedef struct QNode{ int data; struct QNode *next; }QNode,*QueuePtr; typedef struct{ QueuePtr front,rear; // front指向队头,rear指向队尾 }LinkQueue; // 初始化队列 void InitQueue(LinkQueue *q){ q->front = q->rear = (QueuePtr)malloc(sizeof(QNode)); if(!q->front) exit(1); // 分配空间失败,直接退出 q->front->next = NULL; } // 判断队列是否为空 int IsEmpty(LinkQueue q){ if(q.front == q.rear) // 队列为空的条件 return 1; else return 0; } // 入队操作 int EnQueue(LinkQueue *q, int x){ QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); if(!p) return 0; // 分配空间失败,返回0 p->data = x; p->next = NULL; q->rear->next = p; q->rear = p; return 1; } // 出队操作 int DeQueue(LinkQueue *q, int *x){ if(q->front == q->rear) // 队列为空的条件 return 0; QueuePtr p = q->front->next; *x = p->data; q->front->next = p->next; if(q->rear == p) // 若队列中只有一个元素,那么p出队后,rear也需要改变 q->rear = q->front; free(p); return 1; } int main(){ LinkQueue q; int i,x; InitQueue(&q); for(i=0; i<5; i++){ scanf("%d",&x); EnQueue(&q,x); } while(!IsEmpty(q)){ DeQueue(&q,&x); printf("%d ",x); } return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值