Implement queue

I have the following queue class (taken from wordpress):

#include<iostream.h>

class Queue
    {
    private:
     int data;
     Queue*next;
    public:
     void Enque(int);
     int Deque();
    }*head,*tail;    

    void Queue::enque(int data)
    {
     Queue *temp;
    temp=new Queue;
    temp->data=data;
    temp->next=NULL;
    if(heads==NULL)
     heads=temp;
    else
    tail->next=temp;
    tail=temp;
    }

    int Queue::deque()
    {
    Queue* temp;//
    temp=heads;
    heads=heads->next;
    return temp->data;
    }

I am trying to understand why the compiler tells me that I have a multiple definition of "head" and "tail" - without success.

edit: When the compiler issues an error message, it opens the locale_facets.tcc file from I-don-t-know-where and says that the error is on line 2497 in the following function:

bool
 __verify_grouping(const char* __grouping, size_t __grouping_size,
        const string& __grouping_tmp)

Does anyone have any ideas?

+3
source share
6 answers

Since this is homework, here is some information about the queues and how you could implement it.

A queue is a standard abstract data type. It has several properties associated with it:

  • - .
  • / - .
  • , , , .

.
, , , , . ( ). ( ), , . , .

Linked-List, , . / .

, , . :

  • enqueue - ()
  • dequeue - () .
  • empty -
  • size -

, , , ( ++ / ), , , , .

, , , . / . .

+9

, std::queue ++:

#include <queue>

void test() {
    std::queue<int> myQueue;
    myQueue.push(10);
    if (myQueue.size())
        myQueue.pop(); 
}
+5

++?

#include <queue>

using namespace std;

int main() {
    queue<int> Q;

    Q.push(1);
    Q.push(2);
    Q.push(3);

    Q.top(); // 1
    Q.top(); // 1 again, we need to pop
    Q.pop(); // void

    Q.top(); // 2
    Q.pop();

    Q.top(); // 3
    Q.pop();

    Q.empty(); // true
    return 0;
}
+4

:

  • Enqueue Dequeue, enqueue dequeue: ++ .
  • "", , , , ""?
+2

BFS... deque.

#include <deque>

using namespace std;

void BFS() {
    deque<GraphNode*> to_visit;
    to_visit.push_back(start_node);
    while (!to_visit.empty()) {
        GraphNode* current = to_visit.front();
        current->visit(&to_visit);  // enqueues more nodes to visit with push_back
        to_visit.pop_front();
    }
}

GraphNode::visit "" . push_back(), front() pop_front()

. , .

+1

, - , :

class Queue {
  // blah
} *head, * tail;

Queue head tail Queue*. , .

+1

Source: https://habr.com/ru/post/1708498/


All Articles