队列是一种先进先出的结构。 FIFO(first in first out)
顺序队列是基于顺序标配个两个下标,一个是队列头 front,一个是队列尾 rear。
顺序队列相当于对顺序表操作的一种约束:一端插入,另一端删除。
一般这种顺序队列我们不使用,因为 入队列时 rear++ 出队列时 front++
相当于每块空间指使用了一次,即使出队列了,空间也不会复用了,相当于一次性的。
- #ifndef _SEQ_QUEUE_H_
- #define _SEQ_QUEUE_H_
-
-
- #include
- #include
-
- #define N 5
-
- typedef struct _Queue{
- int s[N];
- int front;
- int rear;
- }queue_t;
-
- int print_queue(queue_t *my_queue);
- int push_queue(queue_t *my_queue, int in_data);
- int is_empty(queue_t *my_queue);
- int is_full(queue_t* my_queue);
- int create_queue(queue_t **p);
- int clean_queue(queue_t *my_queue);
- int pop_queue(queue_t *my_queue, int *out_data);
- #endif
- #include "seq_queue.h"
-
- int create_queue(queue_t **p)
- {
- if (NULL == p){
- printf("传参c错误\n");
- return -1;
- }
- *p = (queue_t*)malloc(sizeof(queue_t));
- if (*p == NULL){
- printf("内存分配失败\n");
- exit(-1);
- }
-
- (*p)->front = 0;
- (*p)->front = 0;
-
- return 0;
- }
-
- int is_full(queue_t* my_queue)
- {
- if (NULL ==my_queue){
- printf("传参c错误\n");
- return -1;
- }
-
- return (my_queue->rear+1)%N == my_queue->front?1:0;
- }
-
- int is_empty(queue_t *my_queue)
- {
- if (NULL ==my_queue){
- printf("传参c错误\n");
- return -1;
- }
-
- return my_queue->front == my_queue->rear?1:0;
- }
-
- int push_queue(queue_t *my_queue, int in_data)
- {
- if (NULL ==my_queue){
- printf("传参c错误\n");
- return -1;
- }
-
- if (is_full(my_queue)){
- printf("已经满了\n");
- return -1;
- }
-
- my_queue->s[my_queue->rear] = in_data;
- my_queue->rear = (my_queue->rear + 1)%N;
-
- return 0;
- }
-
- int print_queue(queue_t *my_queue)
- {
- if (NULL ==my_queue){
- printf("传参c错误\n");
- return -1;
- }
-
- for (int i = my_queue->front; (i%N)!=my_queue->rear; i++){
- printf("%d ",my_queue->s[i%N]);
- }
-
- puts("");
- }
-
- int clean_queue(queue_t *my_queue)
- {
- if (NULL ==my_queue){
- printf("传参c错误\n");
- return -1;
- }
-
- my_queue->front = 0;
- my_queue->rear = 0;
- }
-
- int pop_queue(queue_t *my_queue, int *out_data)
- {
- if (NULL ==my_queue){
- printf("传参c错误\n");
- return -1;
- }
-
- *out_data = my_queue->s[my_queue->front];
- my_queue->front = (my_queue->front + 1)%N;
-
- return 0;
- }
-
- int destroy_queue(queue_t **my_queue)
- {
- if (my_queue == NULL || *my_queue == NULL){
- printf("传参c错误\n");
- return -1;
- }
-
- free(*my_queue);
- *my_queue = NULL;
-
- return 0;
- }
链式队列是基于链表实现的。
链式队列相当于对链表操作的一种约束:一端插入,另一端删除。
一般使用 尾插头删法。
链式队列的操作:
创建队列
清空队列
销毁队列
入队列
出队列
判断队列是否为空
遍历队列--学习阶段看现象用的
- #ifndef _LINK_QUEUE_H_
- #define _LINK_QUEUE_H_
-
- #include
- #include
-
- typedef struct _Node{
- int data;
- struct _Node* next;
- }node_t;
-
- typedef struct _Queue{
- node_t* front;
- node_t* rear;
- }queue_t;
-
- int print_queue(queue_t *my_queue);
- int push_queue(queue_t *my_queue, int in_data);
- int create_queue(queue_t **p);
- int pop_queue(queue_t *my_queue, int *out_data);
- int is_empty(queue_t *my_queue);
- int pop_queue(queue_t *my_queue, int *out_data);
- int clean_queue(queue_t *my_queue);
- #endif
- #include "link_queue.h"
-
- int create_queue(queue_t **p)
- {
- if (p == NULL){
- printf("入参为NULL,请检查\n");
- return -1;
- }
-
- *p = (queue_t*)malloc(sizeof(queue_t));
- if (*p == NULL){
- printf("内存分配失败\n");
- exit(-1);
- }
-
- (*p)->front = NULL;
- (*p)->rear = NULL;
-
- return 0;
- }
-
- int push_queue(queue_t *my_queue, int in_data)
- {
- if (my_queue == NULL){
- printf("入参为NULL,请检查\n");
- return -1;
- }
-
- node_t* pnew = (node_t*)malloc(sizeof(node_t));
- pnew->next = NULL;
- pnew->data = in_data;
-
- if (my_queue->front == NULL && my_queue->rear == NULL){
- my_queue->front = pnew;
- my_queue->rear = pnew;
- } else {
- my_queue->rear->next = pnew;
- my_queue->rear = pnew;
- }
-
- return 0;
- }
-
- int print_queue(queue_t *my_queue)
- {
- if (my_queue == NULL){
- printf("入参为NULL,请检查\n");
- return -1;
- }
-
- node_t* temp = my_queue->front;
-
- while (temp!=NULL){
- printf("%d ",temp->data);
- temp = temp->next;
- }
- puts("");
- return 0;
- }
-
- int is_empty(queue_t *my_queue)
- {
- if (my_queue == NULL){
- printf("入参为NULL,请检查\n");
- return -1;
- }
-
- return my_queue->front == NULL?1:0;
- }
-
- int pop_queue(queue_t *my_queue, int *out_data)
- {
- if (my_queue == NULL){
- printf("入参为NULL,请检查\n");
- return -1;
- }
-
- if (is_empty(my_queue)){
- printf("队列为空,不需要出队列\n");
- return -1;
- }
-
- node_t* temp;
-
- temp = my_queue->front;
- my_queue->front = temp->next;
- *out_data = temp->data;
-
- free(temp);
- temp = NULL;
-
- return 0;
- }
-
- int clean_queue(queue_t *my_queue){
- if(NULL == my_queue){
- printf("入参为NULL, 请检查\n");
- return -1;
- }
-
- node_t *pdel = NULL;
- while(NULL != my_queue->front){
- pdel = my_queue->front;
- my_queue->front = pdel->next;
- free(pdel);
- }
- pdel = NULL;
- my_queue->rear = NULL;
- return 0;
- }
-
-
- int destroy_queue(queue_t **my_queue){
- if(NULL == my_queue || NULL == *my_queue){
- printf("入参为NULL, 请检查\n");
- return -1;
- }
-
- clean_queue(*my_queue);
-
- free(*my_queue);
- *my_queue = NULL;
- return 0;
- }