Linked Lists

This is a discussion on Linked Lists within the C Programming forums, part of the General Programming Boards category; G'ah my c programming needs major improvement. I've created a function that reads information from files and then enters this ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    24

    Linked Lists

    G'ah my c programming needs major improvement. I've created a function that reads information from files and then enters this information in a linked list.

    What I've done is a bit of a mess as I'm not at all confident what I'm doing is correct, in fact I know its wrong. I'll give you the code I've written and then write down what i'm trying to achieve with it.

    Code:
    ListNode *read_vectorfiles(FileNames vectorFile)
    {
    	FILE *fptr;
    	int vectorCount, i=0;
    	ListNode *head=(ListNode *)malloc(sizeof(ListNode));
    	ListNode *node=0;
    	Colour *colour=colour_create();
    	Vector *position=vector_create();
    
    	/*Open the file*/
    	fptr=fopen(vectorFile.fileName, "r");
    
    	fscanf(fptr, "Vector Count: %d\n\n", &vectorCount);
    	
    	node=head;
    
    	for(i=0; i<vectorCount; i++){
    		fscanf(fptr, "position: %f %f %f %f", &position->x, &position->y, &position->z, &position->w);
    		fscanf(fptr, "colour: %f %f %f %f\n", &colour->r, &colour->g, &colour->b);
    		node->next=(ListNode *)malloc(sizeof(ListNode));
    		node->next->colour=(Colour *)malloc(sizeof(Colour));
    		node->next->vector=(Vector *)malloc(sizeof(Vector));
    		node->next=vectorlist_setnode(position, colour);
    		node=node->next;
    	}
    
    	printf("Here is one of the read colours apparently %.2f\n\n", head->next->colour->r);
    
    
    	return(head);
    }
    I'm allocating a whole load of memory - is the allocation correct? and I'm doing this as it reads the relevant details and then I'm sending the newly read data off to a function to set the values.

    The reason i have a node=head in there is because I want to return the head and using the node to loop through the linked list allows me to do that, but I think what I've done there is wrong also.

    The structure of my ListNode:

    Code:
    typedef struct tagListNode{
    	Vector *vector;
    	Colour *colour;
    	struct tagListNode *next;
    }ListNode;
    This is all a complete mess I know but any help is much appreciated.

    Cheers

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    It looks ok to me but you don't need to cast malloc()

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    24
    I always thought if you were mallocing a variable that you had created it needed to be cast?

  4. #4

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I don't think that you close the file, fptr.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 04:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 10:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

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