I'm having a little problem with my linked list. What am I doing wrong?
Here's the source
Code:
#include <string>
#include <iostream>
using namespace std;
class Inventory
{
private:
typedef struct node
{
struct node* next;
string value;
};
node *head;
public:
Inventory()
{
head=NULL;
}
void PickUp(string item)
{
node* newNode, *nodeMove, *previousNode;
newNode = new node;
newNode->value = item;
if(!head)
{
newNode = head;
newNode->next = NULL; //the debugger says this is the line that messes up.
}
else
{
nodeMove = head;
while (nodeMove != NULL && nodeMove->value < item)
{
previousNode = nodeMove;
nodeMove = nodeMove->next;
}
if (previousNode == NULL)
{
head = newNode;
newNode->next = nodeMove;
}
else
{
previousNode->next = newNode;
newNode->next = nodeMove;
}
}
}
void DisplayInv()
{
node *nodeMove;
nodeMove = head;
while (nodeMove)
{
cout << nodeMove->value << endl;
nodeMove = nodeMove->next;
}
}
~Inventory()
{
node *nodeMove, *nextNode;
nodeMove = head;
while (nodeMove != NULL)
{
nextNode = nodeMove->next;
delete nodeMove;
nodeMove = nextNode;
}
}
};
void main()
{
int choice = 0;
string Value;
Inventory List;
cout << "Test v1.0" << endl;
cout << "1. Add item." << endl;
cout << "2. Exit." << endl;
cin >> choice;
switch(choice)
{
case 1: cout << "Type in the item you want to pick up." << endl;
cin >> Value;
List.PickUp(Value);
List.DisplayInv();
break;
case 2: List.~Inventory();
break;
}
}
I get an application error that says "The memory could not be 'written' "
Thanks in advance
-Psycho