Thread: use with a function

  1. #1
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96

    use with a function

    can some1 help me with understanding the code
    Code:
    struct Edge
    {
      int terminal;
      struct Edge *next;
    };
    
    struct Edge *Insert_Vertex (int , struct Edge *);
    
    struct Edge * Insert_Vertex (int vertex_no, struct Edge *first) {
      struct Edge *new1, *current;
      new1 = (struct Edge *) malloc(sizeof(struct Edge));
      //current = (struct Edge *) malloc(sizeof(struct Edge));
      new1->terminal = vertex_no;
      new1->next = NULL;
      if (!first)
        return (new1);
      else {
        for(current = first; current->next; current = current->next)
          current->next = new1;
        return (first);
      }
    }
    I also put how the Linked List is made.
    thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,736
    How are we supposed to know what you do (or do not) understand about it already?

    You could try commenting the code, and then posting the results.

    Then at least we don't have to worry about things you seem to know already.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96

    Unhappy

    Code:
    struct Edge
    {
      int terminal;
      struct Edge *next;
    };
    
    struct Edge *Insert_Vertex (int , struct Edge *);
    
    struct Edge * Insert_Vertex (int vertex_no, struct Edge *first) { // this function takes verttex_no and pointer to the struct edge
      struct Edge *new1, *current;  // we define 2 new variables with struct Edge
      new1 = (struct Edge *) malloc(sizeof(struct Edge));
      //current = (struct Edge *) malloc(sizeof(struct Edge));
      new1->terminal = vertex_no; // the new1 points to terminal : its gives Vertex_no. basically
      new1->next = NULL; // if the list next is null :means not pointing to anything anymore
      if (!first)      // i dont follow this
        return (new1);
      else {
        for(current = first; current->next; current = current->next) // not even this??????
          current->next = new1;
        return (first);
      }
    }
    I hope I am clear with my question

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    74
    if (!first) is testing to see if the variable first is equal or not to 0 (NULL)
    its equivalent to
    Code:
    if (first == NULL)
    else

    the cicle I don't get it what it is doing, either way, it could had been written as:
    Code:
    for(current = first; current->next != NULL; current = current->next)
    Last edited by noobiept; 08-24-2010 at 11:51 AM.

  5. #5
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    thanks ....
    I actually dont understand why i get error when i run my actual file (i.e 500 elements)


    in Insert_Vertex (vertex_no=334, first=0x0) error at
    for(current = first; current->next==NULL; current = current->next)


    but dont see it when i run some small example file.(10 elements)

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by noobiept View Post
    if (!first) is testing to see if the variable first is equal or not to 0 (NULL)
    its equivalent to
    Code:
    if (first != NULL)
    else

    the cicle I don't get it what it is doing, either way, it could had been written as:
    Code:
    for(current = first; current->next == NULL; current = current->next)
    Those are turned around. !first is equivalent to first == NULL, and current->next in a condition (like in an if statement or loop condition) is equivalent to current->next != NULL.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    74
    sorry, I got it wrong, its actually the other way around. aahh

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    74
    Quote Originally Posted by robwhit View Post
    Those are turned around. !first is equivalent to first == NULL, and current->next in a condition (like in an if statement or loop condition) is equivalent to current->next != NULL.

    yep, was just thinking now and wasn't sounding right. you're right.

  9. #9
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    I still dont follow...
    I understoof very much like noobiept wrote
    so u say it is equivalent to :
    Code:
     if (first== NULL)      // i dont follow this
        return (new1);
      else {
        for(current = first; current->next != NULL; current = current->next) 
          current->next = new1;
        return (first);
      }
    }
    is it ???

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by satty View Post
    I still dont follow...
    I understoof very much like noobiept wrote
    so u say it is equivalent to :
    Code:
     if (first== NULL)      // i dont follow this
        return (new1);
      else {
        for(current = first; current->next != NULL; current = current->next) 
          current->next = new1;
        return (first);
      }
    }
    is it ???
    Your loop is assigning 'current->next' to 'new1' upon *each* iteration. That's not what you want, is it (hint: you're aim should be to get to the end of the list first)? Also, you should probably return the newly created node - not 'first'.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Either add to the head or the tail of the list, the former being the easier of the two options.
    The way for loop is written currently, makes the program end up with lots of dangling pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM