zoukankan      html  css  js  c++  java
  • 数据结构<三> 队列

    #include<iostream>
    using namespace std;
    struct Node{
        int data;
        Node *next;
        Node(const int& Newdata, Node* nextnode=NULL):data(Newdata), next(nextnode){
        }
    };
    class Queue{
        private:
            Node *front;
            Node *rear;
            int count;
        public:
            Queue();
            ~Queue();
            bool empty();
            void push(int x);
            void pop();
            void clear();
            int Length();
            int Front();
    };
    Queue::Queue():front (NULL),  rear (NULL), count(0){    
    }
    Queue::~Queue() {
        clear();
    }
    void Queue::push(int x) {
        if (front == NULL)
          front = rear = new Node(x);
        else {
            Node *q = new Node(x);
            rear->next = q;
            rear = q;
        }
        count++;
    }
    int Queue::Length() {
        return count;
    }
    bool Queue::empty(){
        return front == NULL;
    }
    void Queue::pop() {
        if (empty()) {
            cout<<"错误"<<endl; 
        }
        Node *q = front;
        front = front->next;
        delete q;
        count--;
    }
    void Queue::clear() {
        while (front) {
            Node *q = front;
            front= front->next;
            delete q;
        }
        count=0;
    }
    int Queue::Front() {
        if (empty()) {
            cout<<"错误"<<endl; 
        }
        return front->data;
    }
    int main() {
        Queue s;
        s.push(1);
        s.push(11);
        s.push(12);
        s.push(13);
        s.push(14);
        s.push(15);
        s.push(16);
        s.push(17);
        cout<<s.Front()<<endl;
        cout<<s.Length()<<endl;
        s.pop();
        cout<<s.Length()<<endl;
        return 0;
    }
  • 相关阅读:
    java中排序算法
    maven常用命令
    Team_GJX模板整理
    BZOJ 4128
    BZOJ 1169: [Baltic2008]Grid
    Codeforces Round #448 (Div. 2)
    HDU 5942
    2016 ICPC 沈阳
    2016 ICPC 北京
    2016 CCPC 杭州
  • 原文地址:https://www.cnblogs.com/a863886199/p/7595795.html
Copyright © 2011-2022 走看看