Linked List question from a newb(Need code Critique)

This is a discussion on Linked List question from a newb(Need code Critique) within the C++ Programming forums, part of the General Programming Boards category; Hi! If someone has a few minutes, I could really use critique on the following code. I understand Linked Lists, ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    16

    Linked List question from a newb(Need code Critique)

    Hi!

    If someone has a few minutes, I could really use critique on the following code. I understand Linked Lists, and I believe I understand how to implement it, but my attempts in school tended to generate fatal errors, and my text is somewhat...poor at giving code examples.

    The goal of the code is to create a linked list of items with a string for a name, an ID number(techID), and 3 int parameters. I'll also need to use several methods on the data, some of which are too long for in-line definition.

    The code compiles correctly in VC++.net 2003, and seems to work properly(Read it didn't crash), but I still feel very unsure about it.

    The header file

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    class techNode
    {
    public:
    	techNode();
    	techNode(string tName, int tID, int miDmg, int maDmg, int spec,
    		techNode* theLink);
    	techNode* getLink() const ;
    	string getName() const ;
    	int getID() const;
    	vector<int> getStats() const;
    	void setData(string tName, int tID, int miDmg, int maDmg, int spec,
    				techNode* theLink);
    	void setLink(techNode* pointer);
    
    private:
    	string name;
    	int techID;
    	int minDmg;
    	int maxDmg;
    	int specials;
    	techNode* link;
    };
    typedef techNode* techNodePtr;
    
    void headInstert(techNodePtr& head, string tName, int tID, int miDmg, int maDmg,
    				 int spec);
    
    void insert(techNodePtr afterMe, string tName, int tID, int miDmg, int maDmg,
    			int spec);
    The Implementation file

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include "techNode.h"
    
    using namespace std;
    
    techNode::techNode()
    {
    }
    
    techNode::techNode(string tName, int tID, int miDmg, int maDmg, int spec, 
    				   techNode *theLink)
    {
    	name = tName;
    	techID = tID;
    	minDmg = miDmg;
    	maxDmg = maDmg;
    	specials = spec;
    	link = theLink;
    }
    
    techNode* techNode::getLink() const
    {
    	return link;
    }
    
    string techNode::getName() const
    {
    	return name;
    }
    
    int techNode::getID() const
    {
    	return techID;
    }
    
    vector<int> techNode::getStats() const
    {
    	vector<int> tempStats;
    	tempStats.push_back(minDmg);
    	tempStats.push_back(maxDmg);
    	tempStats.push_back(specials);
    
    	return tempStats;
    }
    
    void techNode::setData(string tName, int tID, int miDmg, int maDmg, int spec,
    					   techNode* theLink)
    {
    	name = tName;
    	techID = tID;
    	minDmg = miDmg;
    	maxDmg = maDmg;
    	specials = spec;
    	link = theLink;
    }
    
    void techNode::setLink(techNode* pointer)
    {
    	link = pointer;
    }
    
    void headInstert(techNodePtr& head, string tName, int tID, int miDmg, int maDmg,
    				 int spec)
    {
    	head = new techNode(tName, tID, miDmg, maDmg, spec,	head);
    }
    
    void insert(techNodePtr afterMe, string tName, int tID, int miDmg, int maDmg,
    			int spec)
    {
    	afterMe->setLink(new techNode(tName, tID, miDmg, maDmg, spec, afterMe->getLink()));
    }
    Thanks in advance if anyone could take a look at it!

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    You should use initialiser lists here:
    Code:
    techNode::techNode(string tName, int tID, int miDmg, int maDmg, int spec, 
    				   techNode *theLink) :
    	name(tName), techID(tID), minDmg(miDmg),
    	maxDmg(maDmg), specials(spec), link(theLink)
    {}
    You could perhaps pass string's by const reference?
    Oh and you have a spelling mistake on one of your function names: "headInstert".

    Other than that, it looks fine and should work okay too.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    > using namespace std;
    Never put this inside header files.
    Anyone who includes your header automatically gets the std namespace along with it otherwise, and that's simply bad news if you didn't want that.

    I would put
    string name;
    int techID;
    int minDmg;
    int maxDmg;
    int specials;
    into a separate structure/class.
    This would be the start of separating the idea of 'list' from 'data'. That is, the list shouldn't care what it is a list of. It would also shorten the parameter lists of your list data manipulation functions.

    Several places use pointers, where references would have been cleaner.

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, 06:16 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  4. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  5. linked list question
    By yeahdixon in forum C++ Programming
    Replies: 2
    Last Post: 03-28-2002, 08:16 AM

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