C language/C++ queue detailed tutorial and complete code implementation

C language/C++ queue detailed tutorial and complete code implementation

Queue is a commonly used data structure, which follows the principle of first-in-first-out (FIFO), that is, elements that enter the queue first will be taken out first. Queues can be used to solve many practical problems, such as task scheduling, message delivery, etc. In this tutorial, we will introduce the concept of queues, basic operations and how to implement a queue in C language in detail.

The concept of queue

A queue is a linear data structure that includes two basic operations: enqueue and dequeue. The enqueue operation adds elements to the tail of the queue, and the dequeue operation removes and returns the head element of the queue.

Queue features:

  • First in, first out (FIFO): Elements that enter the queue first will be taken out first.
  • The enqueue operation can only be performed at one end of the queue, which is called the tail of the queue.
  • Dequeue operations can only be performed at the other end of the queue, known as the head of the queue.

Basic operation of the queue

The basic operations of the queue include:

  • Create Queue: Initialize an empty queue.
  • Enqueue operation: add elements to the tail of the queue.
  • Dequeue operation: remove and return the head element of the queue.
  • Null judgment operation: judge whether the queue is empty.
  • Judgment full operation: determine whether the queue is full.
  • Get the head element of the queue: returns the head element of the queue, but does not remove it.

Implementation of the queue

The following is the code to implement a queue in C language:

#include <stdio.h>
#define MAX_SIZE 100

// 定义队列结构
typedef struct {
    
    
    int data[MAX_SIZE];
    int front;
    int rear;
} Queue;

// 初始化队列
void initQueue(Queue *queue) {
    
    
    queue->front = queue->rear = 0;
}

// 判断队列是否为空
int isEmpty(Queue *queue) {
    
    
    return queue->front == queue->rear;
}

// 判断队列是否已满
int isFull(Queue *queue) {
    
    
    return queue->rear == MAX_SIZE;
}

// 入队
void enqueue(Queue *queue, int item) {
    
    
    if (isFull(queue)) {
    
    
        printf("队列已满,无法入队!\n");
        return;
    }
    queue->data[queue->rear++] = item;
}

// 出队
int dequeue(Queue *queue) {
    
    
    if (isEmpty(queue)) {
    
    
        printf("队列为空,无法出队!\n");
        return -1;
    }
    return queue->data[queue->front++];
}

// 获取队列长度
int getSize(Queue *queue) {
    
    
    return queue->rear - queue->front;
}

// 打印队列中的所有元素
void printQueue(Queue *queue) {
    
    
    if (isEmpty(queue)) {
    
    
        printf("队列为空!\n");
        return;
    }
    printf("队列中的元素为:");
    for (int i = queue->front; i < queue->rear; i++) {
    
    
        printf("%d ", queue->data[i]);
    }
    printf("\n");
}

int main()
{
    
    
    Queue queue;
    initQueue(&queue);
  
    enqueue(&queue, 10);
    enqueue(&queue, 20);
    enqueue(&queue, 30);
  
    printQueue(&queue); // 队列中的元素为:10 20 30
  
    printf("出队元素:%d", dequeue(&queue)); // 出队元素:10
  
    printQueue(&queue); // 队列中的元素为:20 30
  
    return 0;
}

In the above code, we use an array to store the elements of the queue, and use two pointers to point to the head frontand reartail of the queue respectively. Through the modulo operation, the function of the circular queue can be realized.

In mainthe function, we first initialize an empty queue, then perform enqueue and dequeue operations, and output the results.

Summarize

Queue is a commonly used data structure with the characteristics of first-in first-out. Queues can play an important role when solving practical problems. Through the implementation code of C language, we can better understand the concept and basic operation of the queue. I hope this tutorial can help you understand queues!

Guess you like

Origin blog.csdn.net/qq_43884946/article/details/131475899