zoukankan      html  css  js  c++  java
  • 队列的实现

    #include<stdio.h>
    #include<malloc.h>
    #include<stdlib.h>
    typedef struct node{
        int value;
        struct node * pNext;
    }NODE,*PNODE;
    
    typedef struct queue{
        PNODE front;
        PNODE rear;
    }QUEUE,*PQUEUE;
    
    void init_queue(PQUEUE pQueue);//1
    void destory_queue(PQUEUE pQueue);//1
    void clean_queue(PQUEUE pQueue);//1
    bool is_empty(PQUEUE pQueue);//1
    void get_head(PQUEUE pQueue,int *e);//1
    void add_elem(PQUEUE pQueue);//1
    void del_tail(PQUEUE pQueue);//1
    void get_length(PQUEUE pQueue);//1
    void trea_queue(PQUEUE pQueue);//1
    
    int main()
    {
        int e = 0;
        QUEUE queue;
        init_queue(&queue);
        add_elem(&queue);
        trea_queue(&queue);
        destory_queue(&queue);
        trea_queue(&queue);
        return 0;
    }
    
    
    
    void init_queue(PQUEUE pQueue)
    {
        pQueue->front = pQueue->rear = NULL;
    }
    
    
    void add_elem(PQUEUE pQueue)
    {
        int val;
        while(scanf("%d",&val) && val != -1){
            PNODE p = (PNODE)malloc(sizeof(NODE));
            if(pQueue->front ==NULL && pQueue->rear == NULL){
                p->value = val;
                p->pNext = NULL;
                pQueue->front = pQueue->rear = p;
            }else{
                p->value = val;
                p->pNext = NULL;
                pQueue->rear->pNext = p;
                pQueue->rear = p; 
            }
        }
    }
    
    void trea_queue(PQUEUE pQueue)
    {
        PNODE p = pQueue->front;
        for(;p;p = p->pNext){
            printf("%d	",p->value);
        }
        printf("
    ");
    }
    
    void del_tail(PQUEUE pQueue)
    {
        PNODE p = pQueue->front;
        if(pQueue->front == pQueue->rear){
            pQueue->front = pQueue->front->pNext;
            free(p);
            pQueue->front = pQueue->rear = NULL;
        }else{
            pQueue->front = pQueue->front->pNext;
            free(p);
        }
    
    }
    
    void get_length(PQUEUE pQueue)
    {
        if(pQueue->front == NULL && pQueue->rear == NULL){
            printf("ERRORs
    ");
        }else{
            int cnt = 0;
            PNODE p = pQueue->front;
            while(p){
                cnt++;
                p = p->pNext;
            }
            printf("The length of the queue is %d
    ",cnt);
        }
    } 
    
    bool is_empty(PQUEUE pQueue)
    {
        if(pQueue->front == NULL && pQueue->rear == NULL){
            printf("The queue is empty
    ");
            return true;
        }else{
            printf("The queue is not empty
    ");
            return false;
        }
    }
    
    void get_head(PQUEUE pQueue,int *e)
    {
        if(is_empty(pQueue)){
            printf("SORRY IS EMPTY
    ");
        }else{
            *e = pQueue->front->value;
        }
    }
    
    void clean_queue(PQUEUE pQueue)
    {
        PNODE p = pQueue->front;
        while(p){
            p->value = 0;
            p = p->pNext;
        }
    }
    
    void destory_queue(PQUEUE pQueue)
    {
        PNODE p = pQueue->front;
        while(pQueue->front != NULL && pQueue->rear != NULL){
            del_tail(pQueue);
        }
    
    }
    
  • 相关阅读:
    BZOJ4383 : [POI2015]Pustynia
    BZOJ4382 : [POI2015]Podział naszyjnika
    BZOJ4381 : [POI2015]Odwiedziny
    BZOJ4380 : [POI2015]Myjnie
    BZOJ4378 : [POI2015]Logistyka
    BZOJ3424 : Poi2013 Multidrink
    BZOJ4367 : [IOI2014]holiday假期
    BZOJ4369 : [IOI2015]teams分组
    BZOJ4421 : [Cerc2015] Digit Division
    BZOJ1315 : Ural1557Network Attack
  • 原文地址:https://www.cnblogs.com/pengwill/p/7367193.html
Copyright © 2011-2022 走看看