Thread: Help with Linked List

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    39

    Help with Linked List

    Just started learning Linked List and I am pretty lost. I have started a class but not sure it it is correct. Just need some feed back on if the class is setup right and any additional things about linked list. Every site I have looked at so far has pretty much did and said the same thing and its not helping any. I understand the concept it is just turning it into code.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #include "swcString.h" //Custom string class
    
    class objList
    {
    public:
    	objList();
    	~objList();
    	
    	objList operator+(objList);
    
    private:
    	swcString objName;
    	swcString objMaterial;
    	int objWeight;
    	int objMass;
    };
    
    class objNode
    {
    public:
    	objNode();
    	objNode(objNode &);
    	~objNode();
    	void addNewObj();
    	void delObj();
    	bool emptyObj();
    	void traverseObj();
    	objList * searchObj();
    private:
    	objList object;
    	objNode *start;
    	objNode *next;
    	objNode *previos;
    	objNode *end;
    };
    
    int main (void)
    {
    	return 0;
    }
    also when trying to do
    Code:
    objNode::objNode()
    {
    }
    i get this error:
    Code:
    ompiling...
    main.cpp
    Linking...
    main.obj : error LNK2001: unresolved external symbol "public: __thiscall objList::objList(void)" (??0objList@@QAE@XZ)
    Debug/List.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    
    List.exe - 2 error(s), 0 warning(s
    It is not reconizing my constructor in the class for some reason
    Last edited by Bitphire; 03-10-2005 at 07:05 AM.

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Where and how did you define your objList constructor?. When trying to create object objNode objList constructor is called so think about it a little

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    39
    LOL, thanks I never thought about that. I had not defined the other class yet. Gave it a shot and then I got a different error this time. It's a fatal error: INTERNAL COMPILER ERROR. I am using Visual C++ 6. Here is what i have:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #include "swcString.h"
    
    class objList
    {
    public:
    	objList();
    //	~objList();
    	
    	objList operator+(objList);
    
    private:
    	swcString objName;
    	swcString objMaterial;
    	int objWeight;
    	int objMass;
    };
    
    class objNode
    {
    public:
    	objNode();
    	objNode(objNode &);
    //	~objNode();
    	void addNewObj();
    	void delObj();
    	bool emptyObj();
    	void traverseObj();
    	objList * searchObj();
    private:
    	objList object;
    	objNode *start;
    	objNode *next;
    	objNode *previos;
    	objNode *end;
    };
    
    int main (void)
    {
    	return 0;
    }
    objList::objList
    :objName(0), objMaterial(0), objWeight(0), objMass(0)
    {
    }
    
    objList operator+(objList)
    {
    
    }
    
    objNode::objNode()
    {
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Just a couple of design issues/comments. You have an objList class that seems to represent a node of a linked list and an objNode class that seems to represent the list. Just going by the names of these objects I would say the possibility for confusion is very high. Also, it does not seem that your objNode class (actually the list) should have the next and previous pointers, they should be a part of the objList class (actually the nodes). My setup would look like this:

    Code:
    // The node class contains the pointers to the next and previous nodes
    class objNode
    {
        swcString objName;
        swcString objMaterial;
        int objWeight;
        int objMass;
        objNode *next;
        objNode *previous;
    public:
        ...
    };
    
    
    // The list class should include pointers to the first and last nodes
    // It does not contain an actual node element like your original design
    // and it also does not contain the next and previous pointers
    class objList
    {
        objNode *start;
        objNode *end;
    public:
        ...
    };
    Just some suggestions...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    39
    Thanks, that is something I was looking for. Just a basic design of my classes. Now that you have shed some light maybe i can this thing started Thanks again

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Just started learning Linked List and I am pretty lost.
    I think the best way to understand them is to draw pictures. I'm sure some of the things you have read have the pictures of linked lists, but you need to draw out a picture for yourself to understand them. Use a pencil and as you add Nodes to your picture draw arrows to show the new links. Here is an example:

    http://cboard.cprogramming.com/showt...ht=linked+list

    which shows the basic structure of a linked list drawing: a List container object with Node objects inside it.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    39
    Thanks for all the replies. Sorry its been so long since I posted. Things came up but i'm back. Had class tonight and got this error and even the teacher wasn't to sure on why. So I am hoping you guys can help. Thanks.

    Error:

    Code:
    --------------------Configuration: LinkedList - Win32 Debug--------------------
    Compiling...
    objList.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\objList.cpp(55) : error C2440: '=' : cannot convert from 'class objList::objNode *' to 'class objNode *'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    Error executing cl.exe.
    
    LinkedList.exe - 1 error(s), 0 warning(s)
    Here is my code that I have:

    Code:
    #include <iostream>
    
    using namespace std;
    
    #include "swcString.h"
    
    class objNode;
    class objList;
    
    class objList
    {
    public:
    	objList();
    	void addObj(swcString nameOfObj, swcString materialOfObj, int weightOfObj, int massOfObj);
    
    private:
    	objList::objNode *start;
    	objNode *end;
    
    	class objNode
    	{
    	public:
    		swcString objName;
    		swcString objMaterial;
    		int objWeight;
    		int objMass;
    		objNode *next;
    		objNode *previous;
    	};
    
    	objNode myNode;
    };
    
    int main(void)
    {
    	cout << "Hello World!" << endl;
    	return 0;
    }
    
    objList::objList()
    :start(NULL), end(NULL)
    {
    }
    
    void objList::addObj(swcString nameOfObj, swcString materialOfObj, int weightOfObj, int massOfObj)
    {
    	objNode *newNode = new objList::objNode;
    	
    	newNode->objName = nameOfObj;
    	newNode->objMaterial = materialOfObj;
    	newNode->objWeight = weightOfObj;
    	newNode->objMass = massOfObj;
    
    	newNode->next = NULL;
    	start = newNode;
    }

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    As a start:

    Code:
    #include <iostream>
    
    using namespace std;
    
    #include "swcString.h"
    
    class objNode
    {
    public:
        swcString objName;
        swcString objMaterial;
        int objWeight;
        int objMass;
        objNode *next;
        objNode *previous;
    };
    
    class objList
    {
    public:
        objList();
        void addObj(swcString, swcString, int, int);
    
    private:
        objNode *start;
        objNode *end;
    };
    
    objList::objList() : start(NULL), end(NULL)
    {
    }
    
    void objList::addObj(swcString nameOfObj, swcString materialOfObj, int weightOfObj, int massOfObj)
    {
        objNode *newNode = new objNode;
    	
        newNode->objName = nameOfObj;
        newNode->objMaterial = materialOfObj;
        newNode->objWeight = weightOfObj;
        newNode->objMass = massOfObj;
    
        newNode->next = NULL;
        start = newNode;
    }
    
    int main(void)
    {
        cout << "Hello World!" << endl;
        return 0;
    }
    Your addObj function only works for adding the first node and you are also forgetting to set the new node's previous pointer (an approriate objNode constructor could make things like this a bit simpler). You will have to deal with the end pointer as well. Repeated calls to this function would currently result in a memory leak as it stands now.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM