Thread: Dbl list

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Question Dbl list

    I am having problems with my doubly linked list. Can anyone help, I get a bus error after the first word is added. Thanks.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define Z 21
    
    struct node  {
        char strg[Z];
        struct node *next, *prev; 
    };
    
    void print_list(struct node *,struct node *);
    void add_word(int *,struct node *, struct node *,struct node *);
    
    /*********************************************************main()******/
    int main()  {
    char user_input='1'; 
    int run=1;
    struct node *first,*current,*previous,*next,*newnode;
    
    first=(struct node *)malloc (sizeof(struct node)); 
    first->next=NULL;
    first->prev=NULL;
    
      while((user_input!='q')&&(user_input!='Q'))  {
          if (run!=1)
            newnode=(struct node *)malloc(sizeof(struct node)); 
          add_word(&run,first,current,newnode);
          user_input='1';
          print_list(first,current);
      }        
    }  
           
    void add_word(int *run,struct node *first,struct node *current,struct node *newnode)  {
    
     if (*run==1)  {
       printf("Enter word to add.\n"); 
       scanf("%s",first->strg);
       current=first;
       *run=0;   
     }
     else { 
    
       printf("Enter word to add.\n"); 
       scanf("%s",newnode->strg);
       newnode->prev=current;
       printf("%s",current->next);
       newnode->next=current->next;
       current->next=newnode;
       current=newnode;
     }
    }
    
    void print_list(struct node *first,struct node *current)  {
    struct node *print_current;
    
     print_current=first;
     printf("\n");
     while (print_current!=NULL)  {
        
        if (print_current==current)
          printf(">");
        printf("%s ", print_current->strg);
        print_current=print_current->next;
      }
      printf("\n\n");
    }
    Last edited by justin69enoch; 02-21-2003 at 11:03 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your list is way too complicated. All you need is one pointer to hold the list, and one pointer for each function that needs to loop.
    Code:
    void add( data_type data )
    {
        struct node *ptr;
        ptr = malloc( sizeof( struct node ) );
        ptr->prev = NULL;
        ptr->next = NULL;
        ptr->data = data;
    
        if( list != NULL )
            list->prev = ptr;
        ptr->next = list;
        list = ptr;
    }
    This code uses a global pointer 'list', which will hold the first node in the list. See how simple that is? Now you could of course skip the global list by passing it a pointer to the list pointer and updating it that way. Either way works.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    53
    I have to have a previous and a next. My code has to be this way due to other functions that work in the program ... is there anyway to fix this? or at least tell me what is going sooo bad wrong .?? thanks.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    53
    can someone at least tell me what is wrong with my code; how to pass the linked list back and forth from main to another function?

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    53
    can someone at least tell me what is wrong with my code; how to pass the linked list back and forth from main to another function?

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    > is there anyway to fix this?

    You could change the other working functions, or you could use the list-pointer to retrieve the previous and next pointers, since a node has a previous and next pointer. Such like this:

    add_word (number, list->prev, list, new_node);

    > how to pass the linked list back and forth from main to another function?

    You don't pass a linked list to functions or back, but a pointer to the list. You should store a pointer to the list and pass that pointer to functions.

    Some other comments:

    Function main should return a value.
    Function main should release the allocated memory.
    Before using nodes, check if malloc returned NULL.

    You don't need variable run, it makes the program too complicated. Realise that if first is NULL, then the list is empty and if first->next is NULL, then first is the only element in the list.

    Your program could be made much less complicated by letting the functions do the work instead of main. The function add_word could receive a pointer to the beginning of the list. Then add_word could search for the end of the list, allocate memory for a new node, initialise the node and add it to the list.

    The function print_list prints from the beginning to a certain node isn't it? You could also transfer complexity to this function by not letting main determine a pointer, but letting main pass a value for strz to the function. The function then prints from the beginning until it found a node which strz-member is equal to the passed strz.
    Last edited by Shiro; 02-22-2003 at 08:42 AM.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    53
    Okay, I have my code working using global variables. Every time I try to simplify this, I either get a Seg fault or a bus error. Mainly, I want to get rid of the global variables before I try to simplify the code. Can anyone show me how to do this? Next, I would like to simplify add_word() with quzah's method because it looks much cleaner and much easier than my code. The problem is, when I have tried to implement this code with mine, it doesn't work right... I currently have the following code doing what I want it to do, if you want to compile and take a look, I would be grateful. thanks for all of the wonderful help you have given thus far and if I get closer before someone posts, I'll post the code again. Thanks again.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define Z 21
    
    struct node  {
        char strg[Z];
        struct node *next, *prev; 
    };
    
    struct node *first,*current,*previous,*next,*newnode;
    void print_list();
    void add_word(int *);
    
    /**************************************************
    *******main()******/
    int main()  {
    char user_input='1'; 
    int run=1;
    
    first=(struct node *)malloc (sizeof(struct node)); 
    first->next=NULL;
    first->prev=NULL;
    
      while((user_input!='q')&&(user_input!='Q'))  {
          add_word(&run);
          user_input='1';
          print_list();
      }        
    }  
           
    void add_word(int *run)  {
    
     if (*run==1)  {
       printf("Enter word to add.\n"); 
       scanf("%s",first->strg);
       current=first;
       *run=0;   
     }
     else { 
    
       newnode=(struct node *)malloc(sizeof(struct node)); 
       printf("Enter word to add.\n"); 
       scanf("%s",newnode->strg);
       newnode->prev=current;
       newnode->next=current->next;
       current->next=newnode;
       current=newnode;
     }
    }
    
    void print_list()  {
    struct node *print_current;
    
     print_current=first;
     printf("\n");
     while (print_current!=NULL)  {
        
    						if (print_current==current)
    							printf(">");
        printf("%s ", print_current->strg);
        print_current=print_current->next;
      }
      printf("\n\n");
    }

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Question RE Please help

    Can someone please help me pass the pointers correctly back and forth from main () to add_word().

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There's some useful info on lists here, if that helps you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The algorithm for add_word could be simplified to this:

    Code:
    function add_word (list_pointer)
    begin
      // allocate memory and initialise new node
      new_node = allocate_memory (size of node)
      if new_node == NULL
      begin
        // could not allocate memory
        return error_value
      end
    
      initialise_node (new_node)
    
      if list_first == NULL
      begin
        // the list is empty, so new node is first node
        list_first = new_node
      end
      else
      begin
        // the list is not empty, so first find last node
        // list_pointer should not be altered, so create temp pointer
        list_pointer = list_first
    
        while list_pointer->next != NULL
        begin
          list_pointer = list_pointer->next
        end
    
        // add new node after last node in the list
        list_pointer->next = new_node
      end
    end

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM