队列
本质:是一种线性表,但是它规定先进先出
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;
}