How to store nodes in a vector?

A node is a data type right? So I should be able to create a vector of that dataType? Or am I missing something or doing it wrong? Can I create a vector<node> nodeVector?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template <class Type> 
struct node
{
    Type info;
    node<Type> *nextNode; //pointer to next node.  
};

template <class Type>
class circularLinkedList
{
private:
    Type * head; //head points to the first node.  
    Type * tail; //tail points to the last node, 
    int count;
    circularLinkedList()
    {
        count = 0;
        head = nullptr;
        tail = nullptr;
    }
    circularLinkedList(int listSize)
    {
        count = listSize;
        vector<node> nodes(count);
        head = nodes.front();
        tail = nodes.back();
    }
    void getValues(Type values[]);
}
Last edited on
On line 24 you never specify the template parameter for node. node<int>, node<std::string>, or what?
The node is supposed to store any type of data, so would I do vector<node<type>> nodes(count);?
Make that an uppercase T and it should work, but what's the point? nodes is an object local to the constructor function, so it will go out of scope and stop existing when that function returns. You probably want it as a private data member.

I can't figure out what you're up to with those pointers.

Now that I think about it, what's the vector for?
Last edited on
I have the vector to make future functions easier.
eg a function that adds a node, I can just reference nodes[i] in a loop for example.

Is there a better alternative I'm missing? I self teach myself so I am bound to have some misconceptions or bad practices, which I do want to fix.
Last edited on
I have the vector to make future functions easier.
eg a function that adds a node, I can just reference nodes[i] in a loop for example.
You're not really making a linked list, though. The point of a list is that inserting elements in the middle is extremely fast. If you maintain a vector with the nodes, you lose that property. You may as well just use a vector.
Topic archived. No new replies allowed.