当前位置:网站首页>Chained storage of queues

Chained storage of queues

2022-07-01 12:22:00 Between the steps

Chain storage of queues

1. initialization

#include<stdio.h>
typedef struct LinkNode{
    
    ElemType data;
    struct LinkNode *next;
}LinkNode;

typedef struct{
    
    LinkNode *front,*rear;
}LinkQueue;

// Initialize queue , Leading node 
void InitQueue(LinkQueue &Q){
    
    // On initialization ,front  and rear  All point to the head node 
    Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));
    Q.front->next=NULL;

}

void testLinkQueue(){
    
    LinkQueue Q; // Declare a queue 
    InitQueue(Q); // Initialize queue 
    
    // Follow up operation 
}

 Insert picture description here

2. Is it empty

bool isEmpty(LinkQueue Q){
    
    if(Q.rear==Q.front)
        return true;
    else
        return false;

}

3. Do not lead the node to initialize the queue


// Initialize queue , No leading node 
void InitQueue(LinkQueue &Q){
    
    // On initialization ,front  and rear  All point to NULL
    Q.front=NULL;
    Q.rear=NULL;

}

4. Don't take the lead in judging empty nodes

bool isEmpty(LinkQueue Q){
    
    if(Q.front==NULL)
        return true;
    else
        return false;

}

5. Joining operation ( Leading node ) Similar to tail interpolation

void EnQueue(LinkQueue &Q,ElemType x){
    
    LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode));
    s.data=x;
    s->next=NULL;
    Q.rear->next=s;  // New node insertion rear after 
    Q.rear=s;        // Modify the tail pointer 
    

}

 Insert picture description here

6. Join the team without taking the lead ( Maybe there are elements in it Maybe an empty element is inserted )

// New elements in the team   No leading node 
void EnQueue(LinkQueue &Q,ElemType x){
    
    LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode));  // Open up space to store new elements 
    s->data=x;                                         // The inserted value is put into the newly opened space 
    s->next=NULL;                                      // The newly opened space points to NULL
    if(Q.front==NULL){
     // There is no element   There is no head node   Only Q.front  and Q.rear  The pointer 
        Q.front=s;
        Q.rear=s;
    }
    else{
    
        Q.rear->next=s;
        Q.rear=s;
    }
        
}

 Insert picture description here

7. Out of line operation ( Leading node )

bool DeQueue(LinkQueue &Q,ElemType &x){
    
    if(Q.front==Q.rear)   // Description is empty stack 
        return false;
    LinkNode *p=Q.front->next; //p Point to the first node 
    x=p->data;            // With variable x Return to team leader element 
    Q.front->next=p->next; // Modify the header node next The pointer 
    
    if(Q.rear==p){
       // The last node comes out of the stack 
        Q.rear=Q.front;    // modify rear The pointer 
    }
    free(p);
    
    return true;
}

 Insert picture description here
 Insert picture description here

8. Do not take the lead in the out of line operation of the node

bool DeQueue(LinkQueue &Q,ElemType &x){
    
    if(Q.front==NULL)   // Description is empty stack 
        return false;
    LinkNode *p=Q.front;  //p Point to the node out of the queue 
    x=p->data;            // Variable x Returns the header element 
    Q.front=p->next;      // modify front The pointer 
    
    if(Q.rear==p){
       // The last node comes out of the stack 
        Q.front=NULL;
        Q.rear=NULL;
    }
    free(p);
    
    return true;
}

 Insert picture description here

Chain storage does not consider the situation of full queues

原网站

版权声明
本文为[Between the steps]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011204421684.html