链队列的基本操作

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

typedef struct node{
    char data;
    struct node * next;
}Qnode;

typedef struct{
    Qnode * front;
    Qnode * rear;
}LinkQueue;

LinkQueue * init(){
    LinkQueue * lq;
    Qnode * qn;
    qn = (Qnode *)malloc(sizeof(Qnode));
    lq = (LinkQueue *)malloc(sizeof(LinkQueue));

    qn->next = NULL;
    lq->front = qn;
    lq->rear = qn;
    return lq;
}

void inLinkQueue(LinkQueue * lq,char x){
    Qnode * p = (Qnode *)malloc(sizeof(Qnode));
    p->data = x;
    p->next = NULL;
    lq->rear->next = p;
    lq->rear = p;
}

void outLinkQueue(LinkQueue * lq){
    Qnode * p;
    char x;
    if(lq->front == lq->rear){
        printf("the queue is empty!\n");
    }else{
        p = lq->front->next;
        x = p->data;
        lq->front->next = p->next;
        free(p);
        if(lq->front->next == NULL){
            lq->rear = lq->front;
        }
    }
}

void print(LinkQueue * lq){
    Qnode * p;
    p = lq->front->next;
    while(p!=NULL){
        printf("%4c",p->data);
        p = p->next;
    }
    printf("\n");
}

int main(){
    LinkQueue * lq;
    char x;
    lq = init();
    printf("please input the values of the queue!\n");
    scanf("%c",&x);
    while(x != '\n'){
        inLinkQueue(lq,x);
        scanf("%c",&x);
    }
    print(lq);
    printf("after output (once)\n");
    outLinkQueue(lq);
    print(lq);
    printf("after output (twice)\n");
    outLinkQueue(lq);
    print(lq);

    return 0;
}

你可能感兴趣的:(链队列的基本操作)