Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
<pre>ifndef CARGO_H
#define CARGO_H
#include "stdafx.h"
#include <iomanip>
#include <string>
#include <iostream>
#include <list>
using namespace std;
template <class T>
class LinkedList
{
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 print() const;
	void reverse(LinkedList* head);
	
};
	template <class T>
	LinkedList<T>::LinkedList() {
		head = nullptr;
	}
	template <class T>
	LinkedList<T>::LinkedList(T)
	{
		head = NULL;
		nullptr = head;
	}
	template <class T>
	LinkedList<T>::~LinkedList()
	{
		
			ListNode *nodePtr;
			ListNode *nextNode;
			nodePtr = head;

			while (nodePtr != nullptr)
			{
				nextNode = nodePtr->next;
				delete nodePtr;
				nodePtr = nextNode;
			}
		
	}
	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;
			nodePtr->next = newNode;
		}

	}
	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;
			}
		}
	}
	template <class T>
	void LinkedList<T>::print() const
	{
		ListNode *nodePtr;
		nodePtr = head;

		while (nodePtr)
		{
			cout << nodePtr->value << endl;
			nodePtr = nodePtr->next;
		}
	}

	template<class T>
	void LinkedList<T>::reverse(LinkedList* head)
	{
         reverse1(LinkedList* head)  //This is my reverse function that calls the 
                                     //other reverse function
	}
	template<class T>
  reverse1(LinkedList* head)     //This is my reverse Functions
		{
		LinkedList* ptr = NULL;
		ListNode* next;

		while (head)
		{
			next = head->next;
			head->next = ptr;
			ptr = head;
			head = next;
		}

		return ptr;
	}





C++
<pre>
#include "stdafx.h"         //This is my main function
#include "LinkedList.h"

#include "CargoShip.h"


int main()
{
	LinkedList<CargoShip> list;     // I am using a separate class to insert data
	LinkedList<CargoShip>* head = NULL;
	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);

	LinkedList<CargoShip> list2;
	
	list2.appendNode(a);
	list2.appendNode(b);
	list2.appendNode(c);
	list2.appendNode(d);
	list2.appendNode(e);
	
	list2.print();
	list2.reverse(head);
	list2.print();
	
	
    
	system("PAUSE");
    return 0;


What I have tried:

I have been working on this for a few hours now and I feel like I am really close but am making a stupid mistake. Thank You for the help!:)
Posted
Comments
Richard MacCutchan 25-Oct-17 12:47pm    
What exactly is the problem?
Member 13484972 25-Oct-17 14:20pm    
The reverse function wont print the list out in reverse.I get errors "LinkedList: use of class template requires template arguement list"
and the error
"Missing type specifier int assumed"
Richard MacCutchan 26-Oct-17 4:07am    
Why exactly are you using templates here, since the only place you use then is inside a Node object? So the Node class should use a template, and the LinkedList would no longer need them. That would simplify your code considerably.

And why do you have two reverse functions, when one should do the job?

And finally, where does this error message occur?
Member 13484972 25-Oct-17 14:24pm    
More so I do not know if my reverse function is technically right. I have been working at it for a few hours and can't seem to wrap my head around it or how to implement it in my main.cpp so it works.
Patrice T 25-Oct-17 17:54pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900