Thread: Double Linked List

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    Double Linked List

    This is my code for double linked list.
    The pointer next is OK
    I have big problem in creating the prev pointer.

    Take a look:
    Code:
    struct AkeraiosStruct {
               int Value;
               
               AkeraiosStruct * next;
               AkeraiosStruct * prev;
    } AkeraiosStruct;
    
    //--------------------------------------------->
    
    void insert_at_end (AkeraiosStruct * * ptraddr, int v)
                           /* Insert v as last element of list *ptraddr */
                           
    { 
        AkeraiosStruct * templist,prevnode;
        templist = * ptraddr;
        
        while (*ptraddr != NULL){                     /* Go to end of list */
                                 ptraddr = &((*ptraddr)->next);                /* Prepare what we need to change */
                                 if(((*ptraddr)->next)==NULL)
                                                           prevnode = *ptraddr;
                                 }
        
        *ptraddr = malloc(sizeof(struct AkeraiosStruct)); /* Space for new node */
        (*ptraddr)->Value = v;                               /* Put value */ 
        (*ptraddr)->next = NULL;              /* There is no next element */
        (*ptraddr)->prev = prevnode;
    }
    
    
    void insert_at_start(AkeraiosStruct * *ptraddr, int v)
                          /* Insert v as first element of list *ptraddr */
    { AkeraiosStruct * templist,prevnode;
    
      prevnode = &((*ptraddr)->prev);
      
      templist = *ptraddr;                /* Save current start of list */
      *ptraddr = malloc(sizeof(struct AkeraiosStruct)); /* Space for new node */
      
      prevnode = &(ptraddr);
      
      (*ptraddr)->Value = v;                               /* Put value */
      (*ptraddr)->next = templist;      /* Next element is former first */
      (*ptraddr)->prev = NULL;
      
    
    }
    Can anyone tell me the exact problem?????

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Code:
    struct AKERAIOSSTRUCT {
               int Value;
               
               AKERAIOSSTRUCT* next;
               AKERAIOSSTRUCT* prev;
    };
     
    AKERAIOSSTRUCT AkeraiosStruct;
    will probably produce better results. its generally considered proper to use all caps when defining a struct type. plus your are using pointers all wrong.
    Code:
    (*ptraddr)->Value = v;
    shoudl be
    Code:
    ptraddr->Value = v;
    unless you are intending to pass the function a pointer to a pointer to a struct, in which case you should already understand pointers before trying a double redirect
    Last edited by abachler; 06-19-2007 at 12:20 PM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    its generally considered proper to use all caps when defining a struct type
    never so it...
    shoudl be
    It shouldn't
    unless you are intending to pass the function a pointer to a pointer
    According to the function prototype - it is so.

    Can anyone tell me the exact problem?????
    YOU should the exact problem and we will try to show the path to solve it...

    How you intend to use your function
    What you want to achieve
    What is the actual results...

    some sample code with description what goes wrong will help
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    "prevnode" isn't declared as a pointer; It doesn't have a star in front of it. That would make a huge dent in your program's ability to perform the desired functions.

    abachler: You are mostly alone in your usage of all capitals for struct definitions. Most people only use all capitals for macros.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    I want to use these two functions in order to insert elements in each node of the struct.
    In every node i will place only one number < 10.
    Generaly i want to do operation for very huge numbers using lists.
    e.x.

    3541482438438443484

    in the first node i will place number 3
    in the second node i will place number 5
    ...
    in the last node i will place number 4

    So far using single linked list i achieved to do the operations of sum difference and i hope, using double linked list to achieve multiply and division digit to digit.
    THe problem is i want to move forward and backward but i only can move forward.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by iMalc View Post
    abachler: You are mostly alone in your usage of all capitals for struct definitions. Most people only use all capitals for macros.
    mostly, unless you also count everyone at microsoft, and 90&#37; of professional programmers.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Microsoft types everything in caps, so they don't count.

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by MacGyver View Post
    Microsoft types everything in caps, so they don't count.
    and linux people write obfuscated code, so lets not count them either, hmm, who do we have left, just me and you and a dog named boo, so I suppose it depends on the dog

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Bah, who ever said Microsoft employees were people!

    Microsoft even go as far as to define LONG long etc, just so that they can do their stupid over-uppercasing. Doesn't make them right, and doesn't mean most people will follow suit.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by abachler View Post
    and linux people write obfuscated code, so lets not count them either, hmm, who do we have left, just me and you and a dog named boo, so I suppose it depends on the dog
    Obviously a Linux dog. If it was a Microsoft dog, it would be named BOO. if it's was a Mac dog, it would be named SomeIntelligentAndCoolMacDogThatIsNamedBoo.


  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by ch4 View Post
    I want to use these two functions in order to insert elements in each node of the struct.
    In every node i will place only one number < 10.
    Generaly i want to do operation for very huge numbers using lists.
    e.x.

    3541482438438443484

    in the first node i will place number 3
    in the second node i will place number 5
    ...
    in the last node i will place number 4

    So far using single linked list i achieved to do the operations of sum difference and i hope, using double linked list to achieve multiply and division digit to digit.
    THe problem is i want to move forward and backward but i only can move forward.
    That is a horrendously inefficient way to implement a large number library. If you need a quick working solution I can point you towards a few existing solutions. However, I suggest you keep going with what you've got as you obviously still have much more to learn from doing this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked List Problem
    By Shiggins in forum C++ Programming
    Replies: 4
    Last Post: 03-10-2009, 07:15 AM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM