单向队列
尾插法
- 队头(front):允许进行删除的一端称为队首。
- 队尾(tail):允许进行插入的一端称为队首。
队列先进先出,是一个特殊的线性表,只能在一端进行操作
1、初始化栈
2、出栈
3、入栈
4、判断栈是否为空
入队1,2,3,4
出队1,2,3,4
下面是存在头节点的情况
#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
int data;
struct Node* next;
}Node;
//初始化,建立一个空节点,这个头节点一直都存在队伍里面,出队的时候不会出它
Node* initQueue(){
Node* Q = (Node*)malloc(sizeof(Node));
Q->data = 0;//记录有多少个节点
Q->next = NULL;
return Q;
}
//入队
void enQueue(Node* Q, int data)
{
Node* q = Q;//定义一个指针q指向我们的头节点
Node* node = (Node*)malloc(sizeof(Node));//创建一个新节点空间
node->data = data;//将输入传入到空间中
//找到尾部节点.
for (int i = 0; i < Q->data; i++) {
q = q->next;
}
//后插
node->next = q->next;//新节点指向q的下一个节点也就插在最后一个节点与空节点之间
q->next = node;//最后节点指向新节点
Q->data++;//头节点的数值加一,也就是表示队列的节点数加一
}
//遍历
void printqueue(Node* Q) {
Node* node = Q ->next;//不打印出首节点
while (node)
{
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
//判断队列是否为空
int isEmpty(Node* Q) {
if (Q->data == 0 || Q->next == NULL) {
return 1;
}
else return 0;
}
//出队
int deQueue(Node* Q) {
if (isEmpty(Q)) {
return -1;
}
else {
Node* node = Q->next;//指向首节点的下一个节点
int data = node->data;//将将node节点的数据给备份存放在data里面
Q->next = node->next;//将头节点的指针指向node的下一个节点,跳过了node,就是把node出队了
}
}
int main(void)
{
Node*Q = initQueue();//Q指向一个初始化好的队列,只有一个记录的头节点
enQueue(Q, 1);
enQueue(Q, 2);
enQueue(Q, 3);
enQueue(Q, 4);
printqueue(Q);
deQueue(Q);
printqueue(Q);
return 0;
}
循环队列(顺序存储结构)
front指向队首,rear指向队尾
存放进去一个数据
要是队列满了,就是rear等于front,这回空队列一样了,我们需要牺牲一个空间去判断是否是否满了
队满就是队列的front与rear相差1
1、双向队列结构体
typedef struct Queue {
int front; //头部
int rear; //尾部
int data[MAXSIZE];//创建出一个数组
} Queue;
2、初始化队列
Queue* initQueue() {
Queue* Q = (Queue*)malloc(sizeof(Queue));
Q->front = Q->rear = 0;
return Q;
}
3、入队
int enQueue(Queue* Q, int data) {
if (isFull(Q)) {
return 0;
} else {
Q->data[Q->rear] = data; //将要加入的数据存放在队尾指针
Q->rear = (Q->rear + 1) % MAXSIZE; //队尾后移一格
return 1;
}
}
4、出队
int deQueue(Queue* Q) {
if (isEmpty(Q)) {
return -1;
} else {
int data = Q->data[Q->front]; //队首存放是数据
Q->front = (Q->front + 1) % MAXSIZE; //队首往后移动一个
return data;
}
}
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
/**
* define the struct of circular queue
*/
typedef struct Queue {
int front;
int rear;
int data[MAXSIZE];
} Queue;
/**
* init queue
* @return the pointer of queue
*/
Queue* initQueue() {
Queue* Q = (Queue*)malloc(sizeof(Queue));
Q->front = Q->rear = 0;
return Q;
}
/**
* print all items in queue
* @param Q the pointer of queue
*/
void printQueue(Queue* Q) {
// 要知道队列当前有多少个元素
int length = (Q->rear - Q->front + MAXSIZE) % MAXSIZE;
int index = Q->front;
for (int i = 0; i < length; i++) {
printf("%d -> ", Q->data[index]);
index = (index + 1) % MAXSIZE;
}
printf("NULL\n");
}
/**
* judge queue is or not full
* @param Q the pointer of queue
* @return full flag
*/
int isFull(Queue* Q) {
if ((Q->rear + 1) % MAXSIZE == Q->front) {
return 1;
} else {
return 0;
}
}
/**
* judge queue is or not empty
* @param Q the pointer of queue
* @return empty flag
*/
int isEmpty(Queue* Q) {
if (Q->front == Q->rear) {
return 1;
} else {
return 0;
}
}
/**
* enqueue
* @param Q the pointer of queue
* @param data the data you want to enqueue
* @return success flag
*/
int enQueue(Queue* Q, int data) {
if (isFull(Q)) {
return 0;
} else {
Q->data[Q->rear] = data;
Q->rear = (Q->rear + 1) % MAXSIZE;
return 1;
}
}
/**
* dequeue
* @param Q the pointer of queue
* @return the data you want to dequeue
*/
int deQueue(Queue* Q) {
if (isEmpty(Q)) {
return -1;
} else {
int data = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return data;
}
}
/**
* main function
* @return null
*/
int main() {
Queue* Q = initQueue();
enQueue(Q, 1);
enQueue(Q, 2);
enQueue(Q, 3);
enQueue(Q, 4);
printQueue(Q);
deQueue(Q);
printQueue(Q);
return 0;
}