gpt4 book ai didi

c++ - 将项目插入我的排序列表时出现问题?

转载 作者:行者123 更新时间:2023-11-28 06:37:58 25 4
gpt4 key购买 nike

因此,我构建了一个排序的链表并进行了编译和所有爵士乐,我插入第一个项目很好,但是当我尝试插入第二个项目时,我得到这个错误:[main] 1000 ( 0) handle_exceptions: Exception: STATUS_ACCESS_VIOLATION 我做了一些搜索,发现它可能与我的插入函数或我的 IsFull 函数有关(我应该只能将 10 个项目插入列表中) - 但对于我的生活,我无法弄清楚它有什么问题。也许这里有人可以帮忙? (我不确定发布全部代码是否有帮助,但无论如何我都会这样做)

美国橄榄球联盟.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++ - 将项目插入我的排序列表时出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26476658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com