I'm pretty new to linked lists - I understand the concept and how they work - but coding them is a bit more...complicated. I have some code below that I'm using for homework - essentially we're to create a struct containing data for a football player, and then create a linked list and add the struct to it. I'm not 100% sure on how my code looks as I haven't compiled it - I just want to make sure that one part looks solid before I continue onto anything else.
So far I have the following:
NFL.h
#include <string>
using namespace std;
#ifndef NFL_H
#define NFL_H
struct NFL {
string firstName;
string lastName;
string currentTeam;
string Position;
string school;
};
#endif
sortedNFL.h
#include "NFL.h"
// Header file for Sorted List ADT.
struct NodeType;
#ifndef SORTEDNFL_H
#define SORTEDNFL_H
class SortedNFL
{
public:
SortedNFL(); // Class constructor
~SortedNFL(); // Class destructor
bool IsFull() const;
int GetLength() const;
void MakeEmpty();
NFL GetItem(NFL& playerRequested, bool& found);
void PutItem(NFL inputPlayer);
void DeleteItem(NFL playerDeleted);
void ResetList();
NFL GetNextItem();
private:
NodeType* nflList;
int length;
NodeType* currentPos;
};
#endif
sortedNFL.cpp
#include "sortedNFL.h"
struct NodeType
{
NFL player;
NodeType* next;
};
sortedNFL::sortedNFL() // Class constructor
{
length = 0;
nflList = NULL;
}
bool sortedNFL::IsFull() const
{
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;
NodeType* location;
location = nflList;
found = false;
moreToSearch = (location != NULL);
while (moreToSearch && !found)
{
switch(playerRequested.lastName.ComparedTo(location->player))
{
case GREATER: location = location->next;
moreToSearch = (location != NULL);
break;
case EQUAL: found = true;
item = location->player;
break;
case LESS: moreToSearch = false;
break;
}
}
return item;
}
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)
{
switch(inputPlayer.lastName.ComparedTo(location->player))
{
case GREATER: predLoc = location;
location = location->next;
moreToSearch = (location != NULL);
break;
case LESS: moreToSearch = false;
break;
}
}
// 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 item;
if (currentPos == NULL)
currentPos = nflList;
item = currentPos->player;
currentPos = currentPos->next;
return item;
}
sortedNFL::~sortedNFL()
{
NodeType* tempPtr;
while (nflList != NULL)
{
tempPtr = nflList;
nflList = nflList->next;
delete tempPtr;
}
}
I'm not entirely sure I grasp the concept of inserting structs into linked lists - but I think this would cover it?