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);
        }
    
    }
    
  • 相关阅读:
    SuperMap房产测绘成果管理平台
    SuperMap产权登记管理平台
    Android adb shell am 的用法(1)
    由浅入深谈Perl中的排序
    Android 内存监测和分析工具
    Android 网络通信
    adb server is out of date. killing...
    引导页使用ViewPager遇到OutofMemoryError的解决方案
    adb logcat 详解
    How to send mail by java mail in Android uiautomator testing?
  • 原文地址:https://www.cnblogs.com/pengwill/p/7367192.html
Copyright © 2011-2022 走看看