3.2 栈和队列-队列


链接: https://gitee.com/michael_linux/data-struct-study.git

1、队列的类型定义

ADT Queue{
数据对象:
D = { a i ∣ a i ∈ E l e m S e t ;   i = 1 , 2 , . . . , n ;   n ⩾ 0 } D=\{a_{i} | a_{i} \in ElemSet; \ i=1,2,...,n;\ n \geqslant 0\} D={aiaiElemSet; i=1,2,...,n; n0}

数据关系:
R 1 = { < a i − 1 , a i > ∣ a i − 1 , a i ∈ D ;   i = 1 , 2 , . . . , n } 约定 a 1 端为队列头, a n 端为队列尾。 \begin{aligned}R1=\{&<a_{i-1},a_{i}> | a_{i-1},a_{i} \in D;\ i=1,2,...,n\}\\ &约定a_{1}端为队列头,a_{n}端为队列尾。\end{aligned} R1={<ai1,ai>ai1,aiD; i=1,2,...,n}约定a1端为队列头,an端为队列尾。

基本操作:
基本操作:
InitQueue(&Q)
操作结果:构造一个空队列Q。

DestroyQueue(&Q)
初始条件:队列Q已存在。
操作结果:队列Q被销毁,不再存在。

QueueEmpty(Q)
初始条件:队列Q已存在。
操作结果:若Q为空队列,则返回TRUE,否则FALSE。

QueueLength(Q)
初始条件:队列Q已存在。
操作结果:返回Q的元素个数,即队列的长度。

GetHead(Q, &e)
初始条件:Q为非空队列。
操作结果:用e返回Q的队头元素。

ClearQueue(&Q)
初始条件:队列Q已存在。
操作结果:将Q清为空队列。

EnQueue(&Q, e)
初始条件:队列Q已存在。
操作结果:插入元素e为Q的新的队尾元素。

DeQueue(&Q, &e)
初始条件:Q为非空队列。
操作结果:删除Q的队头元素,并用e返回其值。

} ADT Queue

2、队列类型的实现

2.1、链队列:链式映象

//链队结点类型定义
typedef struct QNode {
	int data;//数据域
	struct QNode *next;//指针域
}QNode;

//链队类型定义
typedef struct LiQueue {
	struct QNode *front;//队头指针
	struct QNode *rear;//队尾指针
}LiQueue;//链队类型定义


void initQueue(LiQueue *&lqu){
	lqu=(LiQueue*)malloc(sizeof(LiQueue));
	lqu->front=lqu=>rear=NULL;
}

int isQueueEmpty(LiQueue *lqu){
	if(lqu->front==NULL || lqu->rear==NULL){
		return 1;
	}else{
		return 0;
	}
}

void enQueue(LiQueue *lqu, int x){
	QNode *p;
	p = (QNode*)malloc(sizeof(QNode));
	p->data=x;
	p->next=NULL;
	if(lqu->rear == NULL){//若队列为空,则新节点是队首节点,也是队尾结点;
		lqu->front=lqu->rear=p;
	}else{//将新节点链接到队尾,rear指向它
		lqu->rear->next=p;
		lqu->rear=p;
	}
}

int deQueue(Lilqueue *lqu, int &x){
	QNode *p;
	if(lqu->rear == NULL){
		return 0;
	}else{
		p=lqu->front;
	}
	
	if(lqu->front == lqu->rear){
		lqu->front = lqu->rear = NULL;
	}else{
		lqu->front=lqu->front->next;
	}
	x=p->data;
	free(p);
	return 1;
}

2.2、循环队列:顺序映象

#define MAX_Q_SIZE 100 //最大队列长度
typedef struct{
QElemType *base; //动态分配存储空间
int front; //头指针,若队列不空,指向队列头元素
int rear; //尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;

另一种描述:

//顺序队列定义
typedef struct {
	int data[maxSize];
	int front;//队首指针
	int rear;//队尾指针
}SqQueue;//顺序队类型定义


循环队列的要素
队空状态:qu.rear == qu.front
对满状态:(qu.rear)%maxSize == qu.front
进队操作:
qu.rear = (qu.rear+1)%maxSize;
qu.data[qu.rear]=x;
出队操作:
qu.front=(qu.front+1)%maxSize;
x=qu.data[qu.front];


void initQueue(SqQueue &qu){
	qu.front=qu.rear=0;//队首和队尾指针重合,并且指向0;
}

int isQueueEmpty(SqQueue qu){
	if(qu.front == qu.rear){//不论队首队尾指针指向数组中的哪个位置,只要两者重合,即为队空。
		return 1;
	}else{
		return 0;
	}
}

int enQueue(SqQueue &qu, int x){
	if((qu.rear+1)%maxSize == qu.front){//队满
		return 0;
	}
	
	qu.rear = (qu.rear+1)%maxSize;
	qu.data[qu.rear]=x;
	return 1;
}


int deQueue(SqQueue &qu, int &x){
	if(qu.front == qu.rear){//若队空,则不能出队
		return 0;
	}
	qu.front = (qu.front+1)%maxSize;
	x=qu.data[qu.front];
	return 1;
}
// 静态链表 循环链表
# include <stdio.h>
# include <malloc.h>

typedef struct Queue{
	int * pBase;
	int front;
	int rear;
}QUEUE;  

void init(QUEUE *);
bool en_queue(QUEUE *, int val);  //入队
void traverse_queue(QUEUE *);
bool full_queue(QUEUE *);
bool out_queue(QUEUE *, int *);
bool empty_queue(QUEUE *);

int main(void)
{
	QUEUE Q;
	int val;

	init(&Q);
	en_queue(&Q, 1);
	en_queue(&Q, 2);
	en_queue(&Q, 3);
	en_queue(&Q, 4);
	en_queue(&Q, 5);
	en_queue(&Q, 6);
	en_queue(&Q, 7);
	en_queue(&Q, 8);

	traverse_queue(&Q);

	if ( out_queue(&Q, &val) )
	{
		printf("出队成功,队列出队的元素是: %d\n", val);
	}
	else
	{
		printf("出队失败!\n");
	}
	traverse_queue(&Q);

	return 0;
}

void init(QUEUE *pQ)
{
	pQ->pBase = (int *)malloc(sizeof(int) * 6);
	pQ->front = 0;
	pQ->rear = 0;
}

bool full_queue(QUEUE * pQ)
{
	if ( (pQ->rear + 1) % 6 == pQ->front  )
		return true;
	else
		return false;
}

bool en_queue(QUEUE * pQ, int val)
{
	if ( full_queue(pQ) )
	{
		return false;
	}
	else
	{
		pQ->pBase[pQ->rear] = val;
		pQ->rear = (pQ->rear+1) % 6;

		return true;
	}
}

void traverse_queue(QUEUE * pQ)
{
	int i = pQ->front;

	while (i != pQ->rear)
	{
		printf("%d  ", pQ->pBase[i]);
		i = (i+1) % 6;
	}
	printf("\n");

	return;
}

bool empty_queue(QUEUE * pQ)
{
	if ( pQ->front == pQ->rear )
		return true;
	else
		return false;
}

bool out_queue(QUEUE * pQ, int * pVal)
{
	if ( empty_queue(pQ) )
	{
		return false;
	}
	else
	{
		*pVal = pQ->pBase[pQ->front];
		pQ->front = (pQ->front+1) % 6;

		return true;
	}
}

4、学习目标

  1. 掌握栈和队列类型的特点,并能在相应的应用问题中正确选用它们。
  2. 熟练掌握栈类型的两种实现方法,特别应注意栈满和栈空的条件以及它们的描述方法。
  3. 熟练掌握循环队列和链队列的基本操作实现算法,特别注意队满和队空的描述方法。
  4. 理解递归算法执行过程中栈的状态变化过程。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值