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
And here is the header fileCode:/* 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; }
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("+++++++++++"); }



LinkBack URL
About LinkBacks


