简体   繁体   English

FIFO队列链表实现

[英]FIFO Queue linked list implementation

Here is code in which I am trying to implement a queue using linked list: 这是我尝试使用链表实现队列的代码:

#include <iostream>
#include <cstdlib>
using namespace std;
template <class Item>
class Queue{

public:
    struct  node{
      Item item;node *next;
      node (Item x){
        item=x; next=0;
      }
    };
    typedef node* link;
    link  head, tail;

public:
    Queue(int){  head=0;}
    int empty() const {   return head==0; }
    void put(Item x){

        node* t=tail;
        tail=new node(x);
        if (head==0) head=tail;
        else   t->next=tail;
    }
    Item get(){  

        Item v=head->item;link t=head->next;
        delete head; head=tail return v;
    }

    };

int main(){
    return 0;
}

but I have problems with pointers. 但我的指针有问题。 For example, when I write Item v = head-> it should show me option to choose item but it does not show. 例如,当我写Item v = head->它应该显示我选择项目的选项,但它没有显示。 Also in other place of code after -> this sign code does not give me possibility to choose item or next. 在其他代码之后 - >这个符号代码不允许我选择项目或下一个。 Please help. 请帮忙。

You would probably be better off reusing an existing container. 重用现有容器可能会更好。

The STL explicitly contains, for example, a queue Container Adapter (based on deque by default, which is the most efficient choice). 例如,STL显式包含一个queue容器适配器(默认情况下基于deque ,这是最有效的选择)。

If you don't need polymorphic behavior, a std::queue<Item> is what you're looking for, it's both extremely efficient (more than your custom list-based queue) and you will avoid memory management issues. 如果您不需要多态行为,那么您正在寻找std::queue<Item> ,它既非常高效(超过基于自定义列表的队列),您将避免内存管理问题。

If you need polymorphic behavior, then use a std::queue< std::unique_ptr<Item> > . 如果需要多态行为,请使用std::queue< std::unique_ptr<Item> >

ON: The -> operator can be overloaded so the development environment cannot be sure what to do with it. ON: - >运算符可能会重载,因此开发环境无法确定如何处理它。 You can do the following (temporarily or permanently) if you really want to have auto-completion. 如果您真的想要自动完成,可以执行以下操作(暂时或永久)。

// IMPORTANT. Make sure "head" is not null before you do it!
Node &headNode(*head); // Create a reference 
headNode.next = tail; // Use TAB or CTRL+SPACE or whatever here after dot

OFF: I reviewed your code and made some corrections OFF:我查看了您的代码并进行了一些更正

template <class Item>
class Queue {
public:
        Queue()
        : head(0)
        , tail(0)
        { }
        bool empty() const { return head==0; }
        void put(const Item& x)
        {
                Node* t = tail;
                tail = new Node(x);
                if (head==0)
                        head = tail;
                else
                        t->next = tail;
        }
        Item get()
        {
                Item v = head->item;
                Link t = head->next;
                delete head;
                head = t;
                if(head==0)
                        tail = 0;
                return v;
        }
private:
        struct Node {
                Item item;
                Node *next;
                Node(const Item& x)
                : item(x)
                , next(0)
                {}
        };
        typedef Node* Link;
        Link head,tail;
};
  • Removed int typed nameless parameter from Queue constructor Queue构造函数中删除了int typed无名参数
  • Renamed node to Node and link to Link because Item is Item , not item . 更名nodeNodelinkLink ,因为ItemItem ,没有item Just to make it somewhat standardized 只是为了使它有点标准化
  • Initializing tail at the constructor of Queue . Queue的构造函数中初始化tail
  • Using initializer list instead of code where possible. 尽可能使用初始化列表而不是代码。
  • Fixing Queue::get() , setting tail to zero if the queue become empty. 修复Queue::get() ,如果队列变空则将tail设置为零。
  • Using constant reference in parameter lists of Queue::put() and Queue::Node::Node() Queue::put()Queue::Node::Node()参数列表中使用常量引用
  • Node , Link , head and tail is private from now. NodeLinkheadtail从现在开始是私有的。
  • Queue::empty() returns bool instead of int from now. Queue::empty()从现在开始返回bool而不是int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM