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

Discussion in 'C Programming' started by yang2006, Sep 21, 2008.

  1. yang2006

    yang2006 Guest

    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(NULL){}
    ~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",cur->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;
    }
     
    yang2006, Sep 21, 2008
    #1
    1. Advertisements

  2. yang2006

    osmium Guest

    :

    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

    #if 0
     
    osmium, Sep 21, 2008
    #2
    1. Advertisements

  3. No, you're using Dev-C++
    C doesn't have class
    nor friend or public
    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.
    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.
    Here it crashes. It does not with above mentioned fix...
    Bye, Jojo
     
    Joachim Schmitz, Sep 21, 2008
    #3
  4. The above (and most of what follows) is not C, but C++, a different
    language. You need to post C++ questions in < 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
    <cstring> before posting there. You woll also want to check your
    textbook on the use of namespaces and qualified identifiers, especially
    those in the std namespace.
     
    Martin Ambuhl, Sep 21, 2008
    #4
  5. Not with a C compiler. Don't mislead the clueless.
     
    Martin Ambuhl, Sep 21, 2008
    #5
  6. yang2006

    Richard Guest

    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.
     
    Richard, Sep 21, 2008
    #6
  7. yang2006

    gw7rib Guest

    He doesn't need to do any of that, and he may not want to. Why not let
    him progress at his own rate?
     
    gw7rib, Sep 21, 2008
    #7
  8. yang2006

    CBFalconer Guest

    prev(p), next(n){}+
    You are also polluting this newsgroup with C++ code. Don't do
    that. C++ is a different language.
     
    CBFalconer, Sep 22, 2008
    #8
  9. yang2006

    jameskuyper Guest

    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.
    There are idiots in every newsgroup who will flame you without good
    cause.
     
    jameskuyper, Sep 22, 2008
    #9
  10. 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.
     
    Kenny McCormack, Sep 22, 2008
    #10
  11. yang2006

    Richard Guest

    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.
     
    Richard, Sep 22, 2008
    #11
  12. yang2006

    Ian Collins Guest

    No, he doesn't
    No, he doesn't

    Using the C standard headers is common and acceptable practice in C++.
     
    Ian Collins, Sep 22, 2008
    #12
  13. yang2006

    Ian Collins Guest

    Yet again you repeat something that was posted 10 hours before your post.

    Why?
     
    Ian Collins, Sep 22, 2008
    #13
  14. All of what you say is, of course, completely accurate and on-the-money
    in terms of its analysis of the personalities here.

    But it is still just so mean and nasty to be depriving these shut-ins of
    their one chance at glory.
     
    Kenny McCormack, Sep 22, 2008
    #14
  15. It's the only life he will ever know.
     
    Kenny McCormack, Sep 22, 2008
    #15
  16. yang2006

    Richard Guest

    If you look at the off topic rant replies you will see the same people
    doing it time and time again. And they are simply lying in most cases
    when they say their news servers did not get the previous replies. I'm
    glad to see that a few of the regs have stopped doing to as
    often. Default User being a particularly bad offender has finally seen
    the light I think.
     
    Richard, Sep 22, 2008
    #16
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.