This is the easiest way, assuming that your compiler isn't an old cheap one that schools seem to love and never conforms to the current standard. But before I post the code, two things:
1) Don't use void main, it's wrong.
2) You really don't want to explicitly call the destructor of your class. Bad things can happen. I didn't write a destroyList method for you, that's your job. Just remember to remove the destructor call.
3) Please for the love of god use the code tags when your source is more than 10 lines. I could have fixed this in less than a minute, but it took me considerably longer because I had to reformat everything.
4) I didn't change the interface at all, just the processing. Once again that is your job.
Okay, four things, but you know what they say about mathematicians. There are three kinds, those who can count and those who can't. Good thing I'm a programmer.
Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
template <class T>
class LinkedList
{
private:
struct ListNode
{
string value;
struct ListNode *next;
};
ListNode *head;
public:
LinkedList(void)
{ head = NULL; }
~LinkedList(void);
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList(void);
};
template <class T>
void LinkedList<T>::appendNode(T num)
{
ListNode *newNode, *nodePtr;
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
if (!head)
head = newNode;
else
{
nodePtr = head;
while (nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
template <class T>
void LinkedList<T>::displayList()
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
template <class T>
void LinkedList<T>::insertNode(T num)
{
ListNode *newNode, *nodePtr, *previousNode;
newNode = new ListNode;
newNode->value = num;
if (!head) {
head = newNode;
newNode->next = NULL;
}
else{
nodePtr = head;
while (nodePtr != NULL && nodePtr->value < num) {
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (previousNode == NULL) {
head = newNode;
newNode->next = nodePtr;
}
else {
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
template <class T>
void LinkedList<T>::deleteNode(T num)
{
ListNode *nodePtr, *previousNode;
if (!head)
return;
if (head->value == num) {
nodePtr = head->next;
delete head;
head = nodePtr;
}
else {
nodePtr = head;
while (nodePtr != NULL && nodePtr->value != num) {
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
template <class T>
LinkedList<T>::~LinkedList()
{
ListNode *nodePtr, *nextNode;
nodePtr = head;
while (nodePtr != NULL) {
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
int main(void)
{
LinkedList<string> List;
int Response = -1;
string Value;
cout<<"What do you want to do"<<endl;
while(Response != 1) {
cout<<"(1) Exit"<<endl;
cout<<"(2) Append a node"<<endl;
cout<<"(3) Insert a node"<<endl;
cout<<"(4) Delete a node"<<endl;
cout<<"(5) Delete the list"<<endl;
cout<<"(6) Display the list"<<endl;
cin>>Response;
switch(Response)
{
case 1:
cout<<"Thanks for visiting. Bye."<<endl;
break;
case 2:
cout<<"What value do you wish to append?"<<endl;
cin>>Value;
List.appendNode(Value);
cout<<"Here is the list"<<endl;
List.displayList();
break;
case 3:
cout<<"What value do you wish to insert?"<<endl;
cin>>Value;
List.insertNode(Value);
cout<<"Here is the list"<<endl;
List.displayList();
break;
case 4:
cout<<"What value do you wish to delete?"<<endl;
cin>>Value;
List.deleteNode(Value);
cout<<"Here is the list"<<endl;
List.displayList();
break;
case 5:
cout<<"Are you sure you want to delete the list(Y for yes)?"<<endl;
char Answer;
cin>>Answer;
if(Answer=='Y'||Answer=='y') {
List.~LinkedList();
}
else {
cout<<"Here is the list"<<endl;
List.displayList();
}
break;
case 6:
cout<<"Here is the list"<<endl;
List.displayList();
break;
default:
cout<<"Please enter a value 1 - 6"<<endl;
}
}
cin.get();
return 0;
}
-Prelude