Sorted List as a Linked Structure error
Nov 14, 2013 at 7:09am UTC
I copied this code from the book by Nell Dale. "C++ Plus Data Structures"
Here is my Sorted header/implementation file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
#ifndef SORTED_H
#define SORTED_H
#include "ItemType.h"
#include <new> // std::bad_alloc
class SortedType
{
public :
SortedType();
~SortedType();
bool isFull() const ;
int GetLength() const ;
void MakeEmpty();
ItemType GetItem(ItemType item, bool & found);
void PutItem(ItemType item);
void DeleteItem(ItemType item);
void ResetList();
ItemType GetNextItem();
private :
NodeType* listData;
int length;
NodeType* currentPos;
};
struct NodeType
{
ItemType info;
NodeType* next;
};
SortedType::SortedType()
{
length = 0;
listData = NULL;
}
bool SortedType::isFull() const
{
NodeType* location;
try
{
location = new NodeType;
delete location;
return false ;
}
catch (std::bad_alloc exception)
{
return true ;
}
}
int SortedType::GetLength() const
{
return length;
}
void SortedType::MakeEmpty()
{
NodeType* tempPtr;
while (listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
length = 0;
}
ItemType SortedType::GetItem(ItemType item, bool & found)
{
bool moreToSearch;
NodeType* location;
location = listData;
found = false ;
moreToSearch = (location != NULL);
while (moreToSearch && !found)
{
switch (item.ComparedTo(location->info))
{
case GREATER: location = location->next;
moreToSearch = (location != NULL);
break ;
case EQUAL: found = true ;
item = location->info;
break ;
case LESS: moreToSearch = false ;
break ;
}
}
return item;
}
void SortedType::PutItem(ItemType item)
{
NodeType* newNode; // Pointer to node being inserted
NodeType* predLoc; // Trailing pointer
NodeType* location; // traveling pointer
bool moreToSearch;
location = listData;
predLoc = NULL;
moreToSearch = (location != NULL);
// Find insertion point.
while (moreToSearch)
{
switch (item.ComparedTo(location->info))
{
case GREATER: predLoc = location;
location = location->next;
moreToSearch = (location != NULL);
break ;
case LESS: moreToSearch = false ;
break ;
}
}
// Prepare node for insertion.
newNode = new NodeType;
newNode->info = item;
// Insert node into list.
if (predLoc == NULL) // Insert as first.
{
newNode->next = listData;
listData = newNode;
}
else
{
newNode->next = location;
predLoc->next = newNode;
}
length++;
}
void SortedType::DeleteItem(ItemType item)
{
NodeType* location = listData;
NodeType* tempLocation;
// 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 SortedType::ResetList()
// Post: currentPos has been initialized.
{
currentPos = NULL;
}
ItemType SortedType::GetNextItem()
{
ItemType item;
if (currentPos == NULL)
currentPos = listData;
item = currentPos->info;
currentPos = currentPos->next;
return item;
}
SortedType::~SortedType()
{
NodeType* tempPtr;
while (listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
}
#endif
Here is the ItemType header/implementation file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#ifndef ITEMTYPE_H
#define ITEMTYPE_H
#include <fstream>
const int MAX_ITEMS = 5;
enum RelationType {LESS, GREATER, EQUAL};
class ItemType
{
public :
ItemType();
RelationType ComparedTo(ItemType) const ;
void Print(std::ofstream&) const ;
void Initialize(int number);
private :
int value;
};
ItemType::ItemType()
{
value = 0;
}
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
if (value < otherItem.value)
return LESS;
else if (value > otherItem.value)
return GREATER;
else return EQUAL;
}
void ItemType::Initialize(int number)
{
value = number;
}
void ItemType::Print(std::ofstream& out) const
// Pre: out has been opeend.
// Post: value has been sent to the stream out.
{
out << value << " " ;
}
#endif
The error is at line 145 in sorted file. Both item and location are underlined in red. For item it says expression must have pointer type. For location it says "no suitable constructor exists to convert from "NodeType*" to "ItemType".
Any ideas why?
Last edited on Nov 14, 2013 at 7:11am UTC
Nov 14, 2013 at 8:45am UTC
The
)
is at the wrong place:
1 2
while ((item.ComparedTo(location->next)->info) ) != EQUAL)
location = location->next;
By the way: the loop will certainly lead to a crash. What happens when
location
or
location->next
is NULL before
EQUAL
?
Topic archived. No new replies allowed.