在我的排序列表中插入项目时出现问题
Issue inserting items into my sorted list?
所以我构建了一个排序的链表,它编译了所有的爵士乐,我很好地插入了第一个项目,但当我试图插入第二个项目时,我得到了这个错误:[main] 1000 (0) handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
我做了一点搜索,发现它可能与我的insert函数或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
排序NFL.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;
}
}
让我们看看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)
轰。空指针取消引用。未定义的行为。
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- Eulers 项目问题 no 345 听不懂几行代码
- 屏幕插入运算符<<的运算符过载问题
- 我想直接在结构中插入,但没有一种方法可以正确避免填充问题
- 基本 Cuda C++项目集成问题
- C++ 从链表中删除项目时出现问题
- 插入操作的二叉搜索树代码问题
- 无法在我的链接列表中插入项目,head始终为NULL
- 将项目插入 CTreeCtrl 时线程函数的奇怪行为
- 双向链表问题 - 包含项目但不显示
- 在单元测试项目中包括 .c 文件,并从多个 cpp 文件访问它而不会出现链接问题
- 当索引位于末尾和空数组时,向数组插入元素时出现问题
- 如何在C 中以给定索引列表中的项目插入项目
- 如何在滚动区域中动态插入项目并在QT中加载滚动条
- 在 protobuf 列表的开头插入项目
- 如何在 doxygen 的注释中插入项目名称
- Qt内置数据结构中是否有忽略重复插入项目的数据结构
- 在我的排序列表中插入项目时出现问题
- 链接器问题将C++项目从vs2005移动到vs2010
- 如何在std::map中插入项目而不违反MISRA C++2008必需规则5-2-12