MPNation
MPNation

Reputation: 179

From Vector to Linked List C++

I am pretty new with C++ and I wanted to make sure that I have set up my linked list currently. The information was stored in a vector before, but I was assigned to change it to a linked list. The information stored is CandidateInfo, here is the original code. Have I set my list up correctly?

struct CandidateInfo {
std::string name;
int votesInThisState;
int delegatesWonInThisState;  };
std::vector<CandidateInfo> stateCandidates;

And here is my attempet. Suggestions welcomed.

template <typename StatePrimary>
struct CandidateInfo
{
    std::string name;
    int votesInThisState;
    int delegatesWonInThisState;

    StatePrimary state;
    CandidateInfo<StatePrimary>* next;
    CandidateInfo()
    {
        next = 0;
    }
    CandidateInfo
    (const StatePrimary& c,
     CandidateInfo<StatePrimary>* nxt =0)
        : state(c), next(nxt)
    {}
};
template <typename StatePrimary>
struct CandidateInfoHeader
{

    CandidateInfo<StatePrimary>* first;
    CandidateInfoHeader();

};

Upvotes: 0

Views: 996

Answers (1)

Tim B
Tim B

Reputation: 3133

I would have put this as a comment, but I don't have enough reputation yet. Is there a reason you're not using a std::list? That way you could use the code in the first snippet you posted, i.e.:

struct CandidateInfo {
std::string name;
int votesInThisState;
int delegatesWonInThisState;  };
std::list<CandidateInfo> stateCandidates;

(Note the change from vector to list in the last line)

std::list is an STL implementation of a doubly linked list.

Upvotes: 2

Related Questions