473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Nodes and Linked Lists. Check my code?

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
8 3033
// 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
// 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
// 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
// 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
> #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
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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: alanrn | last post by:
I am using a TreeView to display the hierarchy of a strongly-typed collection (inherited from CollectionBase). The order of the nodes in the TreeView is strictly tied to the order in which they...
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
0
by: aredo3604gif | last post by:
I have coded a serie of singly linked lists in ANSI C which I have to use. The lists are then stored in a serie of buckets with chained hash table technique. In the various lists there are nodes...
4
by: MJ | last post by:
Hi I have written a prog for reversing a linked list I have used globle pointer Can any one tell me how I can modify this prog so that I dont have to use extra pointer Head1. When I reverse a LL...
11
by: JoeC | last post by:
I have been using C++ for several years now and I have been reading about nodes. I am a little confused. I understand many of the concepts behind this such as pointers and dynamic binding. My...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
3
by: KL | last post by:
I am so confused. I am working on a project for my class, and I can't remember how to do Nodes and linked lists. I have the following bit of code but don't know how to proceed from here. If...
5
twillie
by: twillie | last post by:
Hello, I have a linked list in C++ and I am trying to remove all duplicate nodes from the list. The code below is what our prof. wrote on the board in pusdo code in class, yet it doesn't seem to...
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.