3

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. Also in other place of code after -> this sign code does not give me possibility to choose item or next. Please help.

5
  • 1
    Can you post the exact problem you're having, including as appropriate wrong output, error messages, and desired output. Also please use sentences, your question is difficult to read. Commented Aug 30, 2010 at 12:26
  • Considering it should show me option to choose: Do you want your IDE (your editor) to show up a list of heads members? Or something else? Commented Aug 30, 2010 at 12:26
  • yes members but it does not show me Commented Aug 30, 2010 at 12:36
  • Ok, but then it is not a problem of FIFO Queue linked list implementation but a problem with your editor. What editor are you using? Commented Aug 30, 2010 at 12:37
  • I haven't been using VS for quite a time, but I know that its IntelliSense doesnt work sometimes. So try googling "visual studio 2010 intellisense not working" or something similar. Commented Aug 30, 2010 at 12:50

2 Answers 2

4

ON: The -> operator can be overloaded so the development environment cannot be sure what to do with it. 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

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
  • Renamed node to Node and link to Link because Item is Item, not item. Just to make it somewhat standardized
  • Initializing tail at the constructor of Queue.
  • Using initializer list instead of code where possible.
  • Fixing Queue::get(), setting tail to zero if the queue become empty.
  • Using constant reference in parameter lists of Queue::put() and Queue::Node::Node()
  • Node, Link, head and tail is private from now.
  • Queue::empty() returns bool instead of int from now.
Sign up to request clarification or add additional context in comments.

Comments

3

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).

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.

If you need polymorphic behavior, then use a std::queue< std::unique_ptr<Item> >.

3 Comments

"Don't reinvent the wheel, unless you plan to learn more about wheels."
I think you're over-engineering the answer. He just has problems with his editor's automatic completion feature.
@cevalek: I guess so too, it wasn't clear before the comments though. What's amusing is that I still get upvotes for this, so I guess I wasn't the only one who didn't pick up the real issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.