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);
        }
    
    }
    
  • 相关阅读:
    Structured streaming
    streaming窗口操作
    scala伴生对象,apply()及单例
    storm集成kafka
    solr简易安装配置
    拦路雨偏似雪花,饮泣的你冻吗?--稍瑞,我是关键字过滤器
    我存在,你深深的循环里--从反射看JSON死循环
    ueditor:原谅我这一生不羁放纵爱独特
    或许你不知道(2):LinkedList
    自定义负载均衡
  • 原文地址:https://www.cnblogs.com/pengwill/p/7367193.html
Copyright © 2011-2022 走看看