您好我已经制作了一个双向链表容器。对于一个项目,我现在必须将双向链表转换为方形列表。我的意思是方形列表是数据将存储在一个正方形中。因此,例如,4个元素将被存储如下:
10 30
20 40
将存储5个元素:
10 30 50
20 40
将存储10个元素:
10 40 70 100
20 50 80
30 60 90
基本上,行数或列数不能超过列表大小的上限的平方根。它的意思是,如果有大量数据,它可以通过遍历列来快速插入或擦除该数据,直到找到该值将插入或删除的列,然后从该点遍历列表,直到它为止。找到合适的位置进行插入或删除。
我正在考虑的是将数据存储在双向链表中,然后使用表示列的双向链表。因此,列列表中的每个节点都将指向行列表中应该表示列标题的节点。我将如何实现这一目标?
这是我的双链表容器:
#include <iostream>
using namespace std;
template <typename T>
class doubleLinkList {
typedef typename std::size_t size_type;
private:
struct rowNode {
rowNode* prev;
rowNode* next;
T data;
rowNode() : prev(nullptr), next(nullptr) {}
};
struct columnNode {
columnNode* prev;
columnNode* next;
T data;
columnNode() : prev(nullptr), next(nullptr) {}
};
struct
rowNode* head;
rowNode* tail;
unsigned count = 0;
public:
doubleLinkList();
~doubleLinkList();
void insert(T value);
size_type size() const { return tail - head; }
size_type max_size() { return std::numeric_limits<size_type>::max(); }
bool empty() const { return head == tail; };
};
template <typename T> doubleLinkList<T>::doubleLinkList() {
head = nullptr;
tail = head;
}
template <typename T> doubleLinkList<T>::~doubleLinkList()
{
while (head)
{
rowNode *tmp = head;
head = head->next;
delete tmp;
}
}
template <typename T> void doubleLinkList<T>::insert(T value) {
rowNode *node = new rowNode;
node->data = value;
// Case 1: There are no nodes yet
if (head == nullptr) {
head = node;
tail = head;
++count;
return;
}
// Case 2 - Inserting at the head of the list
if (node->data < head->data)
{
node->next = head;
head = node;
++count;
return;
}
// Case 3 - Inserting at the end of the list
if (node->data >= tail->data)
{
node->prev = tail;
tail->next = node;
tail = node;
++count;
return;
}
// General case - Inserting into the middle
rowNode* probe = head;
while (probe && (node->data >= probe->data))
{
probe = probe->next;
}
if (probe)
{
node->next = probe;
node->prev = probe->prev;
probe->prev->next = node;
probe->prev = node;
++count;
return;
}
return;
}
template <typename T> bool operator == (doubleLinkList<T> const& lhs, doubleLinkList<T> const& rhs) {
if (lhs.size() != rhs.size())
return false;
auto lhp = lhs.begin();
auto rhp = rhs.begin();
while (lhp != lhs.end()) {
if (*lhp++ != *rhp++)
return false;
}
return true;
}
template <typename T> bool operator < (doubleLinkList<T> const& lhs, doubleLinkList<T> const& rhs) {
auto lhp = lhs.begin();
auto rhp = rhs.begin();
while (lhp != lhs.end() && rhp != rhs.end()) {
if (*lhp < *rhp)
return true;
if (*lhp > *rhp)
return false;
++lhp;
++rhp;
}
if (lhp == lhs.end() && rhp != rhs.end())
return true;
return false;
}
template <typename T> inline bool operator != (doubleLinkList<T> const& lhs, doubleLinkList<T> const& rhs) {
return !(lhs == rhs);
}
template <typename T> inline bool operator > (doubleLinkList<T> const& lhs, doubleLinkList<T> const& rhs) {
return rhs < lhs;
}
template <typename T> inline bool operator <= (doubleLinkList<T> const& lhs, doubleLinkList<T> const& rhs) {
return !(lhs > rhs);
}
template <typename T> inline bool operator >= (doubleLinkList<T> const& lhs, doubleLinkList<T> const& rhs) {
return !(lhs < rhs);
}
int main() {
doubleLinkList<int> list;
list.insert(10);
list.insert(20);
list.insert(15);
list.insert(25);
}