patterncppMinor
Multiply linked list - implementation
Viewed 0 times
listimplementationlinkedmultiply
Problem
I don't know what is the common name for this data structure I named it like this due to a definition in wikipedia that states the following:
In a 'multiply linked list', each node contains two or more link
fields, each field being used to connect the same set of data records
in a different order (e.g., by name, by department, by date of birth,
etc.). While doubly linked lists can be seen as special cases of
multiply linked list, the fact that the two orders are opposite to
each other leads to simpler and more efficient algorithms, so they are
usually treated as a separate case.
I would like to know if this is a proper implementation of this data structure. Also any kind of improvements are welcome.
Implementation:
```
#pragma once
#include
#include
namespace dts
{
template
class MultiplyList
{
public:
using Comparator = std::function ;
private:
class MultiNode
{
public:
T data;
MultiNode** nexts;
MultiNode** prevs;
unsigned size;
MultiNode(unsigned psize, const T& pdata)
: data(pdata), nexts(new MultiNode*[psize]),
prevs(new MultiNode*[psize]), size(psize)
{ }
MultiNode(unsigned psize, T&& pdata)
:data(std::move(pdata)), nexts(new MultiNode*[psize]),
prevs(new MultiNode*[psize]), size(psize)
{ }
MultiNode(unsigned psize)
:MultiNode(psize, T())
{ }
~MultiNode()
{
delete[] nexts;
delete[] prevs;
}
MultiNode* getNext(unsigned index) const
{
check(index);
return nexts[index];
}
void setNext(unsigned index, MultiNode* n
In a 'multiply linked list', each node contains two or more link
fields, each field being used to connect the same set of data records
in a different order (e.g., by name, by department, by date of birth,
etc.). While doubly linked lists can be seen as special cases of
multiply linked list, the fact that the two orders are opposite to
each other leads to simpler and more efficient algorithms, so they are
usually treated as a separate case.
I would like to know if this is a proper implementation of this data structure. Also any kind of improvements are welcome.
Implementation:
```
#pragma once
#include
#include
namespace dts
{
template
class MultiplyList
{
public:
using Comparator = std::function ;
private:
class MultiNode
{
public:
T data;
MultiNode** nexts;
MultiNode** prevs;
unsigned size;
MultiNode(unsigned psize, const T& pdata)
: data(pdata), nexts(new MultiNode*[psize]),
prevs(new MultiNode*[psize]), size(psize)
{ }
MultiNode(unsigned psize, T&& pdata)
:data(std::move(pdata)), nexts(new MultiNode*[psize]),
prevs(new MultiNode*[psize]), size(psize)
{ }
MultiNode(unsigned psize)
:MultiNode(psize, T())
{ }
~MultiNode()
{
delete[] nexts;
delete[] prevs;
}
MultiNode* getNext(unsigned index) const
{
check(index);
return nexts[index];
}
void setNext(unsigned index, MultiNode* n
Solution
If you have a destructor you should also have a copy constructor and assignment operator (and the move variants).
For a container I expect a set of
The iterator type could be a struct with a pointer to a
I would expect
For a container I expect a set of
begin() and end() functions so the classic STL for loop can be used with it.The iterator type could be a struct with a pointer to a
MultiNode and a int for the index passed to getNext(int) on operator++.I would expect
bool find(Predicate, T&); bool hasKey(); and int paths(); to be const as well.Context
StackExchange Code Review Q#107022, answer score: 2
Revisions (0)
No revisions yet.