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 "GenericStringC lass2.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(cons t String& mac);
bool FindNode(const String&);
// void DeleteList();
bool IsListEmpty();
int GetCount(void);
protected:
MACListPart *head;
MACListPart *current;
private:
int itsCount;
};
MACListPart::MA CListPart(void)
{
next=NULL;
data=0;
// macAdr=..
}
MACListPart::~M ACListPart(void )
{
cout << "Calling the default destructor for MACListPart ...
\n";
if(this != NULL)
{
delete this;
}
else delete this;
}
MACListPart* MACListPart::Ge tNextPointer(vo id)
{
if(this==NULL)
{
cout << "You are pointing to nowhere , where do you
think you are going ??\n";
return NULL;
}
else
{
return this->next;
}
}
MACList::MACLis t(void)
{
head=NULL;
current=NULL;
itsCount=0;
}
MACList::~MACLi st(void)
{/*
if(IsListEmpy() ==true)
{
//do nothing ..
}
else
{
DeleteList();
}*/
}
bool MACList::IsList Empty(void)
{
if(head==NULL)
{
return true;
}
//else ....
return false;
}
bool MACList::FindNo de(const String& mac)
{
MACListPart *pMacListPart=h ead;
while(pMacListP art !=NULL)
{
if(pMacListPart->macAdr==mac)
{
return true;
}
}
return false;
}
int MACList::GetCou nt(void)
{
return itsCount;
}
void MACList::AddNod e(const String& mac)
{
MACListPart *pMACListPart = new MACListPart;
pMACListPart->macAdr=mac;
pMACListPart->data=DEFAULT ;
pMACListPart->next=NULL;
if(IsListEmpty( ) == true)
{
head=pMACListPa rt;
current=head;
}
else // (if IsListEmpty() == false )
{
current->next=pMACListP art;
current=pMACLis tPart;
}
// increasing the elements of the list
itsCount++;
}
void MACList::PrintL ist()
{
MACListPart *temp = head;
while( temp != NULL)
{
cout << temp->macAdr.GetStri ng() << endl;
temp=temp->next;
}
}
void MACList::Delete Node(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,*curr Node;
prevNode=head;
currNode=head->next;
while(currNode != NULL)
{
if(currNode->macAdr==mac)
{
MACListPart *tempNode;
tempNode=currNo de->next;
prevNode->next=tempNod e;
printf("YAHOOOO \n");
this->PrintList();
c=getchar();
delete currNode;
}
else // you didn't find the string in currNode
so ...
{
prevNode=currNo de;
currNode=currNo de->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 ... )
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 "GenericStringC lass2.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(cons t String& mac);
bool FindNode(const String&);
// void DeleteList();
bool IsListEmpty();
int GetCount(void);
protected:
MACListPart *head;
MACListPart *current;
private:
int itsCount;
};
MACListPart::MA CListPart(void)
{
next=NULL;
data=0;
// macAdr=..
}
MACListPart::~M ACListPart(void )
{
cout << "Calling the default destructor for MACListPart ...
\n";
if(this != NULL)
{
delete this;
}
else delete this;
}
MACListPart* MACListPart::Ge tNextPointer(vo id)
{
if(this==NULL)
{
cout << "You are pointing to nowhere , where do you
think you are going ??\n";
return NULL;
}
else
{
return this->next;
}
}
MACList::MACLis t(void)
{
head=NULL;
current=NULL;
itsCount=0;
}
MACList::~MACLi st(void)
{/*
if(IsListEmpy() ==true)
{
//do nothing ..
}
else
{
DeleteList();
}*/
}
bool MACList::IsList Empty(void)
{
if(head==NULL)
{
return true;
}
//else ....
return false;
}
bool MACList::FindNo de(const String& mac)
{
MACListPart *pMacListPart=h ead;
while(pMacListP art !=NULL)
{
if(pMacListPart->macAdr==mac)
{
return true;
}
}
return false;
}
int MACList::GetCou nt(void)
{
return itsCount;
}
void MACList::AddNod e(const String& mac)
{
MACListPart *pMACListPart = new MACListPart;
pMACListPart->macAdr=mac;
pMACListPart->data=DEFAULT ;
pMACListPart->next=NULL;
if(IsListEmpty( ) == true)
{
head=pMACListPa rt;
current=head;
}
else // (if IsListEmpty() == false )
{
current->next=pMACListP art;
current=pMACLis tPart;
}
// increasing the elements of the list
itsCount++;
}
void MACList::PrintL ist()
{
MACListPart *temp = head;
while( temp != NULL)
{
cout << temp->macAdr.GetStri ng() << endl;
temp=temp->next;
}
}
void MACList::Delete Node(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,*curr Node;
prevNode=head;
currNode=head->next;
while(currNode != NULL)
{
if(currNode->macAdr==mac)
{
MACListPart *tempNode;
tempNode=currNo de->next;
prevNode->next=tempNod e;
printf("YAHOOOO \n");
this->PrintList();
c=getchar();
delete currNode;
}
else // you didn't find the string in currNode
so ...
{
prevNode=currNo de;
currNode=currNo de->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 ... )
Comment