Creating Doubly Linked List Help!!

This is a discussion on Creating Doubly Linked List Help!! within the C Programming forums, part of the General Programming Boards category; Hey Everyone, I'm having trouble with my code. It compiles fine, but when I try to run it, I get ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Creating Doubly Linked List Help!!

    Hey Everyone,

    I'm having trouble with my code. It compiles fine, but when I try to run it, I get an error.

    Insight: I'm trying to read numbers in from a FILE and put those numbers into the doubly linked list.


    Code:
       void CreateList(struct node *lnode, int m, FILE *fp1){
       	
          struct node *head, *tail;
    		
          fscanf(fp1, "%d", &m);
       
          while(!feof(fp1)){
    	
    	 lnode = (struct node *)malloc(sizeof(struct node));
     
             lnode->data = m;
    			
             if(head == NULL)
             {
                head = lnode;
                lnode->back = NULL;
             } 
             else {
                tail->next = lnode;
                lnode->back = tail;
             }
          
             tail = lnode;
             lnode->next = NULL;
    
             fscanf(fp1, "%d", &m);
          }
        	
       }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1. Don't control loops with feof()
    Cprogramming.com FAQ > Why it's bad to use feof() to control a loop

    2. Don't cast malloc
    Cprogramming.com FAQ > Casting malloc

    3. Close files you open.

    4. You are passing your function a pointer lnode and then attempting to change it using malloc.
    This has the same effect as trying to pass an integer and change its value. You need to pass a pointer to a pointer in this case.

    5. Head and tail are local to your function which is probably NOT what you want.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    6. head and tail are used without initialization.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Bayint Naung View Post
    6. head and tail are used without initialization.
    Can't believe I missed that one.

    Well anyway, I think it would be useful if the OP provided us with the rest of the code to comment on as it seems that there are more general issues with it than just this function.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 06:47 PM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. Making a doubly linked list
    By mlupo in forum C Programming
    Replies: 1
    Last Post: 10-16-2002, 09:05 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread


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