Problem with linked list ..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Skouloudis

    Problem with linked list ..

    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 ... )
  • Brian MacBride

    #2
    Re: Problem with linked list ..


    "Andrew Skouloudis" <afterskoulNOSP AMPLEASE@yahoo. com> wrote in message
    news:o8huuv4au1 7jsl8mum1bpbd2q jker3med6@4ax.c om...[color=blue]
    > 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::~M ACListPart(void )
    > {
    > cout << "Calling the default destructor for MACListPart ...
    > \n";[/color]

    /*
    [color=blue]
    > if(this != NULL)
    > {
    >
    > delete this;
    > }
    > else delete this;[/color]

    */
    [color=blue]
    > }
    >[/color]

    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::Delete Node (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?
    [color=blue]
    >
    > Can you help ?? I am using Visual C++
    >
    > (Sorry for my english ... )[/color]

    Regards

    Brian


    Comment

    • Andrew Skouloudis

      #3
      Re: Problem with linked list ..

      On Sun, 28 Dec 2003 12:22:14 -1000, "Brian MacBride"
      <macbride@ix.ne tcom.com> wrote:
      [color=blue]
      >
      >"Andrew Skouloudis" <afterskoulNOSP AMPLEASE@yahoo. com> wrote in message
      >news:o8huuv4au 17jsl8mum1bpbd2 qjker3med6@4ax. com...[color=green]
      >> 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::~M ACListPart(void )
      >> {
      >> cout << "Calling the default destructor for MACListPart ...
      >> \n";[/color]
      >
      >/*
      >[color=green]
      >> if(this != NULL)
      >> {
      >>
      >> delete this;
      >> }
      >> else delete this;[/color]
      >
      >*/
      >[color=green]
      >> }
      >>[/color]
      >
      >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::Delete Node (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?
      >[color=green]
      >>
      >> Can you help ?? I am using Visual C++
      >>
      >> (Sorry for my english ... )[/color]
      >
      >Regards
      >
      >Brian
      >[/color]
      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 !!!

      Comment

      • Brian MacBride

        #4
        Re: Problem with linked list ..


        "Andrew Skouloudis" <afterskoulNOSP AMPLEASE@yahoo. com> wrote in message
        news:h7pvuv4al0 n6fdm0s1hu7ef1v ll7q5ghi4@4ax.c om...[color=blue]
        > On Sun, 28 Dec 2003 12:22:14 -1000, "Brian MacBride"
        > <macbride@ix.ne tcom.com> wrote:
        >[color=green]
        > >
        > >"Andrew Skouloudis" <afterskoulNOSP AMPLEASE@yahoo. com> wrote in message
        > >news:o8huuv4au 17jsl8mum1bpbd2 qjker3med6@4ax. com...[color=darkred]
        > >> 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::~M ACListPart(void )
        > >> {
        > >> cout << "Calling the default destructor for MACListPart ...
        > >> \n";[/color]
        > >
        > >/*
        > >[color=darkred]
        > >> if(this != NULL)
        > >> {
        > >>
        > >> delete this;
        > >> }
        > >> else delete this;[/color]
        > >
        > >*/
        > >[color=darkred]
        > >> }
        > >>[/color]
        > >
        > >You don't need to write destructor for MACListPart. If you do... you[/color][/color]
        don't[color=blue][color=green]
        > >want to do the suicide thing...
        > >
        > >Remember to decrement itsCount in the
        > >
        > >void MACList::Delete Node (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?
        > >[color=darkred]
        > >>
        > >> Can you help ?? I am using Visual C++
        > >>
        > >> (Sorry for my english ... )[/color]
        > >
        > >Regards
        > >
        > >Brian
        > >[/color]
        > 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 !!![/color]

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

        c = getchar ();

        ....here. Lose it...

        Regards

        Brian



        Comment

        • Andrew Skouloudis

          #5
          Re: Problem with linked list ..

          [color=blue]
          >
          >You didn't forget that you have this silly....
          >
          > c = getchar ();
          >
          >...here. Lose it...
          >
          >Regards
          >
          >Brian
          >
          >[/color]

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

          Comment

          • Michael Mellor

            #6
            Re: Problem with linked list ..

            Andrew Skouloudis wrote:[color=blue]
            > 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>[/color]
            iostream.h is not standard so use:
            #include <iostream>
            and for the time being
            using namespace std;
            [color=blue]
            > #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;[/color]
            This is quite a strange piece of code as it is equivalent to "delete
            this". It is not needed.[color=blue]
            > }
            >
            > MACListPart* MACListPart::Ge tNextPointer(vo id)
            > {
            > if(this==NULL)[/color]

            If this is NULL you have problems, therefore this check is completely
            unneeded. Do the NULL check where you use this method.
            [color=blue]
            > {
            > 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;[/color]
            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

            Comment

            • Andrew Skouloudis

              #7
              Re: Problem with linked list ..

              On Mon, 29 Dec 2003 13:16:29 +0000, Michael Mellor
              <news-at-@michaelmellor-dot-.com> wrote:
              [color=blue]
              >Andrew Skouloudis wrote:[color=green]
              >> 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>[/color]
              >iostream.h is not standard so use:
              >#include <iostream>
              >and for the time being
              >using namespace std;
              >[color=green]
              >> #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;[/color]
              >This is quite a strange piece of code as it is equivalent to "delete
              >this". It is not needed.[color=green]
              >> }
              >>
              >> MACListPart* MACListPart::Ge tNextPointer(vo id)
              >> {
              >> if(this==NULL)[/color]
              >
              >If this is NULL you have problems, therefore this check is completely
              >unneeded. Do the NULL check where you use this method.
              >[color=green]
              >> {
              >> 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;[/color]
              >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[/color]

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

              Comment

              • Brian MacBride

                #8
                Re: Problem with linked list ..


                "Andrew Skouloudis" <afterskoulNOSP AMPLEASE@yahoo. com> wrote in message
                news:7a20vvos81 t9bnmmsk5rts23b q012q1i78@4ax.c om...[color=blue]
                >[color=green]
                > >
                > >You didn't forget that you have this silly....
                > >
                > > c = getchar ();
                > >
                > >...here. Lose it...
                > >
                > >Regards
                > >
                > >Brian
                > >
                > >[/color]
                >
                > I am not THAT stupid ...[/color]

                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. ;-)
                [color=blue]
                > 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 .[/color]

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

                This...

                MACListPart::~M ACListPart (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.
                [color=blue]
                > Did you run this program with VC++ ???.[/color]

                ....and two others... g++ amd Borland
                [color=blue]
                > What's your String destructor
                > ???[/color]

                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 <malloc.h>
                #include <stdio.h>
                #include <stdlib.h>
                #include <iostream.h>
                // #include "GenericStringC lass2.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


                Comment

                • Brian MacBride

                  #9
                  Re: Problem with linked list ..


                  "Brian MacBride" <macbride@ix.ne tcom.com> wrote in message
                  news:bspjvg$f9j l9$1@ID-26770.news.uni-berlin.de...[color=blue]
                  >
                  > "Andrew Skouloudis" <afterskoulNOSP AMPLEASE@yahoo. com> wrote in message
                  > news:7a20vvos81 t9bnmmsk5rts23b q012q1i78@4ax.c om...[color=green]
                  > >[color=darkred]
                  > > >
                  > > >You didn't forget that you have this silly....
                  > > >
                  > > > c = getchar ();
                  > > >
                  > > >...here. Lose it...
                  > > >
                  > > >Regards
                  > > >
                  > > >Brian
                  > > >
                  > > >[/color]
                  > >
                  > > I am not THAT stupid ...[/color]
                  >
                  > 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. ;-)
                  >[/color]

                  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::Delete Node (const String
                  &mac) function, and then forgot to mention that to you. Sorry.

                  Regards

                  Brian


                  Comment

                  • Wagner Bruna

                    #10
                    Re: Problem with linked list ..

                    Hi,

                    Andrew Skouloudis <afterskoulNOSP AMPLEASE@yahoo. com> wrote in message news:<o8huuv4au 17jsl8mum1bpbd2 qjker3med6@4ax. com>...[color=blue]
                    > 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>[/color]

                    Many of these headers are not necessary in your program. Also, you
                    should be using <iostream> instead of <iostream.h>.

                    [color=blue]
                    > #define DEFAULT 0[/color]

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

                    [color=blue]
                    > (...)
                    >
                    > MACListPart::~M ACListPart(void )
                    > {
                    > cout << "Calling the default destructor for MACListPart ...
                    > \n";
                    > if(this != NULL)
                    > {
                    >
                    > delete this;
                    > }
                    > else delete this;[/color]

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



                    [color=blue]
                    > }
                    >
                    > 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;
                    > }[/color]

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

                    [color=blue]
                    > (...)
                    > bool MACList::FindNo de(const String& mac)
                    > {
                    > MACListPart *pMacListPart=h ead;
                    > while(pMacListP art !=NULL)
                    > {
                    > if(pMacListPart->macAdr==mac)
                    > {
                    > return true;
                    > }
                    > }
                    > return false;
                    > }[/color]

                    You forgot to update pMacListPart.

                    [color=blue]
                    > (...)
                    > 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;[/color]

                    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.

                    [color=blue]
                    > }
                    > // 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 :[/color]

                    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

                    Comment

                    • Andrew Skouloudis

                      #11
                      Re: Problem with linked list ..

                      On Mon, 29 Dec 2003 16:33:24 GMT, "Brian MacBride"
                      <macbride@ix.ne tcom.com> wrote:
                      [color=blue]
                      >
                      >"Brian MacBride" <macbride@ix.ne tcom.com> wrote in message
                      >news:bspjvg$f9 jl9$1@ID-26770.news.uni-berlin.de...[color=green]
                      >>
                      >> "Andrew Skouloudis" <afterskoulNOSP AMPLEASE@yahoo. com> wrote in message
                      >> news:7a20vvos81 t9bnmmsk5rts23b q012q1i78@4ax.c om...[color=darkred]
                      >> >
                      >> > >
                      >> > >You didn't forget that you have this silly....
                      >> > >
                      >> > > c = getchar ();
                      >> > >
                      >> > >...here. Lose it...
                      >> > >
                      >> > >Regards
                      >> > >
                      >> > >Brian
                      >> > >
                      >> > >
                      >> >
                      >> > I am not THAT stupid ...[/color]
                      >>
                      >> 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. ;-)
                      >>[/color]
                      >
                      >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::Delete Node (const String
                      >&mac) function, and then forgot to mention that to you. Sorry.
                      >
                      >Regards
                      >
                      >Brian
                      >[/color]

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

                      Comment

                      • Andrew Skouloudis

                        #12
                        Re: Problem with linked list ..

                        On 29 Dec 2003 09:39:08 -0800, wbruna@yahoo.co m (Wagner Bruna) wrote:
                        [color=blue]
                        >Hi,
                        >
                        >Andrew Skouloudis <afterskoulNOSP AMPLEASE@yahoo. com> wrote in message news:<o8huuv4au 17jsl8mum1bpbd2 qjker3med6@4ax. com>...[color=green]
                        >> 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>[/color]
                        >
                        >Many of these headers are not necessary in your program. Also, you
                        >should be using <iostream> instead of <iostream.h>.
                        >
                        >[color=green]
                        >> #define DEFAULT 0[/color]
                        >
                        >"const int DEFAULT = 0;" is better here.
                        >
                        >[color=green]
                        >> (...)
                        >>
                        >> MACListPart::~M ACListPart(void )
                        >> {
                        >> cout << "Calling the default destructor for MACListPart ...
                        >> \n";
                        >> if(this != NULL)
                        >> {
                        >>
                        >> delete this;
                        >> }
                        >> else delete this;[/color]
                        >
                        >As someone else has pointed out, you shouldn't do this. See the FAQ
                        >for details:
                        >
                        >http://www.parashift.com/c++-faq-lite/
                        >
                        >[color=green]
                        >> }
                        >>
                        >> 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;
                        >> }[/color]
                        >
                        >Here, IMHO, "assert(thi s != NULL)" would make more sense...
                        >
                        >[color=green]
                        >> (...)
                        >> bool MACList::FindNo de(const String& mac)
                        >> {
                        >> MACListPart *pMacListPart=h ead;
                        >> while(pMacListP art !=NULL)
                        >> {
                        >> if(pMacListPart->macAdr==mac)
                        >> {
                        >> return true;
                        >> }
                        >> }
                        >> return false;
                        >> }[/color]
                        >
                        >You forgot to update pMacListPart.
                        >
                        >[color=green]
                        >> (...)
                        >> 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;[/color]
                        >
                        >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.
                        >
                        >[color=green]
                        >> }
                        >> // 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 :[/color]
                        >
                        >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[/color]


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

                        Comment

                        Working...