Hello, so I had to overload virtual functions that work with linked lists so that they accept vectors as parameters. I did that but when I tried to print the values it showed a long negative number. So, if you know, can you tell me why is it printing this instead of the values? thank you.
These are the functions in the derived class:
template <class Type>
void unorderedLinkedList<Type>::insertFirst(const vector<Type>& newItem)
{
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the new node
newNode->info2.push_back(newItem.capacity()); //store the new item in the node
newNode->link = first; //insert newNode before first
first = newNode; //make first point to the
//actual first node
count++; //increment count
if (last == nullptr) //if the list was empty, newNode is also
//the last node in the list
last = newNode;
}//end insertFirst
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the new node
newNode->info2.push_back(newItem.capacity()); //store the new item in the node
newNode->link = nullptr; //set the link field of newNode
//to nullptr
if (first == nullptr) //if the list is empty, newNode is
//both the first and last node
{
first = newNode;
last = newNode;
count++; //increment count
}
else //the list is not empty, insert newNode after last
{
last->link = newNode; //insert newNode after last
last = newNode; //make last point to the actual
//last node in the list
count++; //increment count
}
}//end insertLast
template <class Type>
void unorderedLinkedList<Type>::deleteNode(const vector<Type>& deleteItem)
{
unsigned int index = 0;
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
try
{
if (first == nullptr); //Case 1; the list is empty.
}
catch (nodeType<Type> first)
{
cout << "Cannot delete from an empty list." << endl;
}
if (first->info == deleteItem[index]) //Case 2
{
current = first;
first = first->link;
count--;
if (first == nullptr) //the list has only one node
last = nullptr;
delete current;
}
else //search the list for the node with the given info
{
found = false;
trailCurrent = first; //set trailCurrent to point
//to the first node
current = first->link; //set current to point to
//the second node
while (current != nullptr && !found)
{
if (current->info != deleteItem[index])
{
trailCurrent = current;
current = current->link;
}
else
found = true;
}//end while
try
{
if (found) //Case 3; if found, delete the node
{
trailCurrent->link = current->link;
count--;
if (last == current) //node to be deleted
//was the last node
last = trailCurrent; //update the value
//of last
delete current; //delete the node from the list
}
}
catch (nodeType<Type> first)
{
cout << "The item to be deleted is not in " << "the list." << endl;
}
}//end else
}//end deleteNode
This is the print function in the virtual class:
template <class Type>
void linkedListType<Type>::print() const
{
nodeType<Type> *current; //pointer to traverse the list
current = first; //set current so that it points to
//the first node
while (current != nullptr) //while more data to print
{
cout << current->info << " ";
current = current->link;
}
}//end print