#1
  1. The bad and the ugly...
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2007
    Location
    Oz... No??? Neverland then?
    Posts
    142
    Rep Power
    0

    Circularly linked list woes!


    Code:
    struct phone_book *add(struct phone_book *list, char *lname, char *fname, char *phone) 
    {
    	struct phone_book *current;
    
    	if(list == NULL) 
    	{
    		list = (struct phone_book *)malloc(sizeof(struct phone_book));
    		strcpy(list->lname, lname);
    		strcpy(list->fname, fname);
    		strcpy(list->phone, phone);
    		list->next = NULL;
    		return(list);
    	}
    	else 
    	{
    		current = (struct phone_book *)malloc(sizeof(struct phone_book));
    		strcpy(current->lname, lname);
    		strcpy(current->fname, fname);
    		strcpy(current->phone, phone);
    		current->next = list;
    		return(current);
    	}
    }
    assuming this is my add function. would this call to it not read from a file into a linked list?

    Code:
    current = head;
    do{
    	current = add(current, last_name_buffer, first_name_buffer, phone_number_buffer);
    	current = current->next;			
    }
    while(fscanf(filename, "%s %s %s\n", last_name_buffer, first_name_buffer, phone_number_buffer) != EOF);
    "Life is not a journey with the intent on arriving at the finish line in a pretty and well preserved body. But rather to skid in broadside, totally worn out, thoroughly used up and loudly proclaiming, "Wow! What a ride!" -Anonymous
    Halo! || Diablo 2 LOD Modding || OLGA's BACK!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Assuming your phone book is the same as your previous post, but without looking it up, and recognizing that you didn't rewrite add as I had suggested:

    The title of the post suggests circular linked list. Your add function deals with only one list pointer, so I suppose its just the same stack as before, and not some more exotic structure.

    And your code

    do use_the_data_that_I_just_read();
    while (read_some_data_and_test_success());

    won't work. A for loop might work:
    Code:
    for (read_some_data(); test_success(); read_some_data())
      use_the_data_that_I_just_read();
    although I suppose you'll more commonly find a for loop used for array iterations, and people would write
    Code:
    for read_some_data();
    while(test_success()) {
      use_the_data_that_I_just_read();
      read_some_data();
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. The bad and the ugly...
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2007
    Location
    Oz... No??? Neverland then?
    Posts
    142
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Assuming your phone book is the same as your previous post, but without looking it up, and recognizing that you didn't rewrite add as I had suggested:

    The title of the post suggests circular linked list. Your add function deals with only one list pointer, so I suppose its just the same stack as before, and not some more exotic structure.

    And your code

    do use_the_data_that_I_just_read();
    while (read_some_data_and_test_success());

    won't work. A for loop might work:
    Code:
    for (read_some_data(); test_success(); read_some_data())
      use_the_data_that_I_just_read();
    although I suppose you'll more commonly find a for loop used for array iterations, and people would write
    Code:
    for read_some_data();
    while(test_success()) {
      use_the_data_that_I_just_read();
      read_some_data();
    }

    no i didn't make any changes because i didn't understand what you meant by your last post. I got it running and turned in. now I'm confused about how to turn this singularly linked list into a circularly linked list.

    Code:
    while(fscanf(filename, "%s %s %s\n", last_name_buffer, first_name_buffer, phone_number_buffer) != EOF)
    		{
    			head = add(head, last_name_buffer, first_name_buffer, phone_number_buffer);
    		}
    	for(current = head; current != NULL; current = current->next)
    		n++;
    	current->next = head;
    once i've built my list, the only problem is the last node, it's 'next' pointer is NULL. With that snippet i tried working back through the list to the end, then assigning next to head, but it crashes. any idea why?
    "Life is not a journey with the intent on arriving at the finish line in a pretty and well preserved body. But rather to skid in broadside, totally worn out, thoroughly used up and loudly proclaiming, "Wow! What a ride!" -Anonymous
    Halo! || Diablo 2 LOD Modding || OLGA's BACK!
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    222
    Rep Power
    236
    but it crashes. any idea why
    Sure, you're dereferencing a NULL pointer.
    Why have you decided that you need a circular linked list?
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    A circular list


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define NODE struct Node       /* a->b->a        insert  c after a  */
    NODE {			       /* a->c->b->a     desired configuration */
      void*datum;		       /* a->b, c->b->a  first make c point to b */
      NODE*next;		       /* a->c   ->b->a  then have a->c */
    };
    
    NODE*addNode(NODE*node,void*datum) {
    
      /* addNode: returns pointer to the new node which inserted after node. */
      /* Pass NULL for node to start a new circular list */
    
      NODE*new_node = malloc(sizeof(NODE)); /* always need to allocate space */
      if (NULL == new_node) { /* always test success to avoid unacceptable core dump */
        fputs("\nout of memory.  Quit isn't always best choice.\n",stderr);
        exit(EXIT_FAILURE);
      }
      new_node->datum = datum;	/* always need to store the data */
    
      /* Having factored out the parts that always need to occur, */
      /* next construct the circular list. */
    
      /* identify new circular list */
      if (NULL == node) /* note that (node == NULL) is worse style (because of common hard-to-spot typo node = NULL) */
        new_node->next = new_node; /* circular list.  First node points to itself */
      else {
        new_node->next = node->next; /* "a->b, c->b->a  first make c point to b" */
        node->next = new_node;	 /* "a->c   ->b->a  then have a->c" */
      }
      return new_node;		/* return the new node */
    }
    
    NODE*next(NODE*node) {		/* hide the details in a function */
      if (NULL == node)		/* working code shouldn't know the details of the struct node */
        return NULL;
      return node->next;
    }
    #define NEXT(A) (NULL==(A)?NULL:(A)->next) /* hide the details in a macro */
    #define DATUM(A) (NULL==(A)?NULL:(A)->datum) /* hide details in a macro */
    
    void cycle(void (*f)(void*datum,void*extra_data),NODE*head,void*extra_data) {
      NODE*node;
      if (NULL == head)
        return;
      node = head;
      do (*f)(DATUM(node),extra_data); while (head != (node = NEXT(node)));
    }
    
    void map(void (*f)(void*datum,void*extra_data),NODE*head,int counts,void*extra_data) {
      int i;
      NODE*node;
      if (NULL == head)
        return;
      for (i = 0, node = head; i < counts; ++i, node = NEXT(node))
        (*f)(DATUM(node),extra_data);
    }
    
    void display(void*datum,void*extra_data) {
      if (NULL != datum)
        putchar(*(char*)datum);
    }
    
    int main() {
      NODE*head = NULL;
      char*pc,*data = "this string will save me from having to use malloc";
      puts("circularly linked list demonstration");
      for (pc = data; 'r' != *pc; ++pc) {
        puts("\ndisplay 7 characters");
        map(display,head,7,NULL), putchar('\n');
        puts("show 1 cycle");
        cycle(display,head,NULL), putchar('\n');
        printf("advance 3 positions, insert `%c'\n",*pc);
        head = next(next(next(head)));
        head = addNode(head,(void*)pc);
      }
      return EXIT_SUCCESS;
    }
    
    
    
    
    //NODE*addNode(NODE*n,void*datum) {
    //  NODE*m = malloc(sizeof(NODE)); /* always need to allocate space */
    /// this was not worthwhile, you will not follow this code either.
    //  if (NULL == m) {		 /* always test success to avoid unacceptable core dump */
    //    fputs("\nout of memory.  Quit isn't always best choice.\n",stderr);
    //    exit(EXIT_FAILURE);
    //  }
    //  m->a = datum;	     /* always need to store the data */
    //  if (NULL == n)     /* note that (n == NULL) is worse style (because of potential hard-to-spot typo n = NULL) */
    //    m->n = m;	     /* circular list.  First node points to itself */
    //  else
    //    m->n = n->n, n->n = m;	/* connect m after n */
    //  return m;
    //}
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo