Thread: struct holding data inside a linked list struct

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    struct holding data inside a linked list struct

    hey,

    I have a problem with the current code:
    Code:
    typedef struct movieStruct
    {
       int id;
       char movieTitle[20];
       int year;
       struct movieStruct* next;
    } Movie;
    
    
    
    typedef struct movieListStruct
    {
       Movie* head;
    } MovieList;
    
    Movie *insert(Movie *head, Movie *current, Movie *prev, int id, char *movieTitle, int year)
    {
    	Movie *new;
    	new = (Movie *)malloc(sizeof(Movie)))
    	current = head;
    	prev = NULL;
    	new->id = id;
    	strcpy(new->movieTitle, movieTitle);
    	new->year = year;
    	new->next = current;
    
    	if (prev == NULL)
    	{
    		head = new;
    	}
    	return head;
    }
    Firstly, I want to have the data in the Movie struct be part of the MovieList node but cannot get it working in the insert function, as the data is directly using the Movie struct. Can someone tell me how to change the insert type to MovieList while still actually inserting data into the Movie struct which is part of a MovieList node?
    Last edited by icestorm; 10-06-2009 at 12:48 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That is probably the most minimal linked list ever: no data, just links. If you want to store data inside your linked list (and you do), then your MovieListStruct has to have an actual data element in it.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    ah thanks, I wasn't doing it properly and now I think I got it, going to try and have it hold the movie data in an actual element.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One more linked list implementation
    By BlackOps in forum C Programming
    Replies: 17
    Last Post: 07-16-2009, 09:34 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  4. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM