Thread: Linked list, Head pointing to second node

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    Linked list, Head pointing to second node

    Hey guys,

    I am writing a code using linked lists, and everything seems to be working fine, other than the fact that although I want my head to be pointing to the first node, it points to the second node. The code is here...

    Am I doing something wrong in the header>if (*head==NULL) section?


    This is the driver file

    Code:
    /* Nathan Berliner
     9/28/2011
     prelab5.c file
    */
    
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include"prelab5.h"
    
    
    
    
    int main (int argc, char *argv[])
    {
    
    
    char * movieFile;
    char * runFile;
    FILE* pFile1;
    FILE* pFile2;
    int count;
    char title [100];
    int release;
    char director[100];
    float rating;
    int minute;
    int second;
    
    
    listnode head=NULL;
    
    
    
    
    //will need to use comand line to get #dvds
    //takes the users input from the comand line, proper usage is ./a.out file row column
    movieFile=argv[1];
    runFile=argv[2];
    //opens the file to read only
    pFile1 =fopen(movieFile, "r");
    pFile2=fopen(runFile, "r");
    //will return a pointer, if null, says program can't be used
    //if valid, it will be opened
    if (pFile1 == NULL)
    {
    printf("Error, file not opened");
    }
    
    
    if (pFile2 == NULL)
    {
    printf("Error, file not opened");
    }
    
    
    if (pFile1 && pFile2 != NULL)
    {
    printf("opened\n");
    int i;
    for(i=0; i<=11; i++)
    
    
    {
    fscanf(pFile1, "%s%d%s%f", title, &release, director,&rating);
    fscanf(pFile2, "%d%d", &minute, &second);
    makeList(&head, title, release, director, rating, minute, second);
    }
    printf("-----------\n");
    prints(head);
    }
    
    
    
    
    
    
    return 0;
    }
    And here is the header file

    Code:
    /* Nathan Berliner
    prelab5.h
    */
    
    
    
    
    //structure definitions
     struct movie_runtime
    {
    int minutes;
    int seconds;
    };
    typedef struct movie_runtime runtime;
    
    
    
    
    
    
    typedef struct movielist
    {
    char title[100];
    int release;
    char director[100];
    float rating;
    struct movielist *next;
    runtime timeData;
    } movie;
    
    
    typedef movie * listnode; 
    
    
    void makeList(listnode * head, char* title, int release, char* director, float rating, int minute, int second)
    {
    listnode tail;
    
    
    
    
    if(*head==NULL)
    {
    
    
    printf("head\n");
    tail=(listnode)malloc(sizeof(movie));
    strcpy(tail->title,title);
    tail->release=release;
    strcpy(tail->director,director);
    tail->rating=rating;
    tail->timeData.seconds=second;
    tail->timeData.minutes=minute;
    tail->next=NULL;
    *head=tail;
    
    
    
    
    
    
    }
    else
    {
    printf("Else\n");
    tail->next=(listnode)malloc(sizeof(movie));
    strcpy(tail->title,title);
    tail->release=release;
    strcpy(tail->director,director);
    tail->rating=rating;
    tail->timeData.seconds=second;
    tail->timeData.minutes=minute;
    tail->next->next=NULL;
    tail=tail->next;
    }
    
    
    
    
    }
    
    
    
    
    void prints(listnode head)
    {
    
    
    listnode temp=NULL;
    
    
    for(temp= head; temp != NULL; temp =temp->next)
    {
            printf("%s\n%d\n%s\n%f\n%d\n%d\n\n", temp->title, temp->release, temp->director,temp->rating, temp->timeData.minutes, temp->timeData.seconds);
    }
    
    
    printf("+++++++++++");
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    When you're adding to a list that already exists (i.e. head is not NULL), you're overwriting the current tail with the new information rather than writing the new information to tail->next. All you're doing to tail->next is creating it and setting its next pointer to NULL. Presumably you want
    Code:
    tail->next->release = release;
    and so on.

    Also, it's really bad practice to put code in header files. If your instructor requires this, then you have no choice, but keep in mind that it's the wrong thing to do. If you put code in a header file, that file cannot be included in multiple .c files or it will create multiple definitions. There are exceptions to this, but they are few and far between, and do not apply here.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    13
    Thank you so much, that really did help. I wrote in the code that you said, and then drew out a memory diagram, and saw how what you said was correct!

    Can you elaborate on what you mean by not putting code in the header files? Do you mean just put prototypes in there?

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Can you elaborate on what you mean by not putting code in the header files? Do you mean just put prototypes in there?
    That is precisely correct. The problem is that a header file is simply pasted into another file whenever it is #included. The compiler (or preprocessor) is not smart enough to recognize that a file has been #included in multiple places; it's a very simple copy-paste sort of operation.

    As such, putting code in a header and including it in multiple .c files is the same as putting that code in multiple .c files. This is, according to the C standard, undefined behavior, but will often result in something like this:
    Code:
    /tmp/cc7UFCWR.o: In function `f':
    m.c:(.text+0x0): multiple definition of `f'
    /tmp/cc9AEgM3.o:t.c:(.text+0x0): first defined here
    The general thing you want to do is put the prototype in a .h file and the implementation in a single .c file. The same goes for variables: put something like
    Code:
    extern int foo;
    in a header, and
    Code:
    int foo;
    in a single .c file. extern can more or less be thought of as meaning “there exists a variable with this name and this type, but it will be created somewhere else.”

    The main reason you'd ever put code in a header file is if you have a function you want inlined (e.g. integrated into the caller directly to avoid the overhead of a function call/return). If your compiler supports inline functions, you'd use:
    Code:
    static inline void f(void) { ... }
    The static restricts the function to whatever source file it's declared in (either directly or via an #include); it's hidden from other source files. Thus multiple files could create this “f” function, so long as it remains hidden.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. change the head of linked list question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 03-21-2009, 02:31 PM
  2. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  3. Cant head insert on doubly linked list
    By aciarlillo in forum C++ Programming
    Replies: 4
    Last Post: 09-18-2005, 11:31 AM
  4. head pointer(linked list)
    By Ausandy in forum C Programming
    Replies: 5
    Last Post: 08-24-2002, 01:25 AM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM

Tags for this Thread