[英]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.