By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,725 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,725 IT Pros & Developers. It's quick & easy.

Nodes and Linked Lists. Check my code?

P: n/a
Im having a nightmare trying to understand these nodes and linked lists.

I've posted my code for my node.h, node.cpp, linkedlist.h and linkedlist.cpp
files in separates replies.

Can someone explain to me how I create a list and traverse it?

In particular, how do I make use of the node.h method GetNext()? I don;t
know what to do with it.

Thanks for any help!

Jul 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
// Node.h
#ifndef NODE_H
#define NODE_H

//-----------------------------------------------------------

#include <iostream.h>
#include <fstream.h>

//-----------------------------------------------------------

class Node
{
public:
Node () {m_next = NULL;}

~Node () {m_next = NULL;}

Node *GetNext () const {return m_next;}
void SetNext (Node *next) {m_next = next;}
void GetData (int &num) const {num = m_data;}
void SetData (int num) {m_data = num;}

private:
Node *m_next;
int m_data;

Node (const Node &node);
Node &operator = (const Node &node);
};

//-----------------------------------------------------------

#endif;
Jul 19 '05 #2

P: n/a
// Node.cpp

#include "Node.h"

Node::Node (const Node &node)
{
int num;
node.GetData(num);
SetData(num);
SetNext(node.GetNext());
}

Node &Node::operator = (const Node &node)
{
int num;
if (this != &node)
{
node.GetData(num);
SetData(num);
SetNext(node.GetNext());
}
return *this;
}
Jul 19 '05 #3

P: n/a
// LinkedList.h

//-----------------------------------------------------------

#ifndef LINKED_LIST_H
#define LINKED_LIST_H

//-----------------------------------------------------------

#include <iostream.h>
#include <fstream.h>
#include "Node.h"

//-----------------------------------------------------------

class LinkedList
{
public:
LinkedList() {}
~LinkedList() {Clear();}

void Clear();

bool AddToStart (int datum);
bool Delete (int datum);
bool Get (int index, int &num) const;

friend istream &operator >> (istream &istr, LinkedList &list);
friend ostream &operator << (ostream &ostr, const LinkedList &list);
friend ifstream &operator >> (ifstream &istr, LinkedList &list);
friend ofstream &operator << (ofstream &ostr, const LinkedList &list);

private:
Node m_start;

void Delete (Node *ptr);

LinkedList (const LinkedList &list) {};
LinkedList &operator = (const LinkedList &list) {};
};

//-----------------------------------------------------------

#endif;

//-----------------------------------------------------------
Jul 19 '05 #4

P: n/a
// LinkedList.cpp

//-----------------------------------------------------------

#include "LinkedList.h"
#include "Node.h"
#include <stdio.h>

void LinkedList::Clear()
{
m_start.SetData(NULL);

}
bool LinkedList::AddToStart(int datum)
{
Node newNode;
newNode.SetNext(NULL);
newNode.SetData(datum);

m_start.SetNext(&newNode);

return true;

}
bool LinkedList::Get(int index, int &num) const
{
Node *current;
current = m_start.GetNext();
// STUCK HERE
return true;
}
Jul 19 '05 #5

P: n/a
> #ifndef NODE_H
#define NODE_H
Indent things. Preprocessor commands are no exception:

#ifndef NODE_H
#define NODE_H

#include <iostream.h>
#include <fstream.h>
Your Node class doesn't use these. Resist including anything in a .h file
unless there's no other way. (Contrarily, <iostream> is very common, so
don't sweat about including that one too often. But use the kind without the
..h)
class Node
{
public:
Node () {m_next = NULL;}
Use constructor notation wherever possible:

Node (): m_next(NULL); {}
~Node () {m_next = NULL;}
Avoid compilable comments. If m_next owns the next node, it should delete
it.
Node *GetNext () const {return m_next;}
void SetNext (Node *next) {m_next = next;}
What happens to m_next's current contents? If it's already a node, this
dropped it.
void GetData (int &num) const {num = m_data;}
Returning an 'int' is much healthier & simpler:

int GetData() const { return m_data; }
void SetData (int num) {m_data = num;}

private:
Node *m_next;
int m_data;

Node (const Node &node);
Node &operator = (const Node &node);
};


--
Phlip
Jul 19 '05 #6

P: n/a
Phlip wrote:
#ifndef NODE_H
#define NODE_H

Indent things. Preprocessor commands are no exception:

#ifndef NODE_H
#define NODE_H


A safer convention for include guard name is to use
the extension first:
#ifndef H_NODE
#define H_NODE
since the symbols starting with "E" are reserved for the
compiler (for error reporting of library functions).
class Node
{
public:
Node () {m_next = NULL;}

Use constructor notation wherever possible:


Technically, it is called an "initialization list".

Node (): m_next(NULL); {}

~Node () {m_next = NULL;}

Avoid compilable comments. If m_next owns the next node, it should delete
it.


OP: There is no reason to assign values to members in the
destructor since the object will be destroyed an the
members will not be accessible.

--
Phlip

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #7

P: n/a
J Peterman wrote:
// Node.cpp

#include "Node.h"

Node::Node (const Node &node)
{
int num;
node.GetData(num);
SetData(num);
SetNext(node.GetNext());
}
There is no reason to use accessor function since
the parameter has the exact structure as _this_
class.

Also use the initialization list for a copy
constructor:
Node::Node(const Node & other)
: m_data(other.m_data),
m_next(other.m_next)
{
}


Node &Node::operator = (const Node &node)
{
int num;
if (this != &node)
{
node.GetData(num);
SetData(num);
SetNext(node.GetNext());
}
return *this;
}


Assignment operators are similar to copy constructors.
Thus the rationale of not using accessor methods also
applies here:
Node &
Node::operator=(const Node& other)
{
if (this != &other)
{
m_data = other.m_data;
m_next = other.m_next;
}
return *this;
}
***** Very Important *****
Instances of a class whose name is the same as
the class name except for a variation is case is
bad style. Typos and reading errors are prone to
this.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #8

P: n/a


J Peterman wrote:

Im having a nightmare trying to understand these nodes and linked lists.

I've posted my code for my node.h, node.cpp, linkedlist.h and linkedlist.cpp
files in separates replies.

Can someone explain to me how I create a list and traverse it?

In particular, how do I make use of the node.h method GetNext()? I don;t
know what to do with it.

Thanks for any help!
Please:

Post everything needed in *one*, *single* post.
In my newsreader I see at the moment 55 post. In order to help you
I have to collect the 5 originating by you. To analyze your code I constantly
have to switch between posts.

bool LinkedList::AddToStart(int datum)
{
Node newNode;
newNode.SetNext(NULL);
newNode.SetData(datum);

m_start.SetNext(&newNode);

That's not a good idea, you give the address of newNode to m_start.
But as soon as the function AddToStart returns to the caller, the
variable newNode goes out of scope and doesn't exist any longer.
Thus m_start is left with a pointer to a Node which no longer exists.

Also consider the case that m_start already points to some node. If you
set a new value to m_start, the old value is lost, and thus the Node
where m_start has pointed to previously is no longer accessible.
return true;

}


bool LinkedList::AddToStart( int datum )
{
Node* pNew = new Node;

pNew->SetNext( m_start.GetNext() );
pNew->SetData( datum );

m_start.SetNext( pNew );

return true;
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.