在我的排序列表中插入项目时出现问题

Issue inserting items into my sorted list?

本文关键字:问题 插入项目 我的 排序 列表      更新时间:2023-10-16

所以我构建了一个排序的链表,它编译了所有的爵士乐,我很好地插入了第一个项目,但当我试图插入第二个项目时,我得到了这个错误:[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)

轰。空指针取消引用。未定义的行为。