First: This is for homework and no I am not asking for the entire solution I just need help figuring out how to do this. Now, I've been really sick the past few days and I missed the class that went over linked lists so I'm really confused on this. The teacher gave us most of the code and asked us to add a SplitLists function which I already did and he asked us to change the DeleteItem function so that it will not do anything when an item specified is not in the list and make sure ALL copies of the item are removed if it exist. So the test input he gave us fills the list with these numbers: 5, 7, 6, 9, and 9. Right now when I run the code without changing the DeleteItem function and using the test inputs DeleteItem 5, DeleteItem 2, and DeleteItem 9, it deletes 5 but then it stops working when I try to Delete 2. So what I can't figure out is how to make it keep going even if the item is not in the list and how to make it delete both 9's at once. I did it with an array based list but I just can't figure out how to do it with linked list. I've tried looking at videos on linked lists, looking at slides from class, and other websites and references and I just can't figure it out (though it doesn't help that I'm still really sick). So I appreciate any advice or hints.
here is the code containing the DeleteItem() function and the other class that goes with it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// The following declarations and definitions go into file
// ItemType.h.
#include <fstream>
constint MAX_ITEMS = 5;
enum RelationType {LESS, GREATER, EQUAL};
class ItemType
{
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Print(std::ostream&) const;
void Initialize(int number);
private:
int value;
};
#include <fstream>
#include <iostream>
#include "ItemType.h"
ItemType::ItemType()
{
value = 0;
}
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
if (value < otherItem.value)
return LESS;
elseif (value > otherItem.value)
return GREATER;
elsereturn EQUAL;
}
void ItemType::Initialize(int number)
{
value = number;
}
void ItemType::Print(std::ostream& out) const
// pre: out has been opened.
// post: value has been sent to the stream out.
{
out << value;
}
// This file contains the linked implementation of class
// UnsortedType.
#include "UnsortedType.h"
struct NodeType
{
ItemType info;
NodeType* next;
};
UnsortedType::UnsortedType() // Class constructor
{
length = 0;
listData = NULL;
}
bool UnsortedType::IsFull() const
// Returns true if there is no room for another ItemType
// on the free store; false otherwise.
{
NodeType* location;
try
{
location = new NodeType;
delete location;
returnfalse;
}
catch(std::bad_alloc exception)
{
returntrue;
}
}
int UnsortedType::GetLength() const
// Post: Number of items in the list is returned.
{
return length;
}
void UnsortedType::MakeEmpty()
// Post: List is empty; all items have been deallocated.
{
NodeType* tempPtr;
while (listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
length = 0;
}
void UnsortedType::PutItem(ItemType item)
// item is in the list; length has been incremented.
{
NodeType* location; // Declare a pointer to a node
location = new NodeType; // Get a new node
location->info = item; // Store the item in the node
location->next = listData; // Store address of first node
// in next field of new node
listData = location; // Store address of new node into
// external pointer
length++; // Increment length of the list
}
ItemType UnsortedType::GetItem(ItemType& item, bool& found)
// Pre: Key member(s) of item is initialized.
// Post: If found, item's key matches an element's key in the
// list and a copy of that element has been stored in item;
// otherwise, item is unchanged.
{
bool moreToSearch;
NodeType* location;
location = listData;
found = false;
moreToSearch = (location != NULL);
while (moreToSearch && !found)
{
switch (item.ComparedTo(location->info))
{
case LESS :
case GREATER : location = location->next;
moreToSearch = (location != NULL);
break;
case EQUAL : found = true;
item = location->info;
break;
}
}
return item;
}
void UnsortedType::DeleteItem(ItemType item)
// Pre: item's key has been initialized.
// An element in the list has a key that matches item's.
// Post: No element in the list has a key that matches item's.
{
NodeType* location = listData;
NodeType* tempLocation = NULL;
// Locate node to be deleted.
if (item.ComparedTo(listData->info) == EQUAL)
{
tempLocation = location;
listData = listData->next; // Delete first node.
}
else
{
while (item.ComparedTo((location->next)->info) != EQUAL)
location = location->next;
// Delete node at location->next
tempLocation = location->next;
location->next = (location->next)->next;
}
delete tempLocation;
length--;
}
void UnsortedType::ResetList()
// Post: Current position has been initialized.
{
currentPos = NULL;
}
ItemType UnsortedType::GetNextItem()
// Post: A copy of the next item in the list is returned.
// When the end of the list is reached, currentPos
// is reset to begin again.
{
ItemType item;
if (currentPos == NULL)
currentPos = listData;
else
currentPos = currentPos->next;
item = currentPos->info;
return item;
}
UnsortedType::~UnsortedType()
// Post: List is empty; all items have been deallocated.
{
NodeType* tempPtr;
while (listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
}
void UnsortedType::DeleteItem(ItemType item)
// Pre: item's key has been initialized.
// An element in the list has a key that matches item's.
// Post: No element in the list has a key that matches item's.
{
NodeType* location = listData;
NodeType* tempLocation = NULL;
// Locate node to be deleted.
if (item.ComparedTo(listData->info) == EQUAL)
{
tempLocation = location;
listData = listData->next; // Delete first node.
}
else
{
while (item.ComparedTo((location->next)->info) != EQUAL)
location = location->next;
// Delete node at location->next
tempLocation = location->next;
location->next = (location->next)->next;
}
delete tempLocation;
length--;
}