[英]Sorted Linked List Destructor c++
我目前正在开发一个将电影标题添加到已排序的链表的程序,并且我一直运行“ Segmentation fault:11”,但我不知道它来自何处。 这是我的规范文件和客户端代码。
#include <string>
#include <iostream>
#include "Movies.h"
using namespace std;
struct NodeList {
string movieName; //data
NodeList* next; //points to next item
};
Movies::Movies()
{
headOfList = NULL;
length = 0;
currentPos = NULL;
}
void Movies::insertMovie(string movieName)
{
NodeList* tempPtr = new NodeList;
tempPtr->movieName = movieName;
if(headOfList == NULL)
{
headOfList = tempPtr;
}
else {
currentPos = headOfList;
NodeList* trail = NULL;
while(currentPos != NULL)
{
if(currentPos->movieName >= tempPtr->movieName)
{
break;
}
else
{
trail = currentPos;
currentPos = currentPos->next;
}
if(currentPos == headOfList) {
tempPtr->next = headOfList;
headOfList = tempPtr;
}
else {
tempPtr->next = currentPos;
trail->next = tempPtr;
}
}
}
length++;
}
Movies::~Movies()
{
NodeList* temp;
while(headOfList != NULL)
{
temp = headOfList;
headOfList = headOfList->next;
delete currentPos;
}
}
然后这是我的客户
#include <iostream>
#include <string>
#include "Movies.h"
using namespace std;
int main()
{
Movies myMovieList;
myMovieList.insertMovie("Harry Potter");
myMovieList.printList();
return 0;
}
我认为我的问题可能出在析构函数上,但是每次我尝试不同的操作时,都会遇到相同的错误。
没有足够的声誉来发表评论,因此发布了一个新帖子。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.