zoukankan      html  css  js  c++  java
  • c语言实现基本的数据结构(五) 单链队列

    #include <stdio.h>
    #include <tchar.h>
    #include <stdlib.h>
    
    
    #define MaxQueueSize 100
    // TODO:  在此处引用程序需要的其他头文件
    struct Node{
        int data;
        Node* next;
    };
    struct Queue{
        Node* front;
        Node* rear;
    };
    //初始化队列
    bool Init_Queue(Queue* q){
        q->front = q->rear = (Node*)malloc(MaxQueueSize*sizeof(Node));
        if (!q->front) return false;
        q->front->next = NULL;
        return true;
    }
    //清空队列
    bool Clear_Queue(Queue* q){
        q->front = q->rear;
        return true;
    }
    //销毁队列
    bool Destroy_Queue(Queue* q){
        Node* temp = q->front;
        while (q->front != q->rear){
            temp = q->front->next;
            free(q->front);
            q->front = temp;
        }
        q->front = q->rear = NULL;
        return true;
    }
    //入队
    bool EnQueue(Queue* q, int value){
        Node* n = (Node*)malloc(sizeof(Node));
        n->next = NULL;
        n->data = value;
    
        q->rear->next = n;
        q->rear = n;
        return true;
    }
    //出队,返回队头元素的值
    int DeQueue(Queue* q){
        int temp;
        if (q->front == q->rear) exit(-1);
        Node* p = q->front;
        temp = p->next->data;
        q->front = p->next;
        if (p == q->rear) q->front = q->rear;
        free(p);
        p = NULL;
        return temp;
    }
    //打印队列
    void Print_Queue(Queue q){
        if (q.front == q.rear) printf("空队列
    ");
        while (q.front != q.rear){
            printf("%d
    ", DeQueue(&q));
        }
    }
  • 相关阅读:
    CSU 1333 Funny Car Racing
    FZU 2195 检查站点
    FZU 2193 So Hard
    ZOJ 1655 FZU 1125 Transport Goods
    zoj 2750 Idiomatic Phrases Game
    hdu 1874 畅通工程续
    hdu 2489 Minimal Ratio Tree
    hdu 3398 String
    洛谷 P2158 [SDOI2008]仪仗队 解题报告
    POJ 1958 Strange Towers of Hanoi 解题报告
  • 原文地址:https://www.cnblogs.com/xin1998/p/7745912.html
Copyright © 2011-2022 走看看