Does the chain team need a head node?

Queue is a special linear table. It only allows deletion at the head of the table and insert at the end of the table. It is a first-in first-out data structure.

The queue can be stored in an array or in a chain.

There are two common types of chain storage:

Leading nodes and non-leading nodes. We recommend the implementation of the lead node, because this can greatly simplify the processing of the queue.

The following takes the team entry operation as an example to further elaborate the viewpoints of this article. Suppose the basic structure is defined as:

Public account Zhonglu Xiaoma for more information.

typedef int datatype;
        typedef struct node
        {
                datatype data;
                struct node* next;
        }listnode, *linknode;
        typedef struct
        {
                linknode front;
                linknode rear;
        }linkqueue;

The chain team with the lead node joins the team to achieve:

 void enqueue(linkqueue* q, datatype x){
                linknode p = (linknode)malloc(sizeof(listnode));
                p->data = x;
                p->next = NULL;
                q->rear->next = p;
                q->rear = p;
        }

The chain team enlisting without the lead node is realized:

void enqueue(linkqueue* q, datatype x){
                linknode p = (linknode)malloc(sizeof(listnode));
                p->data = x;
                p->next = NULL;
                if(q->front == NULL){
                        q->front = p;
                        q->rear = p;
                        return;
                }
                q->rear->next = p;
                q->rear = p;
        }

Comparing the above two programs, the enqueue operation of the chain team with the lead node only needs to add the newly generated node to the end node. For operations that do not take the lead, you must also pay attention to boundary operations. If it is the first time to join the team, you need to modify the head pointer. In the same way, for dequeue operations, if the last node dequeues, you need to pay attention to modifying the tail pointer. Therefore, we suggest that the chain queue is best implemented with the lead node.

Guess you like

Origin blog.csdn.net/qq_30787727/article/details/112203926