C++ 当链表中有两个以上的项目时,链表的函数add会崩溃吗?
下面列出了使用链表概念的模板类的add函数。出于某种原因,列表中有两个对象会导致程序在运行时崩溃,而只有一个对象不会导致任何问题。我已经研究代码一段时间了,但仍然没有找到错误的地方。我确保为新项目动态分配内存,这样我的指针就不会指向垃圾内存。下面列出了MapItem的结构和函数add,让大家了解一下C++ 当链表中有两个以上的项目时,链表的函数add会崩溃吗?,c++,templates,pointers,memory,linked-list,C++,Templates,Pointers,Memory,Linked List,下面列出了使用链表概念的模板类的add函数。出于某种原因,列表中有两个对象会导致程序在运行时崩溃,而只有一个对象不会导致任何问题。我已经研究代码一段时间了,但仍然没有找到错误的地方。我确保为新项目动态分配内存,这样我的指针就不会指向垃圾内存。下面列出了MapItem的结构和函数add,让大家了解一下 template <class keyType, class valueType> void Map<keyType, valueType>::add(const keyTy
template <class keyType, class valueType>
void Map<keyType, valueType>::add(const keyType &key, const valueType &value)
{
struct MapItem<keyType, valueType> *newItem; //create pointer to new map item
newItem = new MapItem<keyType, valueType>; //dynamically allocate a new item on the heap
newItem->key = key; //assign the key
newItem->value = value; //assign the value
if(sizeList == 0) //if linked list is empty, make newItem the HEAD
{
sizeList++; //increment size
head = newItem; //set the HEAD of the list to newItem
tail = newItem; //set the TAIL of the list to newItem
newItem->prev = head; //previous item is the head (itself)
newItem->next = head; //next item is the head (itself)
}
else //if the linked list is not empty, add it in
{
struct MapItem<keyType, valueType> *temp = head; //store the first element in the linked list in temp variable
if(sizeList == 1) //if there is only one element in the list, check if they equal eachother
{
if(head->key != key)
{
tail = newItem; //assign newItem as the TAIL
head->next = tail; //assign the next of HEAD to the new map item
head->prev = tail; //assign the previous of HEAD as the newItem (tail)
tail->prev = head; //assign head to PREV of newItem (tail)
tail->next = head; //assign HEAD to NEXT of newItem (tail)
sizeList++; //increment size of list
}
// else
// cout<<"Same key already exists"<<endl;
}
else
{
bool sameKey = false; //boolean value to check if the same key already exists, and if it does it will stop the loop
int i = 1; //which item we are looking at in the list
while(i <= sizeList && !sameKey) //while not past the end of the list, keep checking if a similar key exists
{
if(temp->key == newItem->key)
sameKey = true;
else
{
temp = temp->next; //go to the next map item
i++;
}
}
if(!sameKey) //if the same key has not been found
{
temp->next = newItem; //assign newItem to NEXT of last node (temp) in our list
newItem->prev = temp; //assign temp to PREV of newItem
newItem->next = head; //assign HEAD to NEXT of newItem
head->prev = newItem; //assign newItem to PREV of head
tail = newItem; //assign newItem as the TAIL
sizeList++;
}
else
delete newItem; //deallocate memory of newItem
}
}
}
模板
无效映射::添加(常量键类型和键、常量值类型和值)
{
struct MapItem*newItem;//创建指向新映射项的指针
newItem=new MapItem;//在堆上动态分配一个新项
newItem->key=key;//分配密钥
newItem->value=value;//分配值
if(sizeList==0)//如果链表为空,则将newItem作为标题
{
sizeList++;//增量大小
head=newItem;//将列表的头设置为newItem
tail=newItem;//将列表的尾部设置为newItem
newItem->prev=head;//上一项是head(本身)
newItem->next=head;//下一项是head(本身)
}
else//如果链接列表不是空的,请将其添加到
{
struct MapItem*temp=head;//将链表中的第一个元素存储在temp变量中
if(sizeList==1)//如果列表中只有一个元素,请检查它们是否相等
{
如果(头->键!=键)
{
tail=newItem;//将newItem指定为tail
head->next=tail;//将下一个head分配给新的映射项
head->prev=tail;//将前一个head指定为新项(tail)
tail->prev=head;//将head分配给newItem的prev(tail)
tail->next=head;//将head分配给newItem的下一个(tail)
sizeList++;//列表的增量大小
}
//否则
//coutprev=temp;//将temp分配给newItem的PREV
newItem->next=head;//将head分配给newItem的下一个
head->prev=newItem;//将newItem分配给head的prev
tail=newItem;//将newItem指定为tail
sizeList++;
}
其他的
delete newItem;//释放newItem的内存
}
}
}
下面是MapItem的结构:
template <class keyType, class valueType>
struct MapItem
{
keyType key;
valueType value;
MapItem<keyType, valueType> *prev, *next;
};
模板
结构映射项
{
键型键;
值类型值;
MapItem*prev,*next;
};
析构函数:
template <class keyType, class valueType>
Map<keyType, valueType>::~Map()
{
struct MapItem<keyType, valueType> *temp; //create temp variable to hold which item we are looking at in the list
temp = head; //start at the head
for(int i = 1; i <=sizeList; i++)
{
delete temp; //delete memory to pointed by temp
if(i != sizeList) //if we are not at the last node
temp = temp->next;
}
}
模板
地图::~Map()
{
struct MapItem*temp;//创建临时变量以保存列表中正在查看的项目
temp=head;//从head开始
对于(int i=1;i next;
}
}
我想我明白了-更换线路
temp->next = newItem; //assign newItem to NEXT of last node (temp) in our list
与
因为当你在项目中循环寻找关键点时,你会回到head,所以循环结束后,“temp”指向“head”。但是你想在最后添加
在此更改之前,我确实在销毁过程中遇到了内存访问冲突(尽管我不明白为什么在删除析构函数代码后仍然出现一些错误)
为清晰起见,析构函数代码:
~Map()
{
struct MapItem<keyType, valueType> *temp; //create temp variable to hold which item we are looking at in the list
temp = head; //start at the head
for(int i = 1; i <=sizeList; i++)
{
MapItem<keyType, valueType> *next = temp->next;
delete temp; //delete memory to pointed by temp
if(i != sizeList) //if we are not at the last node
temp = next;
}
}
~Map()
{
struct MapItem*temp;//创建临时变量以保存列表中正在查看的项目
temp=head;//从head开始
对于(int i=1;i next;
delete temp;//删除临时指向的内存
if(i!=sizeList)//如果我们不在最后一个节点
温度=下一个;
}
}
您会收到什么样的异常/错误消息?何时发生-当您添加第二项或第三项时?您使用哪些类作为keyType和valueType?感谢您的回复。当我添加第二项时,只要我按任意键退出程序,程序就会崩溃。如果我创建了一个映射并只添加了一个MapItem,我就可以退出程序正常运行时,Windows不会说“Project1.exe已停止工作”。对于keyType和valueType,我一直在使用字符串来检查它们的功能。所以这将是映射测试。然后我会说,test.add(“A”,“BC”),这很好,然后我会做test.add(“B”,“BC”),这将被添加进去,但会导致程序在之后崩溃。您的析构函数似乎与此相关。将其添加到帖子中。我添加了析构函数,尝试删除它并编译代码,但仍然出现相同的问题。删除“temp”后,您可能不应该访问“temp->next”,这将是一个开始。好的,gi让我试一下,谢谢!好吧,我添加了它并修复了逻辑,但仍然是同一个问题…让我们说我再也不会在深夜编写代码了lol。我不知道…使用固定析构函数(为了清楚起见,我添加了它)我停止了这个问题…好了,它现在正在工作!我必须重建解决方案,退出,然后重新打开,现在它工作得很好。Visual Studio在我身上变得怪异……但是这是修复的人,非常感谢!你不知道我在这LOL上停留了多长时间。我在午夜结束了这样的3个类的编码,因此我的逻辑是什么?有时会走掉。我怎样才能更好地感谢你??
~Map()
{
struct MapItem<keyType, valueType> *temp; //create temp variable to hold which item we are looking at in the list
temp = head; //start at the head
for(int i = 1; i <=sizeList; i++)
{
MapItem<keyType, valueType> *next = temp->next;
delete temp; //delete memory to pointed by temp
if(i != sizeList) //if we are not at the last node
temp = next;
}
}