A
Andrew Skouloudis
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:rintList()
{
MACListPart *temp = head;
while( temp != NULL)
{
cout << temp->macAdr.GetString() << endl;
temp=temp->next;
}
}
void MACList:eleteNode(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("YAHOOOO\n");
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 ... )
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:rintList()
{
MACListPart *temp = head;
while( temp != NULL)
{
cout << temp->macAdr.GetString() << endl;
temp=temp->next;
}
}
void MACList:eleteNode(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("YAHOOOO\n");
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 ... )