-
Linked list problem
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
-
I think
Code:
if(!head)
{
newNode = head;
newNode->next = NULL;
}
should be
Code:
if(!head)
{
newNode->next = NULL;
head = newNode;
}
I haven't read past this so there could be more wrong.
-
woo! it works, thanks :)
it gets annoying when I mess a little thing up like that and I spend so much time on something that's so simple.
-
Sorry to bug you guys again, but I'm getting an error at random times when inputting values in my linked list
ERROR:
Run-Time Check Failure #3 - The variable 'previousNode' is being used without being defined.
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)
{
head = newNode;
newNode->next = NULL;
}
else
{
nodeMove = head;
while (nodeMove != NULL && nodeMove->value < item)
{
previousNode = nodeMove;
nodeMove = nodeMove->next;
}
if (previousNode == NULL) // this is the first place the debugger says the above mentioned error
{
head = newNode;
newNode->next = nodeMove;
}
else
{
previousNode->next = newNode; //heres the other place the debugger says the same error. :(
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;
}
}
when running the program I get this error at random times. Sometimes after the user puts in 10 values sometimes when the user puts in 3 values etc. dunno, its weird and I have no clue what I'm doing wrong.