[day3 data structure] Basic operations of chain queue

关于链队列有以下常见结论:
 1. 初始化队列:lqu->front=lqu->rear
 2. 入队:
 	p->data=x,p->next=NULL;
 	if(lqu->rear==NULL)
 		lqu->front=lqu.rear=p;
 	else
 		lqu->rear->next=p;
 		lqu->rear=p;
 3. 出队:
    p=lqu->front;
    if(lqu->front==lqu->rear)
    	lqu->front=lqu->rear=NULL;
    else
    	lqu->front=lqu->front->next;
    free(p);
 4. 判断条件:
    分为三种情况考虑:栈为空,栈内有一个节点,栈有多个节点。

Define nodes:

typedef struct QNode{
    
    
    int data;
    struct QNode *next;
}QNode;

Define chain queue:

typedef struct {
    
    
    QNode *front;
    QNode *rear;
}LiQueue;

Queue initialization:

void initQuene(LiQueue *&lqu){
    
    
    lqu=(LiQueue*)malloc(sizeof(LiQueue));
    lqu->front=lqu->rear=NULL;
}

The queue is empty:

int isEmpty(LiQueue *lqu){
    
    
    if(lqu->rear==NULL||lqu->front==NULL)
        return 1;
    else
        return 0;
}

Joining the team:

void enQueue(LiQueue *&lqu,int x){
    
    
    QNode *p;
    p=(QNode*) malloc(sizeof (QNode));
    p->data=x;
    p->next=NULL;
    if(lqu->rear==NULL){
    
    
        lqu->front=lqu->rear=p;
    }
    else {
    
    
        lqu->rear->next = p;
        lqu->rear = p;
    }
}

Dequeue operation:

int deQueue(LiQueue *lqu){
    
    
    QNode *p;
    if(lqu->rear==NULL)
        return 0;
    else
        p=lqu->front;
    if(lqu->rear==lqu->front)
        lqu->rear==lqu->front==NULL;
    else
        lqu->front=lqu->front->next;
    free(p);
    return 1;
}

Print chain queue:

void print(LiQueue *lqu){
    
    
    while(lqu->front!=NULL)
    {
    
    printf("%d",lqu->front->data);
        lqu->front=lqu->front->next;}
}

Guess you like

Origin blog.csdn.net/qq_51763547/article/details/132128899