Thread: comment on my comments

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    41

    comment on my comments

    /* can someone please check my comments as i'm new to linked lists, I think they */
    /* are all correct and any other comments would be appreciated */

    /* also I think we have'nt really gained anything here and linked lists should really */
    /* be used for when we don't know how much input will be required */

    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>

    struct node{ /* char array 20 elements 0 to 19 = 20, the nineteeth element */
    char word[20]; /* should never be accessed because its reserved for */
    struct node *next; /* 0\' therefor 19 accessable elements */
    }; /* structure pointer */

    int main()
    {
    struct node *node1,*node2; /* structure pointers */

    node1=(struct node *)malloc(sizeof(struct node)); /* allocate memory for the nodes */
    node2=(struct node *)malloc(sizeof(struct node));

    if(node1==NULL||node2==NULL) /* check the memory is ok */
    exit(1);

    strcpy(node1->word,"anthony"); /* copy anthony into the array of the structure */
    printf("%s",node1->word); /* pointed to by the structure pointer node1 */

    node1->next=node2; /* node1 structure pointer next is assigned the address of */
    /* of the first element of the second structure */

    node2->next=NULL; /* last/final node must always point to NULL */

    free(node1); /* free node memory */
    free(node2);
    getch();

    return 0;
    }

    /* THANKS for your time to help me out */

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Only thing I would change is:
    node1->next=node2; /* node1 structure pointer next is assigned the address of */
    /* of the first element of the second structure */
    To this:
    Code:
    node1->next=node2; /* first nodes' next pointer is assigned the address of */ 
    /* of the second node */
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    41
    /* isn't the address of the second node/structure the address of the first element of that node */

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Yes, but it would seem to me that saying it the way you suggested, that the pointer isn't a pointer to a node but rather an element of a node. For me, I would rather have it the other way, saying that one node's pointer member points to the next node makes the point that this is a linked list and not something else just a little bit clearer in my mind. Anyways, I guess its all just a matter of semantics, choose whatever one you want.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Moreover, this is not really a linked list as such. Truly, a linked list(LL) is a method for easy traversal of some data and also a 'dynamic' mechanism which allows the addition and removal of containers as needed. However, unlike arrays, the manipulations of these structures requires that the programmer write new functions for each new LL type. And it is the functions that are first in order for development. You will need:
    1) A 'new' function(a thin encapsulation of malloc)
    2) A 'delete' function(you must dealloc not just for the structure, but for any char *'s you may have. Plus you must reconnect the list where the node is deleted)
    3) An 'addnode' function(to connect it to the existing list)
    4) One or more 'sort' functions(sort by some key)
    5) One or more 'search/find' functions(find by key)

    Many more and useful functions are possible, but those are the minimum.

    Incidentally, there is one way to use 'generic' functions, but there's a catch. If you write all your functions to handle a structure type 'Node', say, you can use them with any ONE structure if you make this declaration:

    Pretend you have a structue called 'People':

    typedef struct People
    {
    People *prev;
    char *name;
    int age;
    People *next;
    };

    Then after that declaration, do this:

    typedef People Node;

    NOW, so long as you do not try to typedef another structure as type 'Node' you can use the generic functions you wrote. Again you are limited to ONE type "Node" in each program.
    I hope what I've written is helpful. Good luck and enjoy using Linked Lists, the most powerful container of them all!
    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;
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This is a prime example of over commenting to the point of confusion. Most of your comments are redundant because the code says the same thing very obviously. There are several things you could do to get rid of the comments in this program, but here are my suggestions. Note that I'm adding a lot more comments than I normally would to show you some key points.
    Code:
    #include<stdio.h> 
    #include<stdlib.h> 
    #include<string.h> 
    
    /* It's fairly obvious what this struct is and does
    ** so why bother commenting it?
    */
    struct node{ 
      char word[20]; 
      struct node *next;
    };
    
    int main() 
    { 
      struct node *dummyHead, 
                  *list;
    
      /* You really only need to malloc one pointer here
      ** since you just assign the other one later on
      */
      dummyHead = malloc( sizeof *dummyHead );
    
      /* Don't ever just end the program if it breaks, let 
      ** the user know that something went wrong first
      */
      if( dummyHead == NULL ) {
        printf( "ERROR: Insufficient memory!" );
        return EXIT_FAILURE;
      }
    
      strcpy( dummyHead->word, "anthony" );
      dummyHead->next = NULL;
    
      /* Build the list with only two nodes to test it
      ** so a loop is not required
      */
      list = dummyHead;
      list->next = malloc( sizeof *list );
      list = list->next;
      strcpy( list->word, "Cassandra" );
      list->next = NULL;
    
      /* Print the list
      */
      list = dummyHead;
      while( list != NULL ) {
        printf( "%s\n", list->word );
        list = list->next;
      }
    
      /* There is a problem here if you can see it.
      ** What happens to the rest of the list if you
      ** have more than two nodes? Only the dummy
      ** node and the last node in the actual list
      ** get freed with this code. Oops? You need to
      ** iterate through the list and free every node
      */
      free(dummyHead); 
      free(list); 
    
      return EXIT_SUCCESS; 
    }
    Sebastiani:
    >And it is the functions that are first in order for development.
    Not if you're just learning linked lists, modularizing it at this point would just make things even more confusing since a simple linked list really only need take up about 17 lines and still do something useful.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Not if you're just learning linked lists, modularizing it at this point would just make things even more confusing since a simple linked list really only need take up about 17 lines and still do something useful.
    I disagree. The functions allow the programmer to step back and see the bigger picture. For one, in a linked list, the 'head' must have a higher scope than the node 'spawner'. Without functions, this can become a mess of code and frankly MUCH more confusing than your '17 lines of code'...
    Look:

    Node *head = NULL;
    do
    {
    clrscr();
    printf("Please enter another employee or type 'quit' ");
    fgets(input, 100, stdin);
    if(strstr(input, "exit"))
    break;

    Node *born = NewNode(); // declared locally
    AddNode(born, head);
    strncpy(born->name, input, 100)
    }while(1);

    clrscr();

    PrintNames(head);

    DeleteList(head);

    return 0;

    As you can see, the functions allow a cleaner abstraction.
    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;
    }

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >As you can see, the functions allow a cleaner abstraction.
    There's really no need for abstraction when your program is so small. Granted, abstraction and modularization are very important in professional programs but toy programs for learning shouldn't use concepts that hide the overall structure of the list.

    A linked list isn't an easy concept to figure out, but stepping through a 'simple' program with simple actions will work wonders for understanding how the concept of a linked list works.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
      int item;
      struct node *next;
    };
    
    int main( int argc, char **argv )
    {
      int i, 
          N = atoi( argv[1] ), 
          M = atoi( argv[2] );
      struct node *t = malloc( sizeof *t ), 
                  *x = t;
      t->item = 1; 
      t->next = t;
    
      for( i = 2, i <= N; i++ ) {
        x = ( x->next = malloc( sizeof *x ) );
        x->item = i; 
        x->next = t;
      }
    
      while( x != n->next ) {
        for( i = 1; i < M; i++ )
          x = x->next;
        x->next = x->next->next;
        N--;
      }
    
      printf( "%d\n", x->item );
    
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    True, I guess you're right as far as grasping the list. Still, when I was learning them, I was quick to write the functions FIRST to avoid mistakes and to automate more quickly.
    Good point, though.
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comments problem with MSVC 2003
    By mikahell in forum Tech Board
    Replies: 7
    Last Post: 09-04-2006, 06:04 PM
  2. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  3. Comment using // or /* &&& */
    By Roaring_Tiger in forum C Programming
    Replies: 3
    Last Post: 03-16-2005, 02:45 PM
  4. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  5. The Art of Writing Comments :: Software Engineering
    By kuphryn in forum C++ Programming
    Replies: 15
    Last Post: 11-23-2002, 05:18 PM