如何在链表中创建链表
How can I create linked lists within a linked list?
我正在C++中创建蛇游戏,但是我在使用我创建的linkedList类时遇到了一些问题。当我在 Python 中制作蛇时,我在列表中制作列表来表示构成我的蛇的每个圆圈的 x 和 y 位置。我正在尝试在C++做类似的事情。这是我制作的模板化 linkedList 类,如果出于某种原因您需要查看它:
#ifndef LLIST_H_INCLUDED
#define LLIST_H_INCLUDED
#include <cstddef>
#include <iostream>
using namespace std;
template <class T>
class linkedList
{
public:
class node
{
public:
///node class attributes
T mPayload;
node* mNext;
///constructor
node(T toucan):mPayload(toucan),mNext(NULL)
{}
///destructor
~node()
{
///cascading delete
if(mNext)
delete mNext;
}
///node class methods
};
///linkedList class attributes
node* mStart;
///constructor
linkedList():mStart(NULL)
{}
///destructor
~linkedList()
{
///initializes the cascading delete.
if(mStart)
delete mStart;
}
///linkedList class methods
T mReturnT(int indx)
{
if(!mStart)
return NULL;
else
{
node* cur;
for(int i = 0; i<indx; i++)
{
if(!cur->mNext)
{
cout << "Indx out of range. Deleting last item." << endl;
break;
}
cur = cur->mNext;
}
delete cur;
return cur->mPayload;
}
}
void mInsert(int indx, T data)
{
///Insert an item at a given position.
///The first argument is the index of
///the element before which to insert.
node* cur = mStart;
for(int i = 0; i < indx; i++)
{
if(!cur->mNext)
break;
else
{
cur = cur->mNext;
}
}
node* N = new node(data);
node* temp = cur->mNext;
cur->mNext = N;
N->mNext = temp;
}
T mPop()
{
///Removes the last item in the list,
///and returns it.
if(!mStart)
return NULL;
else
{
node* cur = mStart;
while(cur->mNext)
{
cur = cur->mNext;
}
T var = cur->mPayload;
delete cur;
return var;
}
}
int mSize()
{
if(!mStart)
return 0;
else
{
node* cur = mStart;
int counter = 1;
while(cur->mNext)
{
cur = cur->mNext;
counter++;
}
delete cur;
return counter;
}
}
void mPrint()
{
///prints all values in a list.
node* cur;
if(!mStart)
cout << "List is empty." << endl;
else
{
cur = mStart;
cout << "[";
while(cur)
{
cout << cur->mPayload << " ";
cur = cur->mNext;
}
cout << "]" << endl;
delete cur;
}
}
void swapNodes(node* N)
{
///idk
}
void bubbleSort()
{
if(!mStart)
return;
node* cur = mStart;
while(cur)
{
cout << cur->mPayload << endl;
if(cur->mRight->mFrequency > cur->mFrequency)
{
swapNodes(cur);
cur = mStart;
}
else
cur = cur->mRight;
}
delete cur;
}
};
#endif // LLIST_H_INCLUDED
现在在我的主.cpp中,我想做这样的事情,以便我有一个链表的链接列表:
linkedList<linkedList> p1Snake;
linkedList<int> startingPiece;
startingPiece.mInsert(0,600); //This is the starting y position of
// p1snake added to the front of the list.
startingPiece.mInsert(0,350); //This is the starting x position of
//p1snake added to the front of the list.
p1Snake.mInsert(0,startingPiece);
我的问题出现在该代码的第一行。错误:模板类类链接列表的模板参数列表中参数 1 的类型/值不匹配。我该如何解决这个问题?
你的lnkedList
类有 1 个模板化类型T
,所以变量定义的语法应该是:
linkedList<type> variableName;
通过递归,type
可以linkedList
,但它仍然应该以上述形式存在(有type
)。因此,例如,如果最后一个数据类型是 int
:
linkedList<linkedList<int> > variableName;
这需要是一个链表的链表:
linkedList<linkedList> p1Snake;
大致如下:
linkedList< linkedList< something > > p1Snake;
相关文章:
- 使用std::list创建循环链表
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 在C++中为链表类创建实例
- C++ 双向链表:使用矩阵类成员创建和填充列表
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 创建了一个链表,但如何删除 c++ 中的"所有"节点
- 在解决链表问题时创建一个额外的节点是一个好习惯吗?
- 无法创建或返回反向链表
- 在 C++ 中创建单个链表
- 如果我们不创建一个新节点并使用指针插入数据并建立链接(在链表中)怎么办?
- 程序在为链表创建推送函数时崩溃
- 自定义链表创建 RtlValidateHeap 错误,其结构具有链表
- 为数据缺少默认构造函数的链表创建节点
- 如何为链表C++创建"弹出尾巴"函数
- 如何为双链表创建交换函数
- 在链表创建循环中插入
- 正在为链表c++创建DataType
- 为链表C++创建节点
- 如何使用swap函数为双重链表创建冒泡排序
- 如何为链表创建复制构造函数