the code of two-way linked list failed to be compiled.

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

    #1

    the code of two-way linked list failed to be compiled.

    I'm using Dev-c.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>


    class Node
    {
    friend class List;
    public:
    Node(int d,Node* p=NULL,Node*
    n=NULL):data(d) ,prev(p),next(n ){}
    private:
    int data;
    Node* prev;
    Node* next;
    };


    class List
    {
    public:
    List():head(NUL L){}
    ~List()
    {
    Node* cur=head;
    Node* prev=NULL;
    while(cur!=NULL )
    {
    prev=cur;
    cur=cur->next;
    delete prev;
    }
    }


    void insert(int data)
    {
    Node* node=new Node(data);
    if(head=NULL)
    {
    head=node;
    return;
    }

    Node* cur=head;
    while(cur->next!=NULL)
    {
    if(data<=cur->data)
    {
    if(cur->prev==NULL)
    {
    head=node;
    head->next=cur;
    cur->prev=head;
    }
    else
    {
    node->prev=cur->prev;
    node->next=cur;
    cur->prev->next=node; //???
    }
    return;
    }
    else
    {
    cur=cur->next;
    }
    }


    if(cur->data<=data)
    {
    cur->next=node;
    node->prev=cur;
    }
    else
    {
    node->next=cur;
    node->prev=cur->prev;
    cur->prev->next=node;
    }
    }


    void DelNode(int da)
    {
    Node* cur=head;
    while(cur->next!=NULL)
    {
    if(cur->data==da)
    {
    if(cur->prev==NULL)
    {
    head=cur->next;
    delete cur;
    }
    else
    {
    cur->prev->next=cur->next;
    cur->next->prev=cur->prev;
    delete cur;
    }
    return;
    }
    else
    {
    cur=cur->next;
    }
    }
    if(cur->data==da)
    {
    cur->prev->next=NULL;
    delete cur;
    }

    }

    void display()
    {
    Node* cur=head;
    while(cur!=NULL )
    {
    printf("%d\n",c ur->data);
    cur=cur->next;
    }
    }

    private:
    Node* head;
    };





    int main(int argc, char *argv[])
    {
    List l;
    l.insert(10);
    l.insert(100);
    l.insert(150);
    l.insert(145);
    l.insert(200);
    l.insert(2);
    l.insert(180);
    l.insert(135);
    l.insert(130);
    l.insert(1);
    l.insert(1);
    l.insert(200);
    l.display();
    printf("******* ************\n" );
    l.DelNode(200);
    l.display();

    system("PAUSE") ;
    return 0;
    }
  • Eric Sosman

    #2
    Re: the code of two-way linked list failed to be compiled.

    yang2006 wrote:
    I'm using Dev-c.
    >
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    >
    >
    class Node
    [...]
    You're in the wrong newsgroup. comp.lang.c++ is down
    the hall to your left, second door past the water cooler.
    Yes, the room with the bad karaoke party. Have fun!

    --
    Eric Sosman
    esosman@ieee-dot-org.invalid

    Comment

    • osmium

      #3
      Re: the code of two-way linked list failed to be compiled.

      "yang2006" writes:

      Look more closely at your screen. Actually it *does* compile, it just makes
      an error before anything is printed. Next step: isolate the input by
      putting #if 0 --- #endif around the input data. This should be enough so
      you can proceed under your own capabilities. Two elementary notes in code

      I'm using Dev-c.
      >
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      >
      >
      class Node
      {
      friend class List;
      public:
      Node(int d,Node* p=NULL,Node*
      n=NULL):data(d) ,prev(p),next(n ){}
      private:
      int data;
      Node* prev;
      Node* next;
      };
      >
      >
      class List
      {
      public:
      List():head(NUL L){}
      ~List()
      {
      Node* cur=head;
      Node* prev=NULL;
      while(cur!=NULL )
      {
      prev=cur;
      cur=cur->next;
      delete prev;
      }
      }
      >
      >
      void insert(int data)
      {
      Node* node=new Node(data);
      if(head=NULL)
      {
      head=node;
      return;
      }
      >
      Node* cur=head;
      while(cur->next!=NULL)
      {
      if(data<=cur->data)
      {
      if(cur->prev==NULL)
      {
      head=node;
      head->next=cur;
      cur->prev=head;
      }
      else
      {
      node->prev=cur->prev;
      node->next=cur;
      cur->prev->next=node; //???
      }
      return;
      }
      else
      {
      cur=cur->next;
      }
      }
      >
      >
      if(cur->data<=data)
      {
      cur->next=node;
      node->prev=cur;
      }
      else
      {
      node->next=cur;
      node->prev=cur->prev;
      cur->prev->next=node;
      }
      }
      >
      >
      void DelNode(int da)
      {
      Node* cur=head;
      while(cur->next!=NULL)
      {
      if(cur->data==da)
      {
      if(cur->prev==NULL)
      {
      head=cur->next;
      delete cur;
      }
      else
      {
      cur->prev->next=cur->next;
      cur->next->prev=cur->prev;
      delete cur;
      }
      return;
      }
      else
      {
      cur=cur->next;
      }
      }
      if(cur->data==da)
      {
      cur->prev->next=NULL;
      delete cur;
      }
      >
      }
      >
      void display()
      {
      Node* cur=head;
      while(cur!=NULL )
      {
      printf("%d\n",c ur->data);
      cur=cur->next;
      }
      }
      >
      private:
      Node* head;
      };
      >
      >
      >
      >
      >
      int main(int argc, char *argv[])
      {
      List l;
      #if 0
      l.insert(10);
      l.insert(100);
      l.insert(150);
      l.insert(145);
      l.insert(200);
      l.insert(2);
      l.insert(180);
      l.insert(135);
      l.insert(130);
      l.insert(1);
      l.insert(1);
      l.insert(200);
      l.display();
      printf("******* ************\n" );
      l.DelNode(200);
      l.display();
      #endif
      system("PAUSE") ;
      return 0;
      }

      Comment

      • Joachim Schmitz

        #4
        Re: the code of two-way linked list failed to be compiled.

        yang2006 wrote:
        I'm using Dev-c.
        No, you're using Dev-C++
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        >
        >
        class Node
        C doesn't have class
        {
        friend class List;
        public:
        nor friend or public
        Node(int d,Node* p=NULL,Node*
        n=NULL):data(d) ,prev(p),next(n ){}
        private:
        or private. C++ does have these, so you're better off asking your quesion in
        comp.lang.c++. If you do, do also mention the error message(s) you get from
        your compiler.

        However: your code compiles cleanly with VisualC++ Express, but crashes when
        started.
        int data;
        Node* prev;
        Node* next;
        };
        >
        >
        class List
        {
        public:
        List():head(NUL L){}
        ~List()
        {
        Node* cur=head;
        Node* prev=NULL;
        while(cur!=NULL )
        {
        prev=cur;
        cur=cur->next;
        delete prev;
        }
        }
        >
        >
        void insert(int data)
        {
        Node* node=new Node(data);
        if(head=NULL)
        I guess you most probably meant to write
        if(head==NULL)
        here, and as this is a typical error in C as well as in C++, you're not
        completly OT here.
        {
        head=node;
        return;
        }
        >
        Node* cur=head;
        while(cur->next!=NULL)
        Here it crashes. It does not with above mentioned fix...
        {
        if(data<=cur->data)
        {
        if(cur->prev==NULL)
        {
        head=node;
        head->next=cur;
        cur->prev=head;
        }
        else
        {
        node->prev=cur->prev;
        node->next=cur;
        cur->prev->next=node; //???
        }
        return;
        }
        else
        {
        cur=cur->next;
        }
        }
        >
        >
        if(cur->data<=data)
        {
        cur->next=node;
        node->prev=cur;
        }
        else
        {
        node->next=cur;
        node->prev=cur->prev;
        cur->prev->next=node;
        }
        }
        >
        >
        void DelNode(int da)
        {
        Node* cur=head;
        while(cur->next!=NULL)
        {
        if(cur->data==da)
        {
        if(cur->prev==NULL)
        {
        head=cur->next;
        delete cur;
        }
        else
        {
        cur->prev->next=cur->next;
        cur->next->prev=cur->prev;
        delete cur;
        }
        return;
        }
        else
        {
        cur=cur->next;
        }
        }
        if(cur->data==da)
        {
        cur->prev->next=NULL;
        delete cur;
        }
        >
        }
        >
        void display()
        {
        Node* cur=head;
        while(cur!=NULL )
        {
        printf("%d\n",c ur->data);
        cur=cur->next;
        }
        }
        >
        private:
        Node* head;
        };
        >
        >
        >
        >
        >
        int main(int argc, char *argv[])
        {
        List l;
        l.insert(10);
        l.insert(100);
        l.insert(150);
        l.insert(145);
        l.insert(200);
        l.insert(2);
        l.insert(180);
        l.insert(135);
        l.insert(130);
        l.insert(1);
        l.insert(1);
        l.insert(200);
        l.display();
        printf("******* ************\n" );
        l.DelNode(200);
        l.display();
        >
        system("PAUSE") ;
        return 0;
        }
        Bye, Jojo


        Comment

        • Martin Ambuhl

          #5
          Re: the code of two-way linked list failed to be compiled.

          yang2006 wrote:
          class Node
          {
          friend class List;
          public:
          Node(int d,Node* p=NULL,Node*
          The above (and most of what follows) is not C, but C++, a different
          language. You need to post C++ questions in <news:comp.lang .c++>. Even
          though such etiquette seems to have been lost in the invasion of the
          barbarians, you might consider following it by checking the FAQ and post
          postings to that newsgroup before posting there.

          You will want to change your C headers, <stdio.h>, <stdlib.h>,
          <string.h>, to be the C++ standard headers <cstdio>, <cstdlib>, and
          <cstringbefor e posting there. You woll also want to check your
          textbook on the use of namespaces and qualified identifiers, especially
          those in the std namespace.

          Comment

          • Martin Ambuhl

            #6
            Re: the code of two-way linked list failed to be compiled.

            osmium wrote:
            "yang2006" writes:
            >
            Look more closely at your screen. Actually it *does* compile,
            Not with a C compiler. Don't mislead the clueless.

            Comment

            • Richard

              #7
              Re: the code of two-way linked list failed to be compiled.

              Martin Ambuhl <mambuhl@earthl ink.netwrites:
              yang2006 wrote:
              >
              >class Node
              >{
              > friend class List;
              > public:
              > Node(int d,Node* p=NULL,Node*
              >
              The above (and most of what follows) is not C, but C++, a different
              language. You need to post C++ questions in <news:comp.lang .c++>.
              Even though such etiquette seems to have been lost in the invasion of
              the barbarians, you might consider following it by checking the FAQ
              and post postings to that newsgroup before posting there.
              >
              You will want to change your C headers, <stdio.h>, <stdlib.h>,
              <string.h>, to be the C++ standard headers <cstdio>, <cstdlib>, and
              <cstringbefor e posting there. You woll also want to check your
              textbook on the use of namespaces and qualified identifiers,
              especially those in the std namespace.
              I have a simple question for a few posters here:

              Which news servers do you use?

              I see no reason whatsoever for 5 or 6 people to post the exact same
              reprimand. The days of servers taking days to sync are over. Read the
              responses before throwing in your response. Please.

              Now you use Motzarella from what I can see. That is pretty damn quick. I
              saw that OP and the replies ages ago. Why could you not do the same?

              Unnecessary tellings off are worse than spam.

              Comment

              • gw7rib@aol.com

                #8
                Re: the code of two-way linked list failed to be compiled.

                On 21 Sep, 18:25, Martin Ambuhl <mamb...@earthl ink.netwrote:
                You will want to change your C headers, <stdio.h>, <stdlib.h>,
                <string.h>, to be the C++ standard headers <cstdio>, <cstdlib>, and
                <cstringbefor e posting there.  You woll also want to check your
                textbook on the use of namespaces and qualified identifiers, especially
                those in the std namespace.
                He doesn't need to do any of that, and he may not want to. Why not let
                him progress at his own rate?

                Comment

                • Martin Ambuhl

                  #9
                  Re: the code of two-way linked list failed to be compiled.

                  gw7rib@aol.com wrote:
                  On 21 Sep, 18:25, Martin Ambuhl <mamb...@earthl ink.netwrote:
                  >You will want to change your C headers, <stdio.h>, <stdlib.h>,
                  ><string.h>, to be the C++ standard headers <cstdio>, <cstdlib>, and
                  ><cstringbefo re posting there. You woll also want to check your
                  >textbook on the use of namespaces and qualified identifiers, especially
                  >those in the std namespace.
                  >
                  He doesn't need to do any of that, and he may not want to. Why not let
                  him progress at his own rate?
                  If he wants to write C++, he does. If he wants to avoid unmerciful
                  flames in posting to comp.lang.c++, he does. Why don't you crawl back
                  under your bridge?

                  Comment

                  • CBFalconer

                    #10
                    Re: the code of two-way linked list failed to be compiled.

                    yang2006 wrote:
                    >
                    I'm using Dev-c.
                    >
                    #include <stdio.h>
                    #include <stdlib.h>
                    #include <string.h>
                    >
                    class Node {
                    friend class List;
                    public:
                    Node(int d, Node* p = NULL, Node* n = NULL) : data(d),
                    prev(p), next(n){}+
                    private:
                    int data;
                    Node* prev;
                    Node* next;
                    };
                    You are also polluting this newsgroup with C++ code. Don't do
                    that. C++ is a different language.

                    --
                    [mail]: Chuck F (cbfalconer at maineline dot net)
                    [page]: <http://cbfalconer.home .att.net>
                    Try the download section.

                    Comment

                    • jameskuyper@verizon.net

                      #11
                      Re: the code of two-way linked list failed to be compiled.



                      Martin Ambuhl wrote:
                      gw7rib@aol.com wrote:
                      On 21 Sep, 18:25, Martin Ambuhl <mamb...@earthl ink.netwrote:
                      You will want to change your C headers, <stdio.h>, <stdlib.h>,
                      <string.h>, to be the C++ standard headers <cstdio>, <cstdlib>, and
                      <cstringbefor e posting there. You woll also want to check your
                      textbook on the use of namespaces and qualified identifiers, especially
                      those in the std namespace.
                      He doesn't need to do any of that, and he may not want to. Why not let
                      him progress at his own rate?
                      >
                      If he wants to write C++, he does. ...
                      No, the C standard headers all work in C++, though some of them have
                      minor modifications. When they are used, the C standard library
                      routines are provided both in the std:: namespace, and are also made
                      available in the global namespace, as if by use of a using-
                      declaration.
                      ... If he wants to avoid unmerciful
                      flames in posting to comp.lang.c++, he does. ...
                      There are idiots in every newsgroup who will flame you without good
                      cause.

                      Comment

                      • Kenny McCormack

                        #12
                        Re: the code of two-way linked list failed to be compiled.

                        In article <gb61hh$dpj$1@r egistered.motza rella.org>,
                        Richard <rgrdev@gmail.c omwrote:
                        ....
                        >Which news servers do you use?
                        >
                        >I see no reason whatsoever for 5 or 6 people to post the exact same
                        >reprimand. The days of servers taking days to sync are over. Read the
                        >responses before throwing in your response. Please.
                        >
                        >Now you use Motzarella from what I can see. That is pretty damn quick. I
                        >saw that OP and the replies ages ago. Why could you not do the same?
                        >
                        >Unnecessary tellings off are worse than spam.
                        You are a mean, sadistic man. You want to deprive CBF, the Loser, and
                        others, of the only semblance of a life they have ever known.

                        Comment

                        • Richard

                          #13
                          Re: the code of two-way linked list failed to be compiled.

                          gazelle@shell.x mission.com (Kenny McCormack) writes:
                          In article <gb61hh$dpj$1@r egistered.motza rella.org>,
                          Richard <rgrdev@gmail.c omwrote:
                          ...
                          >>Which news servers do you use?
                          >>
                          >>I see no reason whatsoever for 5 or 6 people to post the exact same
                          >>reprimand. The days of servers taking days to sync are over. Read the
                          >>responses before throwing in your response. Please.
                          >>
                          >>Now you use Motzarella from what I can see. That is pretty damn quick. I
                          >>saw that OP and the replies ages ago. Why could you not do the same?
                          >>
                          >>Unnecessary tellings off are worse than spam.
                          >
                          You are a mean, sadistic man. You want to deprive CBF, the Loser, and
                          others, of the only semblance of a life they have ever known.
                          >
                          I simply do not believe the usual suspects when they say they did not
                          see the other tellings off. They are piling in to get their name up in
                          lights again. It's ridiculous. I would also bet a pound to a penny that
                          Vippstar's little post about how to have const members in dynamically
                          allocated structures was "based" on a previous post in that very same
                          thread. There are two many patterns to recognise for it not to have
                          been. All very silly.

                          Comment

                          • Ian Collins

                            #14
                            Re: the code of two-way linked list failed to be compiled.

                            Martin Ambuhl wrote:
                            gw7rib@aol.com wrote:
                            >On 21 Sep, 18:25, Martin Ambuhl <mamb...@earthl ink.netwrote:
                            >>You will want to change your C headers, <stdio.h>, <stdlib.h>,
                            >><string.h>, to be the C++ standard headers <cstdio>, <cstdlib>, and
                            >><cstringbefor e posting there. You woll also want to check your
                            >>textbook on the use of namespaces and qualified identifiers, especially
                            >>those in the std namespace.
                            >>
                            >He doesn't need to do any of that, and he may not want to. Why not let
                            >him progress at his own rate?
                            >
                            If he wants to write C++, he does.
                            No, he doesn't
                            If he wants to avoid unmerciful
                            flames in posting to comp.lang.c++, he does.
                            No, he doesn't

                            Using the C standard headers is common and acceptable practice in C++.

                            --
                            Ian Collins.

                            Comment

                            • Ian Collins

                              #15
                              Re: the code of two-way linked list failed to be compiled.

                              CBFalconer wrote:
                              >
                              You are also polluting this newsgroup with C++ code. Don't do
                              that. C++ is a different language.
                              >
                              Yet again you repeat something that was posted 10 hours before your post.

                              Why?

                              --
                              Ian Collins.

                              Comment

                              Working...