Linked lists and file i/o, and some other stuff

This is a discussion on Linked lists and file i/o, and some other stuff within the C++ Programming forums, part of the General Programming Boards category; Hereīs the deal: Iīve been working on a project recently that heavily depends on linked lists and to be able ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    30

    Linked lists and file i/o, and some other stuff

    Hereīs the deal: Iīve been working on a project recently that heavily depends on linked lists and to be able to save and load them. What makes the matter more complex is the fact that I want to load the list in to an allready existing list, that is first to be deleted (I donīt really know too much about lists and how to optimize them). All of the code fragments below are parts of different functions.

    This is how I delete the list and make it ready to be reloaded (probably filled with stuff that does not need to be there and perhaps even some real errors):

    Node *tmp=head;
    Node *tmp2;

    if(tmp->nxt!=NULL)
    {
    while(1)
    {
    while(tmp->nxt!=NULL)
    tmp=tmp->nxt;
    tmp2=tmp;if(tmp2==first)goto enddelete;
    tmp=first;
    while(tmp->nxt!=tmp2)
    tmp=tmp->nxt;
    delete tmp2;
    tmp->nxt=NULL;
    }
    }
    enddelete:
    delete head;
    head=new Node;
    head->nxt=NULL;
    tmp=head;

    This is what I use to load the list:

    head=new Node;
    head->nxt=NULL;
    Node *tmp=head;

    while(!level.eof())
    {
    level>>tmp->var1;level>>tmp->var2;
    tmp->nxt=new Node;
    tmp=tmp->nxt;
    tmp->nxt=NULL;
    }

    And this is how I use the list (where i KNOW there are errors, just canīt figure out how to fix them):

    Node *tmp=head;
    temp=bignning;
    while(tmp!=NULL)
    {
    while(tmp->var1!=temp->int1||temp->var!=temp->int2)
    {
    temp=temp->next;
    }
    /* here is quite a bit of code that is not relevant, I know it does what itīs
    supposed to, so it doesnīt really matter. */

    tmp=tmp->nxt;
    }

    temp, int1 and int2 are parts of another linked list, which I know to work. This entire sequense of code is used to get to the matching spot in the two linked lists.

    Something in that last code fragment makes the .exe crash. Please help me fix this!
    Any help will be welcomed, both help with the real problem and optimizing of code.

    //Ninja

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    HINT: when you work with linked lists and saving it to a file, you shall use a so called "rule of thumb", being,

    - You save only the data portion of the list, to the file, and when you want to restore it you indulge into that file, pull your data, create a new list and insert your data into a node, one by one....



    matheo917......

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    30

    Uuhh...

    Wasnīt that what I did? I saved the data, then I loaded the data in to a deleted(?), fresh(?) list. You mean I should create an entirely new list instead of loading it in to an existing one? But I need to use the old one, since I will use that function a great deal of times...

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    what if the program's execution terminates? ....??? hmm
    will you be still using an old list?

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    30

    No...

    ...of course i wont, assuming thereīs no way to work around it, which there must be since Iīve "renewed" other lists... only they were much more simply built, always the same length and such. Are sure I have to create a new list?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,707
    No offence, but that there's some UGLY code! Anyway, here's what I'd do:
    (1) Never use the head for data!! Always leave it data-empty.
    (2) If this is a C++ class, then the initializing of new objects should be done in the constructor.
    (3) If this is NOT a C++ class, then write a function called Init( List *New){} or similar and use it! This will save you from simple errors.

    Here is a way to do what you want:




    Code:
    
    Class Node { public:
    Node *next;
    
    int data;
    
    
    
    Node(){ next = 0, data = 0;}  //...constructor...
    
    
    
    
    //...Load will append to end of list. 
    
    bool Load( char *filename )
    {
     //...open file for reading...
     
     Node *t = this;
     
     while(t->next !=NULL)
     {
       t = t->next;
     } 
    
     while(!level.eof())
    {
     Node *born = new Node;
     
     if(born == NULL)
     //...couldn't allocate, close file and return false...
    
    
     //...read in data...
     
     t->next = born;
     t = t->next;
     }
    
    //...close file and return true...
    }
    
    
    
    
    void Kill()
    {
      Node *t = this;
      
      if( t->next == NULL)
      return;
    
      t = t->next;
    
      while(t->next != NULL)
      {
        loser = t; 
        t = t->next;
        delete loser;     
      }
     delete t;
    }
    };
    
    
    
    
    
    
    int main()
    {
     Node head;
    
     head.Load("myfile.dat");
    
     head.Kill();
      
     head.Load("another.dat"); 
    
     head.Kill();
     
     return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    30

    Thanks...

    ...and I know my old code was ugly. Iīm kond of new to linked lists and such, and as if that wasnīt enough, most of it Iīve just sort of guessed (I never looked at how to do anything but the basics, the rest Iīve figured out for myself as I needed it). Anyway, thanks for your help!

    /Ninja

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    30

    Btw...

    ...What exactly does 'Node *t=this;' do? Iīve never seen 'this' used before...
    Is it the same as getting to the beginning of the list?

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    30

    Iīve tried to modify my code to what was suggested,

    but the program crashes when I start it.
    It uses a linked list successfully once, but then it isnīt reseted to the start when next I use it, so I get a nasty error where the program forever tries in vain to raech a node it has allready past.
    So how do I reset to the beginning of the list? I assumed from the example above that...
    Code:
    Node *t=this;
    ...did just that.
    Am I wrong and thatīs whatīs wrong with my code?
    Or am I right and thereīs something completely different thatīs wrong?

  10. #10
    ĄAmo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Kinda busy right now so I can't explain it to u. Look up the "this pointer" for the information.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21