Notices
Results 1 to 7 of 7

Thread: Double linked list: add data to position function problem.

  1. #1 Double linked list: add data to position function problem. 
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    922
    I have a problem with my doubly linked list 'add' function.
    1) database has a head, pointing to the first data in the list.
    2) the argument recordId is the location in the doubly linked list that i wish to insert the node.
    3) curr is a node in database class that basically is used to point to any node just to transverse the class
    4) getMaxId returns 0 if there is no list, 1 if there is item, 2 if two, etc.
    5) 'add' function is supposed to add the new 'AddData' into the node 'n', and then into the position 'recordId'
    6) 'temp' is just a pointer to hold a node so that curr can point to the next, and temp can edit its next to 'n', so that the original 'next' will be curr, and not missing.
    Code:
    void C_Database::AddNode(C_Monster AddData, int recordId){
    //New node 'n' with data:"AddData" is created to be placed into:"recordId"
    	C_Node* n=new C_Node;
    	n->setData(AddData);
    
    
    	if(getMaxId()>0){//If there is a list
    		if(getMaxId()==1){//If there is only one item in the list
    			n->setPrev(NULL);
    			curr=head;
    			n->setNext(curr);
    			curr->setPrev(n);
    			curr->setNext(NULL);
    		}
    		else{//If there are more than one items in the list
    			curr=head;//Make current point to head
    			for(int Id=1;Id==recordId-1;Id++){//Loop until curr is before recordId
    				curr=curr->getNext();
    			}
    		//Current is now before wanted Id
    			n->setPrev(curr);//Set new node to point back to current
    			temp=curr;
    		//Temp is now before wanted Id
    			curr=curr->getNext();//Set current to be the node that will be after n
    		//Current is now at the node that should be after the new node
    			temp->setNext(n);//Set the wanted Id's previous node's next to n
    			if(curr!=NULL){//If there is supposed to be a node after the new node
    				curr->setPrev(n);
    				n->setNext(curr);
    			}
    			else{//If there is no nodes after the new node
    				n->setNext(NULL);
    			}
    		}
    	}
    	else{//If there is no list
    	//Set head
    		head=n;
    		head->setNext(NULL);
    		head->setPrev(NULL);
    	}
    }
    then, i test these in the main.
    Code:
    //These are just some preset data
        mymonster.setHealth(1);
        mymonster.setLevel(10);
        mymonster.setName("one");
        database.AddNode(mymonster,1);
        mymonster.setHealth(2);
        mymonster.setLevel(20);
        mymonster.setName("two");
        database.AddNode(mymonster,2);
        mymonster.setHealth(3);
        mymonster.setLevel(30);
        mymonster.setName("three");
        database.AddNode(mymonster,3);
        mymonster.setHealth(4);
        mymonster.setLevel(40);
        mymonster.setName("four");
        database.AddNode(mymonster,1);
    Code:
    after all these, the printList() shows:
    Record#1
    Name: "one"
    Level: 10
    Health: 1%
    thats all

    help is appreciated


    Last edited by RamenNoodles; January 11th, 2014 at 12:01 AM.
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  2.  
     

  3. #2  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    I think this is a problem:
    Code:
    n->setNext(curr->getNext()->getNext());
    You might want to draw this out on paper with boxes and arrows and walk through how adding a new item changes everything (and how your code does too).


    Reply With Quote  
     

  4. #3  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    922
    okay, i have changed it, and i have a new problem, view edited OP for the changes

    btw thanks for the reply!

    Quote Originally Posted by MagiMaster View Post
    You might want to draw this out on paper with boxes and arrows and walk through how adding a new item changes everything (and how your code does too).
    well, i wrote a pseudo code to do what i want :
    Code:
    if list size 0,
          set head to n,
          n's next is set to null,
          n's previous is set to null.
    
    
    if list size is 1
          set curr to head,
          set n's previous to null,
          set n's next to curr,
          set curr's previous to n,
          set curr's next to null.
    
    
    if list size is above 1,
          set curr to head,
          loop curr until it points to n's previous,
          set n's previous to curr,
          set temp to curr,
          set curr to curr's next,
          set temp's next to n
          if curr is not null,
                set curr's previous to n,
                and n's next to curr.
    *(i edited the OP after applying the psuedo code)*
    Last edited by RamenNoodles; January 10th, 2014 at 11:46 PM.
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  5. #4  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    There are two subcases if there's only 1 item in the list, though both can probably be wrapped into the general case if you write it right.

    There's also a special subcase of the general case if n's previous (or next, depending on how you do things) is null.
    Reply With Quote  
     

  6. #5  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    922
    YES! i found out whats wrong!

    applying pseudo code should have been
    Code:
    if(getMaxId()==1){//If there is only one item in the list
    			n->setPrev(NULL);
    			curr=head;
    			head=n;
    			n->setNext(curr);
    			curr->setPrev(n);
    			curr->setNext(NULL);
    		}
    but the error was that i put
    Code:
    if(getMaxId()==1){//If there is only one item in the list
    			n->setPrev(NULL);
    			curr=head;
    			n->setNext(curr);
    			curr->setPrev(n);
    			curr->setNext(NULL);
    		}
    i didn't set the head to the new node in case 1, and that made all the mistakes.

    thank you very much for your help, i'll post more replies if i have problem, thanks!!
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  7. #6  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    922
    Quote Originally Posted by MagiMaster View Post
    There are two subcases if there's only 1 item in the list, though both can probably be wrapped into the general case if you write it right.

    There's also a special subcase of the general case if n's previous (or next, depending on how you do things) is null.
    although i really cant understand these statements even after trying to read it for more than 10 minutes
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  8. #7  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    If you have one item in the list, AddNode(m, 0) and AddNode(m, 1) should do different things, right? The same general idea applies to lists with more than one element if you try to add a monster to the beginning of the list or the end of the list. (Make sure you test all those cases to keep from getting a nasty surprise later.)
    Reply With Quote  
     

Similar Threads

  1. Windows 7 sleep function problem
    By Bad Robot in forum Computer Science
    Replies: 19
    Last Post: November 22nd, 2013, 03:07 AM
  2. Trigonometric Function Problem
    By Gwiyomi17 in forum Mathematics
    Replies: 2
    Last Post: June 15th, 2013, 05:36 PM
  3. Replies: 1
    Last Post: October 3rd, 2012, 12:21 PM
  4. C++ Template Question regarding a Linked List.
    By collred in forum Computer Science
    Replies: 1
    Last Post: January 2nd, 2010, 12:42 PM
  5. Please hlep me in (Data preparation for data mining )
    By nadia in forum Computer Science
    Replies: 3
    Last Post: December 22nd, 2008, 01:56 PM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •