簡體   English   中英

自定義隊列類C ++

[英]Custom Queue Class C++

所以我正在嘗試創建一個單鏈列表隊列。 我正在嘗試編寫一個添加元素的函數,並且一切都很好,但是問題是它的FILO而不是FIFO。 我不確定如何處理前后指針。

#include <iostream>
#include <string>
using namespace std;

class Queue{
    public:
        Queue();
       //~Queue();
       void add(const string & item);
       //string remove();
      // unsigned items() const;
       void show() const;
    private:
        struct Node{
            string data;
            Node *next;
        };
        Node *rear;
        Node *front;
        unsigned elements;
};

Queue::Queue():elements(0),rear(NULL),front(NULL){}

//Queue::~Queue(){

//}

void Queue::add(const string & item){
    Node *t=new Node;
    t->data=item;
    t->next=rear;
    if(front==NULL)
        front=t;
    rear=t;
    elements++;

}

void  Queue::show() const{

    Node *p=rear;
    for(; p->next!=rear; p=p->next){
        cout<<" "<<p->data;
    }
    cout<<"\n";
}
int main(){
    Queue obj;
    obj.add("I");
    obj.add("Am");
    obj.add("Super");
    obj.add("Cool");
    obj.show();
}

目前,它既不是FIFO也不是FILO bu JINO(正好是,永不淘汰)。

您要做的是在后端插入。 並且您的節目確實從后到前進行迭代,因為那是唯一的鏈接方向。

為了獲得有效的FIFO,您需要從隊列的前端刪除。 您會注意到,您可以找到front元素,但是沒有簡單的方法來找到設置front指針所需的第二個元素。 這是單鏈接設計的缺點,您必須從后到前進行迭代以找到指向前面的元素。

  • 使用單個鏈接列表,您可以執行FILO(實際上更可能命名為LIFO或堆棧)
  • 對於FIFO,雙鏈表是更好的設計。

如果您想堅持一個鏈接列表,則可以進行一些遞歸。 您消除了前指針,因為它沒有用。

void  Queue::show_one(Node *p) const{
    if (p->next!=rear) {    // i kept the test for p->next!=rear
                            // either fix add or test for p->next!=NULL
        show_one(p->next);
    }
    cout<<" "<<p->data;
}

void  Queue::show() const{
    show_one(rear);
    cout<<"\n";
}

同樣,您可以編寫remove()

若要實現FILO(如STACK?),則在push(添加)時,在末尾追加新元素(您將處理后方指針)。彈出時,請擺脫后方指針指向的元素。

在代碼中,后指針指向結束后的一個元素,該元素為null。 因此,推送需要O(n),而流行花費O(n)。 它沒有效率。 因此,考慮到雙鏈表可能是易於實現的更好選擇。

我想出了如何扭轉整個局面,以便現在可以正常工作。 有效率嗎? 運行main花了1.06ms。

    #include <iostream>
    #include <string>
    using namespace std;
    bool die(const string &msg);

    class Queue{
        public:
            Queue();
           ~Queue();
           void add(const string & item);
           string remove();
           unsigned items() const;
           void show() const;
        private:
            struct Node{
                string data;
                Node *next;
            };
            Node *rear;
            Node *front;
            unsigned elements;
    };

    Queue::Queue():elements(0),rear(NULL),front(NULL){}

    Queue::~Queue(){
     unsigned el=items();
     for(unsigned i=0; i<el; i++)
      remove();
    }
    unsigned Queue::items()const{
        return elements;
    }

    string Queue::remove(){
        if(front==NULL) die("underflow");
        Node *t=front;
        string data=t->data;
        front=t->next;
        delete t;
        elements--;
        return data;
    }
    void Queue::add(const string &item){
     Node *t=new Node;
     t->data=item;
     t->next=NULL;
     if(front==NULL)
        front=t;
     else{
        Node *t2=rear;
        t2->next=t;
     }
     rear=t;
     elements++;
    }

    void  Queue::show() const{
        Node *t=front;
        for(unsigned i=0; i<items(); i++, t=t->next)
            cout<<t->data<<'\n';
    }

    bool die(const string &msg){
        cout<<"Error: "<<msg;
        exit(EXIT_FAILURE);
    }

    int main(){
        Queue obj;
        obj.show();
        obj.add("poo");
        obj.add("cra");
        obj.add("bil");
        obj.add("shi");
        obj.show();
        cout<<obj.remove()<<"\n";
        cout<<obj.remove()<<"\n";
        cout<<obj.remove()<<"\n";
        cout<<obj.remove()<<"\n";
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM