【Data Structure Learning Record 8】——Queue

1. Concept

Contrary to a stack, a queue is a 先进先出(first in first out = FIFO)structure. Is FIFO the FIFO of the microcontroller? It is a linear table that only allows inserting elements at one end of the table and deleting elements at the other end. In the queue, the end that can be inserted is called 队尾, and the end that is allowed to be deleted is called 队头.

2. Principle realization

1. Node

Because our queue is still a linear table, the nodes of our queue should still be composed of two parts: 数据区and 指向下一个节点的指针.
Insert picture description here

2. Queue

If we want to implement a queue, we only need two pointers and point to it 队头和队尾.
It is best to use chain storage, because it 队头will often change, if it adopts a sequential structure, it may waste space or frequently modify a large number of elements.
At the time of initialization, we can 队头队尾point all to the same node, and the next node to which the node points is NULL.
Insert picture description here

3. Join the team

For enqueue, we only need to insert the element after the end of the team, and then point the end of the team to it.
Insert picture description here

4. Get out of the team

Save the address of the next node pointed to by the head of the team, record it as address A, point the head of the team to the next node of address A, and then freedrop A.
Insert picture description here

5. Destroy

Destroy a queue, just start over from the team, followed by all the nodes freeoff

Three. Code

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

#define OK      1
#define ERROR   0

typedef struct dataType{
    
    
    int data;
}dataType;

typedef struct nodeType{
    
    
    dataType data;
    struct nodeType *next;
}nodeType;

typedef struct queue{
    
    
    nodeType *head;
    nodeType *tail;
}queue;

queue* QueueInit(void);
int QueuePush(queue* q, dataType data);
int QueueGet(queue* q, dataType *datain);
int QueuePop(queue* q);
int QueueDistroy(queue *q);

int main()
{
    
    
    // 主函数内容随便改
    queue *Q = QueueInit();
    dataType test;
    int i = 0;
    for (i = 0; i < 5; ++i)
    {
    
    
        test.data = i;
        QueuePush(Q, test);
    }
    while(Q->head != Q->tail)
    {
    
    
        QueueGet(Q, &test);
        printf("%d\n", test.data);
        QueuePop(Q);
    }
    for (i = 5; i >= 0; --i)
    {
    
    
        test.data = i;
        QueuePush(Q, test);
    }
    while(Q->head != Q->tail)
    {
    
    
        QueueGet(Q, &test);
        printf("%d\n", test.data);
        QueuePop(Q);
    }
    return 0;
}

queue* QueueInit(void)
{
    
    
    queue* q = (queue*)malloc(sizeof(queue));
    nodeType* qhead = (nodeType*)malloc(sizeof(nodeType));
    // 动态生成一个队列和它里面的一个节点

    if (q != NULL && qhead != NULL)
    {
    
    
        qhead->next = NULL;
        qhead->data.data = -1;   //信息初始化
        q->head = qhead;    // 头尾都指向同一个节点
        q->tail = qhead;
        return q;
    }
    exit(0);
}

int QueuePush(queue* q, dataType datain)
{
    
    
    nodeType *node = (nodeType*)malloc(sizeof(nodeType));

    if (node != NULL)
    {
    
    
        node->data = datain;    // 更新节点数据
        node->next = NULL;
        q->tail->next = node;   // 更新队列
        q->tail = node;         // 跟新队尾指针
        return OK;
    }
    return ERROR;
}

int QueueGet(queue* q, dataType *datain)
{
    
    
    if (q->head != q->tail && q->head->next != NULL)
    {
    
    
        *datain = q->head->next->data;  // 获得头指针指向的节点
        return OK;
    }
    return ERROR;
}

int QueuePop(queue *q)
{
    
    
    nodeType *dnode = NULL;

    if(q->head != q->tail && q->head->next != NULL)
    {
    
    
        dnode = q->head;
        q->head = q->head->next;    // 将头指针指向现在头指针的下一个节点
        free(dnode);    // 释放空间
        return OK;
    }
    return ERROR;
}

int QueueDistroy(queue* q)
{
    
    
    nodeType *dnode = NULL;
    while(q->head != q->tail)   // 只要头不等于尾,队列就还有成员
    {
    
    
        dnode = q->head;
        q->head = q->head->next;
        free(dnode);    // 同理参考Pop
        return OK;
    }
    return ERROR;
}

Guess you like

Origin blog.csdn.net/u011017694/article/details/109446565