C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with linked list ..

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Andrew Skouloudis
Guest





PostPosted: Sun Dec 28, 2003 9:04 pm    Post subject: Problem with linked list .. Reply with quote



Hello people and merry christmas. I am trying to create a simple
linked list and it seems i am
doing something wrong with the DeleteNode function ...

#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include "GenericStringClass2.h"
#include <string.h>


#define DEFAULT 0

class MACListPart
{
public:
// constructor
MACListPart();
~MACListPart();
MACListPart *GetNextPointer();


private:
friend class MACList;
String macAdr;
int data;
MACListPart *next;
};


class MACList
{
public:
//constructors
MACList();
~MACList();

//accessors
void PrintList(void);
void AddNode(const String& mac);
void DeleteNode(const String& mac);
bool FindNode(const String&);
// void DeleteList();
bool IsListEmpty();
int GetCount(void);

protected:
MACListPart *head;
MACListPart *current;

private:
int itsCount;
};

MACListPart::MACListPart(void)
{
next=NULL;
data=0;

// macAdr=..
}


MACListPart::~MACListPart(void)
{
cout << "Calling the default destructor for MACListPart ...
n";
if(this != NULL)
{

delete this;
}
else delete this;
}

MACListPart* MACListPart::GetNextPointer(void)
{
if(this==NULL)
{
cout << "You are pointing to nowhere , where do you
think you are going ??n";
return NULL;
}
else
{
return this->next;
}
}


MACList::MACList(void)
{
head=NULL;
current=NULL;
itsCount=0;
}


MACList::~MACList(void)
{/*
if(IsListEmpy()==true)
{
//do nothing ..
}
else
{
DeleteList();
}*/
}

bool MACList::IsListEmpty(void)
{
if(head==NULL)
{
return true;
}

//else ....
return false;
}


bool MACList::FindNode(const String& mac)
{
MACListPart *pMacListPart=head;
while(pMacListPart !=NULL)
{
if(pMacListPart->macAdr==mac)
{
return true;
}
}
return false;
}


int MACList::GetCount(void)
{
return itsCount;
}


void MACList::AddNode(const String& mac)
{
MACListPart *pMACListPart = new MACListPart;
pMACListPart->macAdr=mac;
pMACListPart->data=DEFAULT;
pMACListPart->next=NULL;

if(IsListEmpty() == true)
{
head=pMACListPart;
current=head;
}
else // (if IsListEmpty() == false )
{
current->next=pMACListPart;
current=pMACListPart;
}
// increasing the elements of the list
itsCount++;
}




void MACList::PrintList()
{
MACListPart *temp = head;
while( temp != NULL)
{
cout << temp->macAdr.GetString() << endl;
temp=temp->next;
}
}


void MACList::DeleteNode(const String& mac)
{
char c;
// If the first element of the list is going to be deleted:
if(head->macAdr==mac)
{
MACListPart *tempNode;
tempNode=head->next;
delete head;
head=tempNode;
}
else
{
MACListPart *prevNode,*currNode;
prevNode=head;
currNode=head->next;
while(currNode != NULL)
{
if(currNode->macAdr==mac)
{
MACListPart *tempNode;
tempNode=currNode->next;
prevNode->next=tempNode;
printf("YAHOOOOn");
this->PrintList();
c=getchar();
delete currNode;

}
else // you didn't find the string in currNode
so ...
{
prevNode=currNode;
currNode=currNode->next;
}
}
}
}


int main(void)
{
MACList MACListIstance;
MACListIstance.AddNode("11-11-11-11-11-11");
MACListIstance.AddNode("22-22-22-22-22-22");
MACListIstance.AddNode("33-33-33-33-33-33");
MACListIstance.AddNode("44-44-44-44-44-44");
MACListIstance.PrintList();
bool t = MACListIstance.FindNode("11-11-11-11-11-11");
MACListIstance.DeleteNode("22-22-22-22-22-22");
MACListIstance.PrintList();
if (t==true) cout << "This MAC address exists into the
database ... n";
else cout << "This MAC address does not exist in the
database.... n";
cout << "Hello world ... the list currently has " <<
MACListIstance.GetCount() << " elements n";
return 0;
}


The problem is when i am trying to delete a node .. The program hangs
after the command
delete currNode is executed ... The String class is declared in
another file and here is the
default destructor :


String::~String()
{
cout << "Calling the default destructor of String ..n";
delete [] itsString;
itsLen=0;
cout <<"Default destructor of String finished ... n";
}


Can you help ?? I am using Visual C++

(Sorry for my english ... )
Back to top
Brian MacBride
Guest





PostPosted: Sun Dec 28, 2003 10:22 pm    Post subject: Re: Problem with linked list .. Reply with quote




"Andrew Skouloudis" <afterskoulNOSPAMPLEASE (AT) yahoo (DOT) com> wrote

Quote:
Hello people and merry christmas. I am trying to create a simple
linked list and it seems i am
doing something wrong with the DeleteNode function ...


class MACListPart
{
public:
// constructor
MACListPart();
~MACListPart();
MACListPart *GetNextPointer();


private:
friend class MACList;
String macAdr;
int data;
MACListPart *next;
};



MACListPart::~MACListPart(void)
{
cout << "Calling the default destructor for MACListPart ...
n";

/*

Quote:
if(this != NULL)
{

delete this;
}
else delete this;

*/

Quote:
}


You don't need to write destructor for MACListPart. If you do... you don't
want to do the suicide thing...

Remember to decrement itsCount in the

void MACList::DeleteNode (const String &mac) function and you'll get...

11-11-11-11-11-11
22-22-22-22-22-22
33-33-33-33-33-33
44-44-44-44-44-44
YAHOOOO
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44
This MAC address exists into the database ...
Hello world ... the list currently has 3 elements

Is that what you wanted?

Quote:

Can you help ?? I am using Visual C++

(Sorry for my english ... )

Regards

Brian



Back to top
Andrew Skouloudis
Guest





PostPosted: Mon Dec 29, 2003 8:34 am    Post subject: Re: Problem with linked list .. Reply with quote



On Sun, 28 Dec 2003 12:22:14 -1000, "Brian MacBride"
<macbride (AT) ix (DOT) netcom.com> wrote:

Quote:

"Andrew Skouloudis" <afterskoulNOSPAMPLEASE (AT) yahoo (DOT) com> wrote in message
news:o8huuv4au17jsl8mum1bpbd2qjker3med6 (AT) 4ax (DOT) com...
Hello people and merry christmas. I am trying to create a simple
linked list and it seems i am
doing something wrong with the DeleteNode function ...


class MACListPart
{
public:
// constructor
MACListPart();
~MACListPart();
MACListPart *GetNextPointer();


private:
friend class MACList;
String macAdr;
int data;
MACListPart *next;
};



MACListPart::~MACListPart(void)
{
cout << "Calling the default destructor for MACListPart ...
n";

/*

if(this != NULL)
{

delete this;
}
else delete this;

*/

}


You don't need to write destructor for MACListPart. If you do... you don't
want to do the suicide thing...

Remember to decrement itsCount in the

void MACList::DeleteNode (const String &mac) function and you'll get...

11-11-11-11-11-11
22-22-22-22-22-22
33-33-33-33-33-33
44-44-44-44-44-44
YAHOOOO
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44
This MAC address exists into the database ...
Hello world ... the list currently has 3 elements

Is that what you wanted?


Can you help ?? I am using Visual C++

(Sorry for my english ... )

Regards

Brian

First of all thank you for your answer

Secondly .. how did you do that ??

I did put

if(this != NULL)
{

delete this;
}
else delete this;

in comments and still the program hangs !! Can you give me the
destructor of your String class ????
I even removed the destructor definition in the MACListPart , so the
compiler should do whatever it want to, when deleting the node .
But still nothing !!! .

This is the output of my program :

Calling the default destructor of String ..
Default destructor of called ...
Calling the default destructor of String ..
Default destructor of called ...
Calling the default destructor of String ..
Default destructor of called ...
Calling the default destructor of String ..
Default destructor of called ...
11-11-11-11-11-11
22-22-22-22-22-22
33-33-33-33-33-33
44-44-44-44-44-44
YAHOOOO
Calling the default destructor of String ..
Default destructor of called ...
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44 // And now hangs !!!

Back to top
Brian MacBride
Guest





PostPosted: Mon Dec 29, 2003 8:58 am    Post subject: Re: Problem with linked list .. Reply with quote


"Andrew Skouloudis" <afterskoulNOSPAMPLEASE (AT) yahoo (DOT) com> wrote

Quote:
On Sun, 28 Dec 2003 12:22:14 -1000, "Brian MacBride"
[email]macbride (AT) ix (DOT) netcom.com[/email]> wrote:


"Andrew Skouloudis" <afterskoulNOSPAMPLEASE (AT) yahoo (DOT) com> wrote in message
news:o8huuv4au17jsl8mum1bpbd2qjker3med6 (AT) 4ax (DOT) com...
Hello people and merry christmas. I am trying to create a simple
linked list and it seems i am
doing something wrong with the DeleteNode function ...


class MACListPart
{
public:
// constructor
MACListPart();
~MACListPart();
MACListPart *GetNextPointer();


private:
friend class MACList;
String macAdr;
int data;
MACListPart *next;
};



MACListPart::~MACListPart(void)
{
cout << "Calling the default destructor for MACListPart ...
n";

/*

if(this != NULL)
{

delete this;
}
else delete this;

*/

}


You don't need to write destructor for MACListPart. If you do... you
don't
want to do the suicide thing...

Remember to decrement itsCount in the

void MACList::DeleteNode (const String &mac) function and you'll get...

11-11-11-11-11-11
22-22-22-22-22-22
33-33-33-33-33-33
44-44-44-44-44-44
YAHOOOO
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44
This MAC address exists into the database ...
Hello world ... the list currently has 3 elements

Is that what you wanted?


Can you help ?? I am using Visual C++

(Sorry for my english ... )

Regards

Brian

First of all thank you for your answer
Secondly .. how did you do that ??

I did put

if(this != NULL)
{

delete this;
}
else delete this;

in comments and still the program hangs !! Can you give me the
destructor of your String class ????
I even removed the destructor definition in the MACListPart , so the
compiler should do whatever it want to, when deleting the node .
But still nothing !!! .

This is the output of my program :

Calling the default destructor of String ..
Default destructor of called ...
Calling the default destructor of String ..
Default destructor of called ...
Calling the default destructor of String ..
Default destructor of called ...
Calling the default destructor of String ..
Default destructor of called ...
11-11-11-11-11-11
22-22-22-22-22-22
33-33-33-33-33-33
44-44-44-44-44-44
YAHOOOO
Calling the default destructor of String ..
Default destructor of called ...
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44 // And now hangs !!!

You didn't forget that you have this silly....

c = getchar ();

....here. Lose it...

Regards

Brian




Back to top
Andrew Skouloudis
Guest





PostPosted: Mon Dec 29, 2003 11:00 am    Post subject: Re: Problem with linked list .. Reply with quote


Quote:

You didn't forget that you have this silly....

c = getchar ();

...here. Lose it...

Regards

Brian



I am not THAT stupid ... When I i say it hangs i mean that the
execution of the program stops and Windows XP creates a window
which says that " LinkedList3.exe has encourted a problem and needs
to close . We are sorry for the inconvenience " blah-blah .. Debug ,
Send Report , Don't Send ". The problem is with the delete operation .
Did you run this program with VC++ ??? . What's your String destructor
???

Back to top
Michael Mellor
Guest





PostPosted: Mon Dec 29, 2003 1:16 pm    Post subject: Re: Problem with linked list .. Reply with quote

Andrew Skouloudis wrote:
Quote:
Hello people and merry christmas. I am trying to create a simple
linked list and it seems i am
doing something wrong with the DeleteNode function ...

#include <malloc.h
#include #include #include iostream.h is not standard so use:

#include and for the time being
using namespace std;

Quote:
#include "GenericStringClass2.h"
#include <string.h


#define DEFAULT 0

class MACListPart
{
public:
// constructor
MACListPart();
~MACListPart();
MACListPart *GetNextPointer();


private:
friend class MACList;
String macAdr;
int data;
MACListPart *next;
};


class MACList
{
public:
//constructors
MACList();
~MACList();

//accessors
void PrintList(void);
void AddNode(const String& mac);
void DeleteNode(const String& mac);
bool FindNode(const String&);
// void DeleteList();
bool IsListEmpty();
int GetCount(void);

protected:
MACListPart *head;
MACListPart *current;

private:
int itsCount;
};

MACListPart::MACListPart(void)
{
next=NULL;
data=0;

// macAdr=..
}


MACListPart::~MACListPart(void)
{
cout << "Calling the default destructor for MACListPart ...
n";
if(this != NULL)
{

delete this;
}
else delete this;
This is quite a strange piece of code as it is equivalent to "delete

this". It is not needed.
Quote:
}

MACListPart* MACListPart::GetNextPointer(void)
{
if(this==NULL)

If this is NULL you have problems, therefore this check is completely
unneeded. Do the NULL check where you use this method.

Quote:
{
cout << "You are pointing to nowhere , where do you
think you are going ??n";
return NULL;
}
else
{
return this->next;
}
}


MACList::MACList(void)
{
head=NULL;
current=NULL;
itsCount=0;
}


MACList::~MACList(void)
{/*
if(IsListEmpy()==true)
{
//do nothing ..
}
else
{
DeleteList();
}*/
}

bool MACList::IsListEmpty(void)
{
if(head==NULL)
{
return true;
}

//else ....
return false;
}


bool MACList::FindNode(const String& mac)
{
MACListPart *pMacListPart=head;
while(pMacListPart !=NULL)
{
if(pMacListPart->macAdr==mac)
{
return true;
}
}
return false;
}


int MACList::GetCount(void)
{
return itsCount;
}


void MACList::AddNode(const String& mac)
{
MACListPart *pMACListPart = new MACListPart;
pMACListPart->macAdr=mac;
pMACListPart->data=DEFAULT;
pMACListPart->next=NULL;

if(IsListEmpty() == true)
{
head=pMACListPart;
current=head;
}
else // (if IsListEmpty() == false )
{
current->next=pMACListPart;
current=pMACListPart;
}
// increasing the elements of the list
itsCount++;
}




void MACList::PrintList()
{
MACListPart *temp = head;
while( temp != NULL)
{
cout << temp->macAdr.GetString() << endl;
temp=temp->next;
}
}


void MACList::DeleteNode(const String& mac)
{
char c;
// If the first element of the list is going to be deleted:
if(head->macAdr==mac)
{
MACListPart *tempNode;
tempNode=head->next;
delete head;
head=tempNode;
}
else
{
MACListPart *prevNode,*currNode;
prevNode=head;
currNode=head->next;
while(currNode != NULL)
{
if(currNode->macAdr==mac)
{
MACListPart *tempNode;
tempNode=currNode->next;
prevNode->next=tempNode;
printf("YAHOOOOn");
this->PrintList();
c=getchar();
delete currNode;
The reason it crashes is because you dont exit the loop. Just add a

break; here. Or if you want to delete all nodes that match update
prevNode and currNode.

Mike

Back to top
Andrew Skouloudis
Guest





PostPosted: Mon Dec 29, 2003 4:03 pm    Post subject: Re: Problem with linked list .. Reply with quote

On Mon, 29 Dec 2003 13:16:29 +0000, Michael Mellor
<news-at- (AT) michaelmellor-dot- (DOT) com> wrote:

Quote:
Andrew Skouloudis wrote:
Hello people and merry christmas. I am trying to create a simple
linked list and it seems i am
doing something wrong with the DeleteNode function ...

#include <malloc.h
#include #include #include iostream.h is not standard so use:
#include and for the time being
using namespace std;

#include "GenericStringClass2.h"
#include

#define DEFAULT 0

class MACListPart
{
public:
// constructor
MACListPart();
~MACListPart();
MACListPart *GetNextPointer();


private:
friend class MACList;
String macAdr;
int data;
MACListPart *next;
};


class MACList
{
public:
//constructors
MACList();
~MACList();

//accessors
void PrintList(void);
void AddNode(const String& mac);
void DeleteNode(const String& mac);
bool FindNode(const String&);
// void DeleteList();
bool IsListEmpty();
int GetCount(void);

protected:
MACListPart *head;
MACListPart *current;

private:
int itsCount;
};

MACListPart::MACListPart(void)
{
next=NULL;
data=0;

// macAdr=..
}


MACListPart::~MACListPart(void)
{
cout << "Calling the default destructor for MACListPart ...
n";
if(this != NULL)
{

delete this;
}
else delete this;
This is quite a strange piece of code as it is equivalent to "delete
this". It is not needed.
}

MACListPart* MACListPart::GetNextPointer(void)
{
if(this==NULL)

If this is NULL you have problems, therefore this check is completely
unneeded. Do the NULL check where you use this method.

{
cout << "You are pointing to nowhere , where do you
think you are going ??n";
return NULL;
}
else
{
return this->next;
}
}


MACList::MACList(void)
{
head=NULL;
current=NULL;
itsCount=0;
}


MACList::~MACList(void)
{/*
if(IsListEmpy()==true)
{
//do nothing ..
}
else
{
DeleteList();
}*/
}

bool MACList::IsListEmpty(void)
{
if(head==NULL)
{
return true;
}

//else ....
return false;
}


bool MACList::FindNode(const String& mac)
{
MACListPart *pMacListPart=head;
while(pMacListPart !=NULL)
{
if(pMacListPart->macAdr==mac)
{
return true;
}
}
return false;
}


int MACList::GetCount(void)
{
return itsCount;
}


void MACList::AddNode(const String& mac)
{
MACListPart *pMACListPart = new MACListPart;
pMACListPart->macAdr=mac;
pMACListPart->data=DEFAULT;
pMACListPart->next=NULL;

if(IsListEmpty() == true)
{
head=pMACListPart;
current=head;
}
else // (if IsListEmpty() == false )
{
current->next=pMACListPart;
current=pMACListPart;
}
// increasing the elements of the list
itsCount++;
}




void MACList::PrintList()
{
MACListPart *temp = head;
while( temp != NULL)
{
cout << temp->macAdr.GetString() << endl;
temp=temp->next;
}
}


void MACList::DeleteNode(const String& mac)
{
char c;
// If the first element of the list is going to be deleted:
if(head->macAdr==mac)
{
MACListPart *tempNode;
tempNode=head->next;
delete head;
head=tempNode;
}
else
{
MACListPart *prevNode,*currNode;
prevNode=head;
currNode=head->next;
while(currNode != NULL)
{
if(currNode->macAdr==mac)
{
MACListPart *tempNode;
tempNode=currNode->next;
prevNode->next=tempNode;
printf("YAHOOOOn");
this->PrintList();
c=getchar();
delete currNode;
The reason it crashes is because you dont exit the loop. Just add a
break; here. Or if you want to delete all nodes that match update
prevNode and currNode.

Mike

Yes that's it .. THANK YOU SO MUCH !!!!!!!!!!!!!!!

Back to top
Brian MacBride
Guest





PostPosted: Mon Dec 29, 2003 4:15 pm    Post subject: Re: Problem with linked list .. Reply with quote


"Andrew Skouloudis" <afterskoulNOSPAMPLEASE (AT) yahoo (DOT) com> wrote

Quote:


You didn't forget that you have this silly....

c = getchar ();

...here. Lose it...

Regards

Brian



I am not THAT stupid ...

I didn't mean to imply that you were. I often overlook the obvious at
times. Sometimes I can't see the forest for the trees. ;-)

Quote:
When I i say it hangs i mean that the
execution of the program stops and Windows XP creates a window
which says that " LinkedList3.exe has encourted a problem and needs
to close . We are sorry for the inconvenience " blah-blah .. Debug ,
Send Report , Don't Send ". The problem is with the delete operation .

One of the things that would give me that message is running out of stack
space.

This...

MACListPart::~MACListPart (void) {
cout << "Calling the default destructor for MACListPart ...n";
if (this != NULL)
delete this;
else
delete this;
}

.... will set up an endless loop and would have caused that.that message for
me.

Quote:
Did you run this program with VC++ ???.

....and two others... g++ amd Borland

Quote:
What's your String destructor
???

Well I don't have your String class but your d'tor looks OK and would have
worked for you elsewhere in your program.

I used std::string, implemented thus...

#include #include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
// #include "GenericStringClass2.h"
#include <string.h>

// Emulate 'class String'
#include <string>
typedef std::string String;
#define GetString c_str

#define DEFAULT 0

<snip>

,,,You could try that.

Regards

Brian



Back to top
Brian MacBride
Guest





PostPosted: Mon Dec 29, 2003 4:33 pm    Post subject: Re: Problem with linked list .. Reply with quote


"Brian MacBride" <macbride (AT) ix (DOT) netcom.com> wrote

Quote:

"Andrew Skouloudis" <afterskoulNOSPAMPLEASE (AT) yahoo (DOT) com> wrote in message
news:7a20vvos81t9bnmmsk5rts23bq012q1i78 (AT) 4ax (DOT) com...


You didn't forget that you have this silly....

c = getchar ();

...here. Lose it...

Regards

Brian



I am not THAT stupid ...

I didn't mean to imply that you were. I often overlook the obvious at
times. Sometimes I can't see the forest for the trees. ;-)


See, I told you that we all overlook the obvious at times. As Michael Mellor
did, I
added a break to the loop in your void MACList::DeleteNode (const String
&mac) function, and then forgot to mention that to you. Sorry.

Regards

Brian



Back to top
Wagner Bruna
Guest





PostPosted: Mon Dec 29, 2003 5:39 pm    Post subject: Re: Problem with linked list .. Reply with quote

Hi,

Andrew Skouloudis <afterskoulNOSPAMPLEASE (AT) yahoo (DOT) com> wrote

Quote:
Hello people and merry christmas. I am trying to create a simple
linked list and it seems i am
doing something wrong with the DeleteNode function ...

#include <malloc.h
#include #include #include #include "GenericStringClass2.h"
#include

Many of these headers are not necessary in your program. Also, you
should be using

Quote:
#define DEFAULT 0

"const int DEFAULT = 0;" is better here.


Quote:
(...)

MACListPart::~MACListPart(void)
{
cout << "Calling the default destructor for MACListPart ...
n";
if(this != NULL)
{

delete this;
}
else delete this;

As someone else has pointed out, you shouldn't do this. See the FAQ
for details:

http://www.parashift.com/c++-faq-lite/


Quote:
}

MACListPart* MACListPart::GetNextPointer(void)
{
if(this==NULL)
{
cout << "You are pointing to nowhere , where do you
think you are going ??n";
return NULL;
}
else
{
return this->next;
}

Here, IMHO, "assert(this != NULL)" would make more sense...


Quote:
(...)
bool MACList::FindNode(const String& mac)
{
MACListPart *pMacListPart=head;
while(pMacListPart !=NULL)
{
if(pMacListPart->macAdr==mac)
{
return true;
}
}
return false;
}

You forgot to update pMacListPart.


Quote:
(...)
void MACList::AddNode(const String& mac)
{
MACListPart *pMACListPart = new MACListPart;
pMACListPart->macAdr=mac;
pMACListPart->data=DEFAULT;
pMACListPart->next=NULL;

if(IsListEmpty() == true)
{
head=pMACListPart;
current=head;
}
else // (if IsListEmpty() == false )
{
current->next=pMACListPart;
current=pMACListPart;

If "current" is used only to help insertions at the end of the list,
its name is misleading; also, you may need to update it when you
delete a node.


Quote:
}
// increasing the elements of the list
itsCount++;
}

(...)

The problem is when i am trying to delete a node .. The program hangs
after the command
delete currNode is executed ... The String class is declared in
another file and here is the
default destructor :

Hard to say anything without the complete String class, but you could
for example replace it with std::string (or, better yet, int) for
testing MACList separately.

HTH,
Wagner

Back to top
Andrew Skouloudis
Guest





PostPosted: Tue Dec 30, 2003 8:03 am    Post subject: Re: Problem with linked list .. Reply with quote

On Mon, 29 Dec 2003 16:33:24 GMT, "Brian MacBride"
<macbride (AT) ix (DOT) netcom.com> wrote:

Quote:

"Brian MacBride" <macbride (AT) ix (DOT) netcom.com> wrote in message
news:bspjvg$f9jl9$1 (AT) ID-26770 (DOT) news.uni-berlin.de...

"Andrew Skouloudis" <afterskoulNOSPAMPLEASE (AT) yahoo (DOT) com> wrote in message
news:7a20vvos81t9bnmmsk5rts23bq012q1i78 (AT) 4ax (DOT) com...


You didn't forget that you have this silly....

c = getchar ();

...here. Lose it...

Regards

Brian



I am not THAT stupid ...

I didn't mean to imply that you were. I often overlook the obvious at
times. Sometimes I can't see the forest for the trees. ;-)


See, I told you that we all overlook the obvious at times. As Michael Mellor
did, I
added a break to the loop in your void MACList::DeleteNode (const String
&mac) function, and then forgot to mention that to you. Sorry.

Regards

Brian


I would like to thank you for your help ... I am new to C++
programming so I make many mistakes !!!

Back to top
Andrew Skouloudis
Guest





PostPosted: Tue Dec 30, 2003 8:07 am    Post subject: Re: Problem with linked list .. Reply with quote

On 29 Dec 2003 09:39:08 -0800, [email]wbruna (AT) yahoo (DOT) com[/email] (Wagner Bruna) wrote:

Quote:
Hi,

Andrew Skouloudis <afterskoulNOSPAMPLEASE (AT) yahoo (DOT) com> wrote

Hello people and merry christmas. I am trying to create a simple
linked list and it seems i am
doing something wrong with the DeleteNode function ...

#include <malloc.h
#include #include #include #include "GenericStringClass2.h"
#include
Many of these headers are not necessary in your program. Also, you
should be using

#define DEFAULT 0

"const int DEFAULT = 0;" is better here.


(...)

MACListPart::~MACListPart(void)
{
cout << "Calling the default destructor for MACListPart ...
n";
if(this != NULL)
{

delete this;
}
else delete this;

As someone else has pointed out, you shouldn't do this. See the FAQ
for details:

http://www.parashift.com/c++-faq-lite/


}

MACListPart* MACListPart::GetNextPointer(void)
{
if(this==NULL)
{
cout << "You are pointing to nowhere , where do you
think you are going ??n";
return NULL;
}
else
{
return this->next;
}

Here, IMHO, "assert(this != NULL)" would make more sense...


(...)
bool MACList::FindNode(const String& mac)
{
MACListPart *pMacListPart=head;
while(pMacListPart !=NULL)
{
if(pMacListPart->macAdr==mac)
{
return true;
}
}
return false;
}

You forgot to update pMacListPart.


(...)
void MACList::AddNode(const String& mac)
{
MACListPart *pMACListPart = new MACListPart;
pMACListPart->macAdr=mac;
pMACListPart->data=DEFAULT;
pMACListPart->next=NULL;

if(IsListEmpty() == true)
{
head=pMACListPart;
current=head;
}
else // (if IsListEmpty() == false )
{
current->next=pMACListPart;
current=pMACListPart;

If "current" is used only to help insertions at the end of the list,
its name is misleading; also, you may need to update it when you
delete a node.


}
// increasing the elements of the list
itsCount++;
}

(...)

The problem is when i am trying to delete a node .. The program hangs
after the command
delete currNode is executed ... The String class is declared in
another file and here is the
default destructor :

Hard to say anything without the complete String class, but you could
for example replace it with std::string (or, better yet, int) for
testing MACList separately.

HTH,
Wagner


Thank you very much !! . Do you know whre I can download a zip file
containing the entire faq ??? (The administrator of the web site
http://www.parashift.com/c++-faq-lite/ has disabled that option !!)

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.