- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
长话短说:我制作了一个模板双链表,将列表放在其他列表中会导致问题。这个问题已经解决了。
我正在测试双链表作为模板,我遇到了一个问题,将一个链表放在另一个链表中会出现以下错误:
(微软 Visual Studio 2010 速成版)
Windows has triggered a breakpoint in linkListTesting.exe. This may be due to a corruption of the heap, which indicates a bug in linkListTesting.exe or any of the DLLs it has loaded. This may also be due to the user pressing F12 while linkListTesting.exe has focus. The output window may have more diagnostic information.
搜索此特定错误会产生类似“确保您不使用未初始化的变量”的结果 - 在这种情况下,当我将列表告诉 push_back/push_front 等时,它应该初始化列表我。(不按 F12,这样场景就结束了。)
这是我的主要cpp->
#include "vcLLMK2.h"
int main(int argc, char *argv[])
{
vcList<vcList<int>> a;
vcList<int> b;
b.push_back(1);
a.push_back(b);
//ERROR HAPPENS HERE APPARENTLY
return 0;
}
将新条目(在本例中为“1”)推回(或推前,也尝试过)到“b”中效果很好。没有崩溃,没有愚蠢。将“b”插入“a”会导致应用程序吐出上述错误,触发断点,然后死掉。
我最近开始尝试使用 SDL,并打算使用一个列表作为 16 层的容器,这些层基本上是要渲染的 sprite/tiles/等列表。思路是,程序将遍历包含层列表中的每个条目,然后在移动到下一个之前渲染包含层中的每个对象。
“为什么不直接使用 std::list?”-因为那是作弊。
虽然很可能在此线程中回答的人能够指出我的列表结构和实现中的每个明显缺陷,而我很幸运地没有意识到这些缺陷,但我最关心的是什么-特别是导致这个特殊错误以及如何补救它。也就是说,我想问你们的问题是我构建的链表设置有什么问题导致上述 cpp 在运行时出错?
下面的链接列表结构是阅读以下页面的结晶:
http://www.codeproject.com/Articles/26631/Using-C-templates-for-functions-and-operators
http://www.cprogramming.com/tutorial/lesson15.html
http://geekswithblogs.net/MarkPearl/archive/2010/02/20/linked-lists-in-c.aspx
把它变成模板主要是对这个页面的创造性解释的结果:
http://www.cplusplus.com/doc/tutorial/templates/
这是我的链接列表模板类,有很多注释->
#pragma once
#ifndef vcLLMK2_H_INCLUDED
#define vcLLMK2_H_INCLUDED
#define NULL 0
//THIS IS ALL OUR NODE SHOULD EVER NEED
template <class T>
struct vcListNode
{
public:
T data;
vcListNode<T> *next;
vcListNode<T> *prev;
};
template <class T>
struct vcList
{
public:
vcListNode<T> *root;//the list's "start"
vcListNode<T> *end;//the list's "end"
vcListNode<T> *cur;//used to bounce around amongst entries
int size();//returns number of non-null entries
int count;//because keeping track of these is easier than iterating through every time
void setAt(T,int);//sets data in a node
void push_front(T);//inserts a new node at the beginning of the list
void push_back(T);//same thing but back of list
void removeAt(int);//kills a node at an arbitrary spot in the list
void clear();//deletes all of the nodes in the list
T getAt(int);//returns data from a node in the list
vcList<T>& operator = (const vcList<T>&);//used for setting list a's contents to list b
//EDIT COPYCONSTRUCTOR
vcList<T>(const vcList<T>&);//copyconstructor
vcList();// : root(NULL),end(root),cur(NULL),count(0){};
~vcList();
};
//constructor - sets everything to null and count to zero on init
template <class T>
vcList<T>::vcList()
{
root=NULL;
cur=NULL;
end=NULL;
count=0;
}
//destructor - deletes all nodes
template <class T>
vcList<T>::~vcList()
{
clear();
}
//deletes all nodes from root to end
template <class T>
void vcList<T>::clear()
{
if(root==NULL)//assume list has nothing in it, abort
return;
//set current targeted entry to root
cur = root;
//as long as we have somewhere to go...
while(cur->next!=NULL)
{
//go there
cur=cur->next;
//and destroy where we were
delete cur->prev;
}
//then destroy ourselves because nihilism is good for memory
delete cur;
}
//used to set the contents of a list equal to those of another
template <class T>
vcList<T>& vcList<T>::operator= (const vcList<T> &fays)
{
//for starters, clear ourselves of any unwanted garbagedata
clear();
//check the import list's root entry
cur = fays.root;
//if the list containing the values we are importing is empty, we're done, move along
if(cur==NULL)
return *this;
//otherwise, make a new node - it's our new root
vcListNode<T> *newEntry = new vcListNode<T>;
newEntry->data = fays.root->data;
newEntry->prev = NULL;
newEntry->next = NULL;
//set root/end to the new entry
root = newEntry;
end = newEntry;
//update our count
count=1;
//fire through the import-list's entries
//cur starts at root
while(cur->next!=NULL)//(count<fays.count)
{
//move to next entry
cur=cur->next;
//add it to our list, push_back should update the location of 'end' for us
push_back(cur->data);
}
//we should be done here, so go ahead and return everything
return *this;
}
//this is mostly for convenience
template <class T>
int vcList<T>::size()
{
return count;
}
//adds a new entry to the front of our linked list
template <class T>
void vcList<T>::push_front(T info)
{
//eat some memory
vcListNode<T> *newEntry;
newEntry = new vcListNode<T>;
//set our memory and all that neat stuff
newEntry->data = info;
newEntry->next = root;
newEntry->prev = NULL;
//if our linked list is not empty
if(root!=NULL)
//set root's previous link to point at our new entry
root->prev = newEntry;
//if our linked list does not have an assigned end yet
if(end==NULL)
//assume it's empty and set end to our entry
end=newEntry;
//update the position of our root in the list, the beginning now begins at the beginning again
root = newEntry;
//and since we added something to the list, we should probably update our count of things in the list
count++;
}
//this finds an element in the pointer-chain and sets its information
template <class T>
void vcList<T>::setAt(T info,int index)
{
//set our target to the root
cur=root;
//run through the list's entries until we're where we're supposed to be
while(index>0)
{
cur=cur->next;
index--;
}
//set the data in the cell/node/whatever to our input
cur->data=info;
}
//returns the data contained in a node at a position in the list
template <class T>
T vcList<T>::getAt(int meBro)
{
//set target to root
cur=root;
//progress through the list
while(meBro>0)
{
cur=cur->next;
meBro--;
}
//dig the data out of the entry and return it
return cur->data;
}
//adds an element-containing node to the end of the list-chain
template <class T>
void vcList<T>::push_back(T info)
{
//if our list already has entries, end shouldn't be null
if(end!=NULL)
//so just target our end slot
cur=end;
//if our list is empty, however
else
//target the root instead
cur=root;
//create our new node, put stuff in it, etc
vcListNode<T> *newEntry;
newEntry = new vcListNode<T>;
newEntry->data = info;
//we're adding to the END of the list so make next null
newEntry->next = NULL;
//if cur is NOT null, then theoretically we're pointed at the end of the list
if(cur!=NULL)
//set our new entry's previous pointer to the end
newEntry->prev = cur;
//cur IS null, which means the list is empty
else
//set our entry's previous pointer to be null, duh
newEntry->prev = NULL;
//if the end of our list exists
if(end!=NULL)
{
//set the next entry in the list to point at our new entry
end->next = newEntry;
//then set end to target the new entry
end=newEntry;
}
//and if our list does not have an end yet for some reason (read as: it's empty)
else
{
//set the root to our new entry
root = newEntry;
//set the end to our new entry as well, since there's only one entry in the list
end = newEntry;
}
//update count of number of objects in list
count++;
}
//this deletes/removes/destroys/obliterates a node at a location in the list
template <class T>
void vcList<T>::removeAt(int index)
{
//for starters - is what we're trying to kill even here?
if(index>=count)
//NOPE GET OUT
return;
//later on it might speed things up to check whether the distance from end or root is shorter
//for now, just start at the beginning
//target the root
cur=root;
//move through the list to the specified entry
while(index>0)
{
index--;
cur=cur->next;
}
//if the previous entry exists
if(cur->prev!=NULL)
//point its next at the entry after this one
cur->prev->next=cur->next;
//if the previous entry is NULL, it means we're at the root
//so tell root to scoot forward one entry
//if there's a forward to scoot to
else if(cur->next != NULL)
root = cur->next;
//if the next entry exists
if(cur->next!=NULL)
//set the next entry's previous pointer to point at the entry before the targeted one
cur->next->prev=cur->prev;
//if the next entry does not exist, we must be at the end of the list
//so tell the end of the list to scoot back one slot
//if there's a back-one-slot to go to
else if(cur->prev!=NULL)
end = cur->prev;
//remove the entry at the targeted location
delete cur;
//decrement our count
count--;
}
//EDIT -> Copy Constructor
//copy constructor, similar as suggested to the operator=
template <class T>
vcList<T>::vcList(const vcList<T>& fays)
{
//might not be completely necessary, but we're not hurting anything by making sure
clear();
//check the import list's root entry
cur = fays.root;
//if the list containing the values we are importing is empty, we're done, move along
if(cur==NULL)
return;//just return, constructors don't get return types
//otherwise, make a new node - it's our new root
vcListNode<T> *newEntry = new vcListNode<T>;
newEntry->data = fays.root->data;
newEntry->prev = NULL;
newEntry->next = NULL;
//set root/end to the new entry
root = newEntry;
end = newEntry;
//update our count
count=1;
//fire through the import-list's entries
//cur starts at root
while(cur->next!=NULL)//(count<fays.count)
{
//move to next entry
cur=cur->next;
//add it to our list, push_back should update the location of 'end' for us
push_back(cur->data);
}
//we should be done here, so go ahead and return everything
//return *this;
}
#endif
最佳答案
我认为您应该看一下 C++ 中最重要的概念之一,即按引用传递。 push_back 方法作为名为 info 的参数,它是您用来调用该方法的对象的拷贝。由于您没有明确告诉编译器如何创建 vsList 对象的拷贝,因此它将创建一个默认的复制构造函数,但它不会按预期工作。
所以当调用存储在对象“a”中的对象的析构函数时,您的代码确实会出现问题。因为它没有有效的指针,所以它会崩溃。您可以通过两种方式解决您的问题,更改 push_back(和所有其他方法)以通过“const 引用”而不是通过值接收 T 或实现复制构造函数。
关于c++ - 包含链表的链表给出堆损坏错误 : Why?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13148375/
今天我们将开始第二个数据类型-链表的学习,同样我们还是用最原始的方式,自己申请内存管理内存来实现一个链表。 01、01、定义 什么是链表?链表在物理存储结构上表现为非顺序性和非连续性,因此链表
前言:笔记是参考B站up主尚硅谷,图片、代码都是哦。在blog写笔记~(图片、代码来源尚硅谷,侵权必删!) 尚硅谷数据结构学习路线B站网站:https://www.bilibili.com/video
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我想创建一个没有全局变量的单个链表。我用 NULL 初始化了第一个元素,然后想将第一个元素 node 复制到 list_。它被复制到函数中,但副作用不起作用。在我的主函数中,该值仍然是NULL。如果我
我正在尝试使链表与此处的链表相似: linked list in C 那就是在另一个结构中有“头”,我首先称它为“头”。但是我发现做那个改变。很难向 list_item 结构添加值。我已经尝试了一些东
我正在尝试理解链表的代码。我明白他们是如何工作的。我正在查看一些与动态内存和链表有关的代码,我在此处对其进行了简化: #include #include typedef struct nod
有人可以解释下面的代码吗?我是 C 的新手,正在努力弄清楚。为什么我们最后有 queueNodeT? typedef char queueElementT; typedef struct queueN
场景如下:- 我想反转单链表的方向,换句话说,反转后所有指针现在应该指向后.. 这个算法应该需要线性时间。 我想到的解决方案是使用另一个数据结构 A Stack.. 借助它可以轻松反转单向链表,所有指
在 python 中使用链表最简单的方法是什么?在 scheme 中,链表由 '(1 2 3 4 5) 定义。 Python 的列表 [1, 2, 3, 4, 5] 和元组 (1, 2, 3, 4,
本文首发公众号:小码A梦 一般数据主要存储的形式主要有两种,一种是数组,一种是链表。数组是用来存储固定大小的同类型元素,存储在内存中是 一片连续 的空间。而链表就不同于数组。链表
虽然之前有人问过关于链表与数组的问题,但答案大多归结为我们大多数人在某个时候可能已经学到的东西: 列表擅长插入和删除 数组擅长随机访问 现在像 Bjarne Stroustrup 这样受人尊敬的人有
位置 在堆中,碎片化(每个节点的 malloc) - 在几种不同的方式(缓慢分配,缓慢访问,内存碎片)方面效率低下 在堆中,在一个大块中 - 当需要重新分配 时,数据结构获得的所有灵活性都将丢失 在堆
我完成了泛型的学习,但并不容易。不过,我确实明白了。这是我的理解。我希望您纠正我的错误并回答几个问题:)。 public class LinkedList { //class definition }
我将如何创建一个链接列表来在 OCaml 中保存我的数据?我正在尝试制作一个单链表,但是我遇到了语法问题。我只想制作一个模块来简单地从链表中获取'a,插入'a或删除'a。 有人知道吗? 最佳答案 正如
我在使用这段代码时遇到了问题,我不确定我做错了什么 #include #include #include #include typedef struct flight_struct{
我正在创建一个函数来删除给定列表的最后一个节点(作为参数输入)。该函数本身非常简单,如下所示。 function popBack(list) { var current = list.head
我正在尝试开发一种方法,该方法将在链接列表中的当前节点之前插入传递给它的节点。它有3个条件。对于此实现,不能有任何头节点(仅对列表中第一个节点的引用),并且我无法添加更多变量。 如果列表为空,则将传递
使用 scala,我已将大约 100000 个节点添加到链表中。当我使用函数 length 时,例如 mylist.length。我收到“java.lang.StackOverflowError”错误
所以我正在学习处理链表。我将如何递归地添加节点内的项目。我可以通过执行 sum = h.item +h.next.item+h.next.next.item 添加它们,但这只有在我有小的链接列表时才有
所以我一直在努力理解链表的概念(一直在看一些示例代码,我在互联网上找到了这个。现在如果我能请别人确认我是否正确掌握了一些概念。我将绘制图表,说明我认为每个代码链接的作用。 #include #inc
我是一名优秀的程序员,十分优秀!