I am not sure why but my linked list template only returns Eminem in the output window. I need it to output all of the Cargo Ship names. I have been stuck for a few hours now and decided to get some help.
//This is my Header file for my LinkedList template
#ifndef CARGO_H
#define CARGO_H
#include "stdafx.h"
#include <iomanip>
#include <string>
#include <iostream>
#include <list>
using namespace std;
My Template
template <class T>
class LinkedList
{
The start of my Linked List
private:
struct ListNode
{
T value;
struct ListNode *next;
};
ListNode *head;
public:
LinkedList();
~LinkedList();
LinkedList(T);
void appendNode(T newValue);
void insertNode(T);
void deleteNode(T);
void printShips() const;
void showReverse(T *nodePtr) const;
};
template <class T>
LinkedList<T>::LinkedList() {
head = nullptr;
}
template <class T>
LinkedList<T>::LinkedList(T)
{
nullptr = head;
}
template <class T>
LinkedList<T>::~LinkedList()
{
ListNode *nodePtr;
ListNode *nextNode;
nodePtr = head;
while (nodePtr != nullptr)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
I think the problem is in my appendNode() function but I am not sure.
template <class T>
void LinkedList<T>::appendNode(T newValue)
{
ListNode *newNode;
ListNode *nodePtr;
newNode = new ListNode;
newNode->value = newValue;
newNode->next = nullptr;
if (!head)
head = newNode;
else
{
nodePtr = head;
while (nodePtr->next)
nodePtr = nodePtr->next;
}
}
template <class T>
void LinkedList<T>::insertNode(T newValue)
{
ListNode *newNode;
ListNode *nodePtr;
ListNode *previousNode = nullptr;
newNode = new ListNode;
newNode->value = num;
if (!head)
{
head = newNode;
newNode->next = nullPtr;
}
else
{
nodePtr = head;
previousNode = nullPtr
while (nodePtr != nullptr && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (previousNode == nullptr)
{
head = newNode;
newNode->next = nodePtr;
}
else
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
template <class T>
void LinkedList<T>::deleteNode(T searchValue)
{
ListNode *nodePtr;
ListNode *previousNode;
if (!head)
return;
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
nodePtr = head;
while (nodePtr != nullptr && nodePtr->value != num)
{
previouseNode = nodePtr;
nodePtr = nodePtr->next;
}
if (nodePtr)
{
previousNode->next = nodePtr->next
delete nodePtr;
}
}
}
I think the problem could also be with my printShips() Function
template <class T>
void LinkedList<T>::printShips() const
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
template <class T>
void LinkedList<T>::showReverse(T *nodePtr) const
{
if (nodePtr != NULL)
{
showReverse(nodePtr->next);
cout << nodePtr->value << " ";
}
}
#endif
//This is my main.cpp
#include "stdafx.h"
#include "LinkedList.h"
#include "CargoShip.h"
int main()
{
LinkedList<CargoShip> list;
CargoShip a(1997, "Eminem");
CargoShip b(2017, "Dr Dre");
CargoShip c(2015, "SnoopDogg");
CargoShip d(1900, "NWA");
CargoShip e(1500, "50 Cent");
list.appendNode(a);
list.appendNode(b);
list.appendNode(c);
list.appendNode(d);
list.appendNode(e);
list.printShips();
system("PAUSE");
return 0;
}
//It only prints Eminem out. It should print all of the names of the
CargoShips
Aucun commentaire:
Enregistrer un commentaire