Nodes and Linked Lists. Check my code?

J

J Peterman

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!
 
J

J Peterman

// 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;
 
J

J Peterman

// Node.cpp

#include "Node.h"

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

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

J Peterman

// 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;

//-----------------------------------------------------------
 
J

J Peterman

// 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;
}
 
P

Phlip

#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; }
 
T

Thomas Matthews

Phlip said:
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


Use constructor notation wherever possible:

Technically, it is called an "initialization list".
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.


--
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
 
T

Thomas Matthews

J said:
// 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::eek:perator = (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::eek:perator=(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
 
K

Karl Heinz Buchegger

J said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top