Linked list problem

This is a discussion on Linked list problem within the C++ Programming forums, part of the General Programming Boards category; I'm having a little problem with my linked list. What am I doing wrong? Here's the source Code: #include <string> ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    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
    Last edited by Psycho; 05-18-2002 at 01:38 PM.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    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.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    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.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    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.
    Last edited by Psycho; 05-18-2002 at 04:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 07:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21