2 way List

This is a discussion on 2 way List within the C Programming forums, part of the General Programming Boards category; I have a list, having nodes with only int an a pointer to the next node. I want to make ...

  1. #1
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142

    2 way List

    I have a list, having nodes with only int an a pointer to the next node.
    I want to make my list 2 way (meaning that i need to add one more pointer to the previous node).
    This is my try for one way
    Code:
    struct AkeraiosStruct {
               int Value;
               AkeraiosStruct * next;
    } AkeraiosStruct;
    
    void insert_at_end (AkeraiosStruct * * ptraddr, int v)
                           /* Insert v as last element of list *ptraddr */
                           
    { 
        AkeraiosStruct * templist;
        templist = * ptraddr;
        while (*ptraddr != NULL)                     /* Go to end of list */
        ptraddr = &((*ptraddr)->next);/* Prepare what we need to change */
        *ptraddr = malloc(sizeof(struct AkeraiosStruct)); /* Space for new node */
        (*ptraddr)->Value = v;                               /* Put value */ 
        (*ptraddr)->next = NULL;              /* There is no next element */
    }
    
    
    void insert_at_start(AkeraiosStruct * *ptraddr, int v)
                          /* Insert v as first element of list *ptraddr */
    { AkeraiosStruct * templist;
      templist = *ptraddr;                /* Save current start of list */
      *ptraddr = malloc(sizeof(struct AkeraiosStruct)); /* Space for new node */
      (*ptraddr)->Value = v;                               /* Put value */
      (*ptraddr)->next = templist;      /* Next element is former first */
      
    }
    And this is my try for two way list
    Code:
    struct AkeraiosStruct {
               int Value;
               AkeraiosStruct * next;
               AkeraiosStruct * prev;
    } AkeraiosStruct;
    But i can't modify insert_at_start/end in order to add the prev pointer.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by ch4 View Post
    But i can't modify insert_at_start/end in order to add the prev pointer.
    So how do you expect to do this if you can't edit the code?

  3. #3
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    I do edit my code but it fails
    Code:
    void insert_at_end (AkeraiosStruct * * ptraddr, int v)
                           /* Insert v as last element of list *ptraddr */
                           
    { 
        AkeraiosStruct * templist;
        AkeraiosStruct * templist_prv;
        templist = * ptraddr;
        
        while (*ptraddr != NULL)                     /* Go to end of list */
        ptraddr = &((*ptraddr)->next);/* Prepare what we need to change */
    
    
        templist_prv=&((*ptrddr);  
    
        *ptraddr = malloc(sizeof(struct AkeraiosStruct)); /* Space for new node */
        
        (*ptraddr)->Value = v;                               /* Put value */ 
    
        (*ptraddr)->next = NULL;              /* There is no next element */
    }
    
    void insert_at_start(AkeraiosStruct * *ptraddr, int v)
                          /* Insert v as first element of list *ptraddr */
    { AkeraiosStruct * templist;
       AkeraiosStruct * templist_prv;
      templist_prv=(*ptraddr)->prev;  /*Save current prev node*/
      templist = *ptraddr;                /* Save current start of list */
      *ptraddr = malloc(sizeof(struct AkeraiosStruct)); /* Space for new node */
      (*ptraddr)->Value = v;                               /* Put value */
      (*ptraddr)->prev=templist_prv;
      (*ptraddr)->next = templist;      /* Next element is former first */
      
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 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. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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