所以我已经构建了一个已排序的链接列表并且它编译了所有爵士乐,我插入了第一个项目,但是当我尝试插入第二个项目时,我收到此错误:[main] 1000 (0) handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
我做了一个一点点搜索,发现它可能与我的插入函数或我的IsFull函数有关(我只能在列表中插入10个项目) - 但对于我的生活,我无法弄清楚是什么错了。也许有人可以帮忙吗? (我不确定发布整个代码是否有帮助,但无论如何我都会这样做)
NFL.h
#include <string>
const int MAX_ITEMS = 10;
enum RelationType {LESS, GREATER, EQUAL};
using namespace std;
#ifndef NFL_H
#define NFL_H
struct NFL {
string firstName;
string lastName;
string currentTeam;
string position;
string school;
};
#endif
sortedNFL.cpp
#include "sortedNFL.h"
struct NodeType
{
NFL player;
NodeType* next;
};
SortedNFL::SortedNFL() // Class constructor
{
length = 0;
nflList = NULL;
}
bool SortedNFL::IsFull() const
{
if (length == MAX_ITEMS || length > MAX_ITEMS)
{
return true;
}
else
return false;
/*NodeType* location;
try
{
location = new NodeType;
delete location;
return false;
}
catch(std::bad_alloc exception)
{
return true;
}*/
}
int SortedNFL::GetLength() const
{
return length;
}
void SortedNFL::MakeEmpty()
{
NodeType* tempPtr;
while (nflList != NULL)
{
tempPtr = nflList;
nflList = nflList->next;
delete tempPtr;
}
length = 0;
}
NFL SortedNFL::GetItem(NFL& playerRequested, bool& found)
{
bool moreToSearch; //flag for more items to search
NodeType* location;
location = nflList; //initial location is first item in NFL list
found = false; //flag for if item is found
moreToSearch = (location != NULL);
while (moreToSearch && !found) //while there is more to search and item is not found
{
if (playerRequested.lastName.compare(location->player.lastName) > 0)
{
location = location->next;
moreToSearch = (location != NULL);
}
if (playerRequested.lastName.compare(location->player.lastName) == 0 && playerRequested.firstName.compare(location->player.firstName) == 0)
{
found = true;
playerRequested = location->player;
}
if (playerRequested.lastName.compare(location->player.lastName) < 0)
{
moreToSearch = false;
}
}
return playerRequested;
}
void SortedNFL::PutItem(NFL inputPlayer)
{
NodeType* newNode; // pointer to node being inserted
NodeType* predLoc; // trailing pointer
NodeType* location; // traveling pointer
bool moreToSearch;
location = nflList;
predLoc = NULL;
moreToSearch = (location != NULL);
// Find insertion point.
while (moreToSearch) //while moreToSearch is true
{
if (inputPlayer.lastName.compare(location->player.lastName) > 0)
{
predLoc = location;
location = location->next;
moreToSearch = (location != NULL);
}
if (inputPlayer.lastName.compare(location->player.lastName) < 0)
{
moreToSearch = false;
}
}
// Prepare node for insertion
newNode = new NodeType;
newNode->player = inputPlayer;
// Insert node into list.
if (predLoc == NULL) // Insert as first
{
newNode->next = nflList;
nflList = newNode;
}
else
{
newNode->next = location;
predLoc->next = newNode;
}
length++;
}
/*void SortedNFL::DeleteItem(NFL playerDeleted)
{
NodeType* location = nflList;
NodeType* tempLocation;
// Locate node to be deleted.
if (playerDeleted.lastName.ComparedTo(nflList->player) == EQUAL)
{
tempLocation = location;
nflList = nflList->next; // Delete first node.
}
else
{
while (playerDeleted.lastName.ComparedTo((location->next)->player) != EQUAL)
location = location->next;
// Delete node at location->next
tempLocation = location->next;
location->next = (location->next)->next;
}
delete tempLocation;
length--;
}*/
void SortedNFL::ResetList()
{
currentPos = NULL;
}
NFL SortedNFL::GetNextItem()
{
NFL playerRequested;
if (currentPos == NULL)
currentPos = nflList;
playerRequested = currentPos->player;
currentPos = currentPos->next;
return playerRequested;
}
SortedNFL::~SortedNFL() //class destructor
{
NodeType* tempPtr;
while (nflList != NULL)
{
tempPtr = nflList;
nflList = nflList->next;
delete tempPtr;
}
}
答案 0 :(得分:1)
让我们看一下GetItem()中的主循环:
if (inputPlayer.lastName.compare(location->player.lastName) > 0)
{
predLoc = location;
location = location->next;
moreToSearch = (location != NULL);
}
让我们假装location
指向链接列表中的最后一个元素,并将比较评估为true
。因此,在if
语句中,您将NULL
放入location
,这是列表中的最后一个元素。
那么之后会立即发生什么?
if (inputPlayer.lastName.compare(location->player.lastName) < 0)
动臂。空指针取消引用。未定义的行为。