栈和队列(5.18)

队列

 

本质:是一种线性表,但是它规定先进先出

 

1.循环队列

 

<1>数据结构

typedef struct 
{
	DATATYPE buf[MAX];
	int front;//出队的时候操作
	int rear;//进队的时候操作
}LoopQueue;

原则:先进先出(尾入头出)

 

<2>规定

[1]初始化的时候front和rear为0

[2]规定队空的条件 front == rear

[3]队满的条件 (rear + 1) % MAX == front

[4]front 和 rear 更新的方法 value = (value + 1) % MAX;

例如:

front = (front + 1) % MAX; 
//front ++;
rear  = (rear +  1) % MAX;


特点:

[1]会浪费一个空间,目的是为了区分队空和队满

[2]随着出队的进行可以再次进队

LoopQueue *create_loopqueue();
int is_empty_loopqueue(LoopQueue *q);
int is_full_loopqueue(LoopQueue *q);
int enter_loopqueue(LoopQueue *q,DATATYPE data);
DATATYPE delete_loopqueue(LoopQueue *q);


2.链式队列

 

(1)数据结构 

 

[1]链表节点类型

typedef struct node 
{
	DATATYPE data;
	struct node *next;
}LinkNode;


[2]队列的头 

typedef struct 
{
	LinkNode *front;//记录链表的头
	LinkNode *rear;//记录链表的尾
}LinkQueue;


(2)创建一个空链式队列

 

LinkQueue *create_empty_linkqueue()
{
	LinkNode *head;
	LinkQueue *q;

	head = (LinkNode *)malloc(sizeof(LinkNode));
	head->next = NULL;

	q = (LinkQueue *)malloc(sizeof(LinkQueue));
	q->front = q->rear = head;

	return q;
}

 

(3)队列空的条件 

int is_empty_linkqueue(LinkQueue *q)
{
	return q->front == q->rear ? 1 : 0;
}

(4)进队 

int enter_linkqueue(LinkQueue *q,DATATYPE data);

(5)出队 

DATATYPE delete_linkqueue(LinkQueue *q);

代码如下:

SeqQueue:

#include<stdio.h>
#include <stdlib.h>

#define MAX 5 

typedef int DATATYPE;

typedef  struct 
{
	DATATYPE buf[MAX];
	int rear;
	int front;
}SeqQueue;


SeqQueue *create_queue()
{
	SeqQueue *q;

	q = (SeqQueue *)malloc(sizeof(SeqQueue));
	q->front = q->rear = 0;

	return q;
}

int is_full_queue(SeqQueue *q)
{
	return q->rear == MAX ? 1 : 0;
}

int is_empty_queue(SeqQueue *q)
{
	return q->front == q->rear ? 1 : 0;
}

int enter_queue(SeqQueue *q,DATATYPE data)
{
	if(is_full_queue(q))
	{
		printf("The queue is full!\n");
		return -1;
	}

	q->buf[q->rear] = data;
	q->rear ++;

	return 0;
}

DATATYPE delete_queue(SeqQueue *q)
{
	DATATYPE data;
	
	if(is_empty_queue(q))
	{
		printf("The queue is empty!\n");
		return -1;
	}

	data = q->buf[q->front];
	q->front ++;

	return data;
}

int main(int argc, const char *argv[])
{
	int i = 1;
	SeqQueue *q = create_queue();

	while(!is_full_queue(q))
	{
		enter_queue(q,i);
		i ++;
	}

	while(!is_empty_queue(q))
	{
		printf("%d ",delete_queue(q));
	}
	printf("\n");
	
	enter_queue(q,100);

	return 0;
}
loopqueue:

#include <stdio.h>
#include <stdlib.h>

#define MAX 5

typedef int DATATYPE;

typedef struct 
{
	DATATYPE buf[MAX];
	int front;
	int rear;
}LoopQueue;

LoopQueue *create_loopqueue()
{
	LoopQueue *q;

	q = (LoopQueue *)malloc(sizeof(LoopQueue));
	q->front = q->rear = 0;

	return q;
}

int is_empty_loopqueue(LoopQueue *q)
{
	return q->front == q->rear ? 1 : 0;
}

int is_full_loopqueue(LoopQueue *q)
{
	return (q->rear + 1) % MAX == q->front;
}

int enter_loopqueue(LoopQueue *q,DATATYPE data)
{
	if(is_full_loopqueue(q))
	{
		printf("The LoopQueue is full!\n");
		return -1;
	}

	q->buf[q->rear] = data;
	//q->rear ++;
	q->rear = (q->rear + 1) % MAX;

	return 0;
}

DATATYPE delete_loopqueue(LoopQueue *q)
{
	DATATYPE data;

	if(is_empty_loopqueue(q))
	{
		printf("The LoopQueue is empty!\n");
		return -1;
	}

	data = q->buf[q->front];
	q->front = (q->front + 1) % MAX;

	return data;
}

int main(int argc, const char *argv[])
{
	int i = 0;
	LoopQueue *q = create_loopqueue();

	while(!is_full_loopqueue(q))
	{
		enter_loopqueue(q,++i);
	}

	while(!is_empty_loopqueue(q))
	{
		printf("%d ",delete_loopqueue(q));
	}
	printf("\n");

	enter_loopqueue(q,100);

	printf("%d\n",delete_loopqueue(q));

	return 0;
}
linkqueue:

#include <stdio.h>
#include <stdlib.h>

typedef int DATATYPE;

typedef struct node 
{
	DATATYPE data;
	struct node *next;
}LinkNode;

typedef struct 
{
	LinkNode *front;
	LinkNode *rear;
}LinkQueue;

LinkQueue *create_empty_linkqueue()
{
	LinkNode *head;
	LinkQueue *q;

	head = (LinkNode *)malloc(sizeof(LinkNode));
	head->next = NULL;

	q = (LinkQueue *)malloc(sizeof(LinkQueue));
	q->front = q->rear = head;

	return q;
}

int is_empty_linkqueue(LinkQueue *q)
{
	return q->front == q->rear ? 1 : 0;
}

int enter_linkqueue(LinkQueue *q,DATATYPE data)
{
	LinkNode *temp;

	temp = (LinkNode *)malloc(sizeof(LinkNode));
	temp->data = data;
	temp->next = NULL;

	q->rear->next = temp;
	q->rear = temp;

	return 0;
}

DATATYPE delete_linkqueue(LinkQueue *q)
{
	LinkNode *temp;

	temp = q->front;
	q->front = temp->next;
	free(temp);
	
	return q->front->data;
}

void answer_poker()
{
	int num = 1;
	int i = 0;
	char result[6] = {'4','3','A','K','8','J'}; 
	char ram[6];

	LinkQueue *q = create_empty_linkqueue();

	for(num = 0;num < sizeof(result)/sizeof(result[0]);num ++)
	{
		enter_linkqueue(q,num);
	}

	while(!is_empty_linkqueue(q))
	{
		//第一张 出来后直接进队
		enter_linkqueue(q,delete_linkqueue(q));
		
		ram[delete_linkqueue(q)] = result[i ++];	
	}

	for(num = 0;num <  sizeof(result)/sizeof(result[0]);num ++)
	{
		printf("%d : %c\n",num + 1,ram[num]);
	}
		
	return;
}

int main(int argc, const char *argv[])
{
	int i = 0;
	LinkQueue *q = create_empty_linkqueue();
	
	for(i = 1;i < 10;i ++)
	{
		enter_linkqueue(q,i);
	}

	while(!is_empty_linkqueue(q))
	{
		printf("%d ",delete_linkqueue(q));
	}
	printf("\n");

	enter_linkqueue(q,100);
	printf("%d\n",delete_linkqueue(q));

	answer_poker();

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值