Thread: Dbl Link Problem

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    17

    Dbl Link Problem

    Hi all,

    I am having a problem with a double link list.
    The list is loaded from a file, I got the list to move forward but cant seem to move backward.

    Account.h:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    class Account
    {
    	int age;
    	public:
    		Account(){age=0;};
                    Account(int a){age=a;};
    		int retAge(){return age;};
    		Account *next;
    		Account *prev;
    };
    Code:
    main.cpp:
    
    #include <cstdlib>
    #include <iostream>
    #include <cstring>
    #include "Account.h"
    #include <fstream>
    using namespace std;
    
    void loadFile();
    void saveFile(int);
    
    Account *currP; //current account pointer
    Account *rootP;//root wont change
    
    int main(int argc, char *argv[])
    {
    	saveFile(12);
    	saveFile(13);
    	saveFile(14);
    	loadFile();
    	
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    void saveFile(int yr)
    {
    	fstream sfile("Account.txt",ios::binary|ios::app|ios::out);
    	if(!sfile.is_open()) sfile.open("Account.txt",ios::binary|ios::out);
    	
    	Account nAcnt(yr);
    	sfile.write(reinterpret_cast<char*>(&nAcnt),sizeof(Account));
    	sfile.close();
    }
    
    void loadFile()
    {
    	fstream ofile("Account.txt",ios::binary|ios::in);
    	if(!ofile.is_open()) return ; //
    	
    	rootP=new Account;
    	Account *tempAcnt;
    
            int sizeAcnt=sizeof(Account);
    	ofile.read(reinterpret_cast<char*>(rootP),sizeAcnt);
    	currP=rootP;
    	rootP->prev=0;  //points to nothing
    
            cout<<"Root Next Pointer: " <<rootP->retAge()<<endl; //check output
    
             int count=1;
    	while(!ofile.eof())
    	{
    			tempAcnt=new Account;
    			currP->next=new Account;
    			currP->prev=new Account;
    			cout<<"loop: "<<count<<endl;
    
                            ofile.read(reinterpret_cast<char*>(tempAcnt),sizeAcnt);
    		
    			cout<<"currP-retAge() : "<<currP->retAge()<<endl;
    			currP->prev = currP;
    			currP->next = tempAcnt;
    			
    			currP=tempAcnt;		
    			cout<<"new currP-retAge() : "<<currP->retAge()<<endl;
    			count++;
    	}
    	
    	ofile.close();
    
            currP=rootP;  //reset point to root
    	while(currP->retAge()!=0)
    	{
    		cout<<"current Previous Pointer: "<<currP->retAge()<<endl;
    		currP=currP->next;
    	}
    
    	currP=currP->prev;   //problem is at either this line or the next
    	cout<<currP->retAge();   //line cause returning age cause an error
    
    return ; 
    }
    Last edited by hoangvo; 07-29-2005 at 12:54 AM.

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    17
    I just found out my current point is not moving back at all.
    any reasons or help?

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    rootP is a pointer to an Account object, so it's a variable that can store the address of an Account object. However, it looks to me like you are trying to read an Account object into rootP, which means you are trying to read an Account object into the memory set aside for an address with this line:
    Code:
    ofile.read(reinterpret_cast<char*>(rootP),sizeAcnt);
    Last edited by 7stud; 07-29-2005 at 01:15 AM.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    17
    Quote Originally Posted by 7stud
    rootP is a pointer to an Account object, so it's a variable that can store the address of an Account object. However, it looks to me like you are trying to read an Account object into the memory set aside for an address with this line:
    Code:
    ofile.read(reinterpret_cast<char*>(rootP),sizeAcnt);

    rootP is a pointer to an Account object but was instantiate at:
    Code:
    rootP=new Account
    its in the loadFile() function

    I have no problem moving the current Pointer (currP) starting from root to the last node, but i have a problem moving it back.

    even if i do this:
    Code:
    currP=rootP;
    currP=currP->next;
    currP=currP->prev;
    cout<<currP-retAge();

    if the root's retAge() return 55 and the next node returns 44.
    The code returns 44 instead of 55 which it is suppose to.
    Last edited by hoangvo; 07-29-2005 at 01:09 AM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    It looks to me like you are creating new objects and assigning them to pointers, and then immediately reassigning other objects to the pointers, which is creating memory leaks and makes no sense. In addition, what do you think this line does:

    currP->prev = currP;

    Do you really think that sets the previous pointer to the previous object in the list? Or, does it set the prev pointer to the current object in the list just like a plain English reading of the statement would suggest?

    With linked lists you need to be able to draw pictures. Get a pencil, an eraser, and a sheet of paper, and go through your code line by line. Everytime you create a new Account object draw a box and inside the box write the words "prev" and "next". Underneath the box write the pointer names which were assigned the address of the object, and draw an arrow pointing from the name to the box. When you assign an object to the prev or next pointers inside the box, draw an arrow that starts at "prev" or "next" and points to the object. Anytime you assign an object to a pointer, erase any old line and draw a new line from that pointer to the object.
    Last edited by 7stud; 07-29-2005 at 02:28 AM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You don't need a return; statement at the end of a void function.

    I see a new, but no delete . . . .

    I got the list to move forward but cant seem to move backward
    Maybe your backwards links are incorrect?

    In loadFile(), should next be set to zero like prev?
    Code:
    rootP->prev=0;
    Just suggestions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    17
    i did a few test and figure it out:

    here is the new test loadFile():
    Code:
    void loadFile()
    {
    	fstream ofile("Account.txt",ios::binary|ios::in);
    	if(!ofile.is_open()) return ; //1=no file exist
    	
    	rootP=new Account; //allocate memory for Account class info
    	Account *tempAcnt;
    
    	//get root first
    	int sizeAcnt=sizeof(Account);
    	ofile.read(reinterpret_cast<char*>(rootP),sizeAcnt);
            
            //point to first node , root node
    	currP=rootP;
    
    	cout<<"Root Next Pointer: " <<rootP->retAge()<<endl;
    	currP->prev=0;
    
    	Account *n; //pointer to account type to hold currP's address before pointing to new node
    
             int count=1;
    	while(!ofile.eof())
    	{
    			tempAcnt=new Account;
    			cout<<"loop: "<<count<<endl;
    			ofile.read(reinterpret_cast<char*>(tempAcnt),sizeAcnt);
    			currP->next = tempAcnt;
    			n=currP;  //hold the address of the current pointer
    			currP=tempAcnt;  //point current pointer to new node
    			currP->prev=n;  //since in new node point prev to n(prev node)
                            count++;
    
    	}
    	
    	ofile.close();
    	
    	while(currP->prev!=0)
    	{
                   currP=currP->prev;
    		cout<<"current Previous Pointer: "<<currP->retAge()<<endl;
    		
    	}
    
    	cout<<"retAge should be 1 :"<<currP->retAge()<<endl;
    	system("pause");
    	return ;  //-1=success
    }

    Thanks for the idea of drawing it out, I did a paintbrush drawn picture of root node, current pointer, temporary node and realize i need another pointer to hold the address of the current node. This allows me to move the current pointer to the temporary node(new node)

    this is the previous code:
    Code:
    currP->prev=currP;
    which works fine except that when I did
    Code:
     currP=tempAcnt;
    i reset the currP->prev to the new (tempAcnt->prev) which is just created and can be anywhere.

    so i added
    Code:
    n=currP
    to hold the address of the current node and move the current pointer to the new node and set the prev of the new node to the address of n.


    anyway ranting on, after fiddling around I manage to figure it out
    thnx again =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List problem
    By Darkinyuasha1 in forum C Programming
    Replies: 2
    Last Post: 05-08-2009, 07:47 PM
  2. destructor with a link list
    By Bones in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2003, 12:01 PM
  3. Undefined Structure in Link List
    By _Cl0wn_ in forum C Programming
    Replies: 1
    Last Post: 03-22-2003, 05:57 PM
  4. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM
  5. Link list and node problem
    By tom_mk in forum C++ Programming
    Replies: 0
    Last Post: 02-21-2002, 05:18 PM