FIFO implementation

When implementing FIFO, I used the following structure:

struct Node
{
    T info_;
    Node* link_;
    Node(T info, Node* link=0): info_(info), link_(link)
    {}
};

I think this is a well-known trick for many STL containers (e.g. List). Is this a good practice? What does this mean for the compiler when you say that Node has a member with its pointer type? Is this some kind of endless loop?

And finally, if this is bad practice, how can I implement the best FIFO.

EDITOR: People, it's all about implementation. I am fairly familiar with the STL library and I know many containers from several libraries. I just want to discuss with people who can give a good implementation or good advice.

+3
source share
5 answers

, , C, ++. , (, 32- 32- ), , .

. :

class A; // forward declared type

struct B
{
    A* pa; //< pointer to A - perfectly legal
};

, , :

#include <A.hpp> // bring in full declaration of class A
...
B b;
b.pa = &a; // address of some instance of A
...
b.pa->func(); // invoke A member function - this needs full declaration

FIFO std::queue. std::list, std::deque, std::vector, .

+1

?

.

, , Node ?

, .

, , , FIFO.

std::queue;)

+2

, . .

FYI, , , std:: queue std:: deque . std:: deque - , , . , , :

  • , . . , .
  • , , .
+2
+1

node. node node . , . node , .

, FIFO, node . link_ Node.

T , Node

struct Node
{
    T * info_;
    Node* link_;
    Node(T * info, Node* link=0): info_(info), link_(link)
    {}
};

, info_ T. , , .

+1

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


All Articles