CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: wrong order

  1. #1
    Join Date
    Feb 2005
    Posts
    176

    wrong order

    Im working on an application using a linked list.
    The user enters a number and this is added as a new link in the linked list.
    The user can then decide to display all the data in the links of the linked list.
    If the user enters the following numbers in this order:

    1
    2
    3
    4

    the application will display:

    4
    3
    2
    1

    This is fine, i think, as a linked list works with Last In First Out. (am i right?)

    I can also write the contents to a text file..... and they are written in the order that they are displayed in. That is:

    4
    3
    2
    1

    Now here is my problem, the application can read each number from the text file and re-input them into the linked list, the problem is that they are being entered in the wrong sequence as the application starts from the BOF (ie. 4). After reading from the text file, if i display the contents of the linke dlist, they are shown as:

    1
    2
    3
    4

    This is wrong!!!
    Any ideas please?

  2. #2
    Join Date
    Sep 2004
    Posts
    519

    Re: wrong order

    Provide example code and information regarding what platform.

  3. #3
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: wrong order

    Quote Originally Posted by Nasty2
    This is fine, i think, as a linked list works with Last In First Out. (am i right?)
    Not necessarily, stack is Last In First Out. You can use std::list, and use the reverse interators or can use the member function reverse which reverse the order of the elements.

  4. #4
    Join Date
    Feb 2005
    Posts
    176

    Re: wrong order

    using Visual Studio .NET
    console applcation.

    Thanks Ejaz, but I cannot use the std::list. (not allowed to!!)

    Here is the code for the function used to read from the file:

    Code:
    void linklist::readfile()
    {
    	ifstream myFile("textfile.txt");
    
    	while(myFile)
    	{
    	      int s;
    	      myFile >> s;
    	      this->additem(s);     //adds each number it reads to the linked list
    	}
    
    	myFile.close();
    	cout << "All the data has been read from the file." << '\n';
    	cout << "Press Enter to continue!" << '\n';
    	getch();
    }
    Thanks

  5. #5
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: wrong order

    Is this a class assignment? Anyway, you didn't mention that its single link list (I guess) or double link list. In case of double, you can traverse backwards, or you can provide the functionality like push_front, where you insert the elements at the front side always, or provide the facility to reverse the link list yourself.

  6. #6
    Join Date
    Apr 2004
    Posts
    55

    Re: wrong order

    Add a recursive readnum() function
    Code:
    void linklist::readfile()
    {
    	ifstream myFile("textfile.txt");
            readnum();
    	myFile.close();
    	cout << "All the data has been read from the file." << '\n';
    	cout << "Press Enter to continue!" << '\n';
    	getch();
    }
    void linklist::readnum()
    {
        if(myFile)
        {
            int s;
            myFile>>s;
            readnum();
            this->additem(s);   // Number read first will be added last
        }
    }
    Or instead of the recursive method, instead of adding the new node at the beginning of the list, add it at the end.
    Code:
    node *n = new node;
    n->info = number read from file;
    n->next = NULL;
    node *end = first;    // first (or list) = pointer to beginning of the list
    if(end==NULL)   // If list is empty
        first = n, end = n;
    else
    {
        while(end->next !=NULL)   //Traverse to last node of list
            end = end->next;
       end->next = n;  // Add new node after the last node
    }
    First one will be faster I guess, coz you won't have to traverse to the end of the list each time, but it will consume more memory (you can keep a pointer to end node in 2nd method, but it might make the code more complicated).
    Last edited by BigEvil; April 6th, 2005 at 07:42 AM.

  7. #7
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: wrong order

    Quote Originally Posted by BigEvil
    Add a recursive readnum() function
    Code:
    void linklist::readfile()
    {
    	ifstream myFile("textfile.txt");
            readnum();
    	myFile.close();
    	cout << "All the data has been read from the file." << '\n';
    	cout << "Press Enter to continue!" << '\n';
    	getch();
    }
    void linklist::readnum()
    {
        if(myFile)
        {
            int s;
            myFile>>s;
            readnum();
            this->additem(s);   // Number read first will be added last
        }
    }
    Or instead of the recursive method, instead of adding the new node at the beginning of the list, add it at the end.
    Code:
    node *n = new node;
    n->info = number read from file;
    n->next = NULL;
    node *end = first;    // first (or list) = pointer to beginning of the list
    if(end==NULL)   // If list is empty
        first = n, end = n;
    else
    {
        while(end->next !=NULL)   //Traverse to last node of list
            end = end->next;
       end->next = n;  // Add new node after the last node
    }
    First one will be faster I guess, coz you won't have to traverse to the end of the list each time, but it will consume more memory (you can keep a pointer to end node in 2nd method, but it might make the code more complicated).
    why reinventing the wheel? There is a std::vector, a std::list and a std::stack implementation...

    http://msdn.microsoft.com/library/de...Stackstack.asp
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  8. #8
    Join Date
    Feb 2005
    Posts
    176

    Re: wrong order

    NoHero...... as i mentioned in my previous post, i cannot use any of those implementations....... got to create my own!

    BigEvil....... first example you provided.....

    void linklist::readfile()
    {
    ifstream myFile("textfile.txt");
    readnum();
    myFile.close();
    cout << "All the data has been read from the file." << '\n';
    cout << "Press Enter to continue!" << '\n';
    getch();
    }

    void linklist::readnum()
    {
    if(myFile)
    {
    int s;
    myFile>>s;
    readnum();
    this->additem(s); // Number read first will be added last
    }
    }
    myFile is not identified as it is declared in the readfile function. I declared on top of the .cpp file and it worked fine (is there any other way i can declare this not globally????)

    Otherwise, it worked fine: output was:
    5
    4
    3
    2
    1
    -858993460 <--------- what is this?

    Thanks

  9. #9
    Join Date
    Apr 2004
    Posts
    55

    Re: wrong order

    Quote Originally Posted by Nasty2
    Thanks Ejaz, but I cannot use the std::list. (not allowed to!!)
    he's not allowed to use std::list

  10. #10
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: wrong order

    If you rewrite the german comments you can use the stack I have written in school last year: stack_t.h (Template), stack_c.h (Class for ints), stack_f.h (Functions for C).
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  11. #11
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: wrong order

    -858993460 <--------- what is this?
    An invalid value, it is -1... so it is invalid. You are running into deleted/freed memory boy
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  12. #12
    Join Date
    Apr 2004
    Posts
    55

    Re: wrong order

    myFile is not identified as it is declared in the readfile function. I declared on top of the .cpp file and it worked fine (is there any other way i can declare this not globally????)
    Oops! You can pass it as an argument to readnum()

    Try this... not very sure about it though!
    Code:
     if(myFile)
    {
    int s;
    if(!myFile>>s)   // If this doesn't display anything, remove the !
       return;
    readnum();
    this->additem(s); // Number read first will be added last
    }
    An invalid value, it is -1... so it is invalid. You are running into deleted/freed memory boy
    Or maybe myFile>>s failed Nasty2, show us your code for additem if the above changes don't work

  13. #13
    Join Date
    Feb 2005
    Posts
    176

    Re: wrong order

    to pass myFile as an argument i've got to do the follwong changes right??:

    Code:
    void linklist::readfile()
    {
    	ifstream myFile("textfile.txt");
    	readnum(myFile);
    
    	cout << "All the data has been read from the file." << '\n';
    	cout << "Press Enter to continue!" << '\n';
    	getch();
    }
    
    void linklist::readnum(ifstream filename)
    {
        if(myFile)
        {
            int s;
            if(!myFile >> s)
    	return;
            readnum();
            this->additem(s);   // Number read first will be added last
        }
        myFile.close();
    }
    its giving me an error in the .h file, where i got:
    void readnum(ifstream filename);

  14. #14
    Join Date
    Apr 2004
    Posts
    55

    Re: wrong order

    What's the error?

    Its better to pass it by reference

    void linklist::readnum(ifstream &filename)

    btw, you're accepting it as "filename" and trying to read it from myFile...

    so change the declaration to
    void linklist::readnum(ifstream &myFile)

  15. #15
    Join Date
    Feb 2005
    Posts
    176

    Re: wrong order

    it's telling me syntax error: identifier 'ifstream' and points to this part of the class:

    public:
    void readnum(ifstream &myFile); <--------------

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured