link list

This is a discussion on link list within the C Programming forums, part of the General Programming Boards category; Ok, so i have a link list of structs.. but in the wrong direction, is there a way to flip ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    16

    link list

    Ok, so i have a link list of structs.. but in the wrong direction, is there a way to flip the elements in the link list?

    for example

    if have pointer -> 1-> 2 -> 3 ->Null
    but i need it pointer ->3 -> 2 -> 1 -> Null

    sorry if this seems confusing

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    How about: for each node in the linked list (excluding the first node), set next equal to the previous node in the list. For a doubly linked list, this wouldn't be necessary.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,763
    Just do some pointer surgery. Make a new, reversed list by stepping through your old list and attaching nodes after the head node of the new list.

    Code:
    struct ll
    {
      int k;
      ll * next;
     };
    
    struct ll * insert_after ( struct ll * beforenode, struct ll * datanode )
    {
      struct ll * rv = NULL;
      if ( datanode != NULL ) {
        if ( beforenode != NULL ) { 
          beforenode->next = datanode;
        }
        rv = beforenode == NULL ? datanode : beforenode;
      }
      return rv;
    }
    
    struct ll * reverse ( struct ll * headnode )
    {
      struct ll * reverselist = NULL;
      while( headnode != NULL ) {
        reverselist = insert_after( reverselist, headnode );
        headnode = headnode->next;
      }
      return reverselist;
    }
    Last edited by whiteflags; 04-17-2008 at 07:36 PM.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    Awesome, thanks guy i got it but i have another simple question, how would i pass a pointer(to a struct) to a function??
    Code:
    struct node
    { 
    	double coefficient;
    	int power;
    	struct node *next;
    };
    
    main()
    {
    
    struct node *first;
    
    ....code(first will be link)
    
    add(  ???  );
    
    ....code
    
    }
    
    add(  ???  )
    {
    }
    Thanks
    -dyn4sty

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The same way you pass any other variable: by putting its name inside the (parentheses) right after the function name.

    And of course, you have to put the type in the function declaration: struct node *.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    ahh, got it, i'm new to C and still confused on where i should and shouldnt put *'s and &'s. thanks everyone for the help

    -Dyn4sty

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 10:28 PM
  3. reading data from a file - link list
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2006, 10:11 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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