Write your question here.
I'm writing code for a singly linked list for my data structures class and I can't figure out what I am doing wrong. Any help is much appreciated!
My teacher wants me to write all of the code in my LinkedList.h file with a LinkedListNode class nested in the LinkedList class.
Please let me know if you have any questions on what my functions are supposed to do or what my logic was in what I'm doing. I think I'm using the template wrong, but I'm not sure. Thanks again in advance for the help.
#pragma once
template <typename type>
class LinkedList
{
private:
LinkedListNode *head;
int count;
public:
template <typename type>
class <type>LinkedListNode
{
private:
type item;
LinkedListNode *next;
public:
<type>LinkedListeNode(const type& newItem)
{
item = newItem;
next = NULL;
}
};
LinkedList()
{
head = NULL;
count = 0;
}
~LinkedList();
/*
insertHead
A node with the given value should be inserted at the beginning of the list.
DO NOT ALLOW DUPLICATE VALUES IN THE LIST. If there is a duplicate item
then throw an exception.
*/
virtualvoid insertHead(type value)
{
LinkedListNode* existing = find(value);
if (existing == NULL)
{
LinkedListNode *n = new LinkedListNode(value);
n->next = head;
head = n;
count++;
delete existing;
}
else
{
delete existing;
throw exception("Duplicate Item!")
}
}
/*
insertTail
A node with the given value should be inserted at the end of the list.
DO NOT ALLOW DUPLICATE VALUES IN THE LIST. If there is a duplicate item
then throw an exception.
*/
virtualvoid insertTail(type value)
{
LinkedListNode* existing = find(value);
if (existing == NULL)
{
LinkedListNode *tail = head;
LinkedListNode *n = new LinkedListNode(value);
while (tail != NULL)
{
tail = tail->next;
}
tail->next = n;
count++;
delete tail;
delete existing;
}
else
{
delete existing;
throw exception("LinkedListNode already exists");
}
}
/*
insertAfter
Insert newValue immediately after the first occurrence of
searchValue (i.e., as you move through the list from head
to tail).
If no match is found, do not add the item to the list. Do
not throw an exception.
*/
virtualvoid insertAfter(type searchValue, type newValue)
{
LinkedListNode* existing = find(searchValue);
if (existing == NULL)
{
delete existing;
throw exception("invalid LinkedListNode");
}
else
{
LinkedListNode *n = new LinkedListNode(newValue);
n->next = existing->next;
existing->next = n;
count++;
delete exsisting;
}
}
/*
remove
The node with the given value should be removed from the list.
If there are two nodes with the same value, only remove the
first one.
The list may or may not include a node with the given value.
*/
virtualvoid remove(type searchValue)
{
LinkedListNode* existing = find(searchValue);
if (existing == NULL)
{
delete existing;
Throw exception("LinkedListNode does not exist.");
}
else
{
LinkedListNode *temp = head;
while (temp->next != existing)
{
temp = temp->next;
}
temp->next = existing->next;
delete existing;
delete temp;
count--;
}
}
/*
clear
Remove all nodes from the list.
*/
virtualvoid clear()
{
while (head != NULL)
{
LinkedListNode *temp = head;
head = temp->next;
delete temp;
}
count = 0;
}
/*
at
Returns the value of the node at the given index. The list begins at
index 0.
If the given index is out of range of the list, throw an out of range exception.
*/
virtual type at(int index)
{
if (index > size() || index < 0)
throw exception("Out of Range.");
elseif (index == 0)
return head->item;
else
{
LinkedListNode *search = head;
type tempItem;
for (int i = 0; i < index; i++)
{
search = search->next;
}
tempItem = search->item;
delete search;
return tempItem;
}
}
/*
size
Returns the number of nodes in the list.
*/
virtualint size()
{
LinkedListNode *temp = head;
int length = 0;
while (temp != NULL)
{
length++;
temp = temp->next;
}
count = length;
return length;
}
LinkedListNode* find(type node)
{
if (head == NULL)
return NULL;
LinkedListNode *temp = head;
while (temp != NULL)
{
if (temp->item == node)
return temp;
temp = temp->next;
}
delete temp;
return NULL;
}
};