dave_567
April 7th, 2007, 01:06 PM
I'm having trouble with a linked list in C. The program reads a list of names and makes two linked lists out of the data, The program compiles, links, and runs but I keep getting a segmentation fault error at run tiime. I think it is from the function walkNode.
What am I missing? :confused:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
struct link
{
struct link *next;
char word[25];
int numTimesAppear;
};
typedef struct link Link;
int CreateAllNodes(Link *, Link *, int *, int *);
void WalkNode(Link *);
int DestroyAllNodes(Link *);
void Compare();
int main(int argc, char *argv[])
{
struct link *Dhead = NULL;
struct link *Compare;
struct link *Ahead = NULL;
int Atotal = 0;
int Dtotal = 0;
int total;
//call function to creat linked list nodes
total = CreateAllNodes(Dhead, Ahead, &Atotal, &Dtotal);
printf("\nAtotal is: %d", Atotal);
printf("\nDtotal is: %d", Dtotal);
printf("\ntotal is: %d\n", total);
WalkNode(Dhead);
//Sort lists alphabetically using compare call
//Compare();
//Delete lists using DestroyAllNodes call; call twice, once for each list
DestroyAllNodes(Dhead);
DestroyAllNodes(Ahead);
return 0;
}
int CreateAllNodes(Link *Dhead, Link *Ahead, int *pAtotal, int *pDtotal)
{
int total = 0;
int x, y, z;
char NameBuffer[50];
char WordBuffer[25];
char a, b, c;
long iEndFile, iFSize, iBegFile;
Link *DnewNode = NULL;
Link *DtmpNode = NULL;
Link *AnewNode = NULL;
Link *AtmpNode = NULL;
Link *tmpNodePtr = NULL;
FILE *MyFile;
iFSize = 0;
//request what file to open
printf("What File would you like to open?");
scanf("%s", NameBuffer );
//check for file, open file
if ((MyFile = fopen( NameBuffer, "r+")) == NULL)
{
printf ("\ncould not open the file '%s'\n", NameBuffer);
printf ("I have failed you... I'm sorry.\n\n");
return 0;
}
//get file size
fseek(MyFile, 0L, SEEK_END);
iEndFile = ftell(MyFile); // the end of the file is the size of the file
iFSize = iEndFile;
rewind(MyFile);
//read file
while (ftell(MyFile) < iEndFile)
{
//read data from file
fscanf (MyFile,"%s",WordBuffer);
printf("Loading nodes %d\n", ftell(MyFile));
//load nodes
//Compare characters to load in proper node using an if-else statement
if (WordBuffer[0] == 'd' || WordBuffer[0] == 'D')
{
if (*pDtotal == 0)
{
//create head node
Dhead = (Link *)malloc(sizeof(Link));
memcpy(Dhead->word, WordBuffer,25);
Dhead->numTimesAppear = 0;
Dhead->next = NULL;
}
else
{
//create nodes following head node
DnewNode = (Link *)malloc(sizeof(Link));
memcpy(DnewNode->word, WordBuffer,25);
DnewNode->numTimesAppear = 0;
DnewNode->next = (Link *) Dhead;
Dhead = DnewNode;
}
*pDtotal = *pDtotal + 1;
}
else
{
*pAtotal = *pAtotal + 1;
}
}
total = *pAtotal + *pDtotal;
printf("\nAtotal function is: %d", *pDtotal);
printf("\nDtotal function is: %d", *pAtotal);
printf("\ntotal function is: %d", total);
fclose(MyFile);
return total;
}
void WalkNode(Link *headNode)
{
Link *currentNode = headNode;
printf("\n%d == zero\n", headNode->numTimesAppear); /* what is going on here??? */
//loop through nodes and print out the info
while (currentNode->next != NULL)
{
printf("\n%s, %d", (char*)headNode->word, headNode->numTimesAppear);
currentNode = (Link *)currentNode->next;
}
//testing to make sure I am out of the loop
printf("\nOut of the loop now.\n");
}
void compare()
{
printf("I am able to compare two words\n");
}
int DestroyAllNodes(Link *headNode)
{
Link *currentNode;
currentNode = headNode;
Link *tmpNodePtr = NULL;
while (currentNode->next != NULL)
{
//Delete the nodes
tmpNodePtr = (Link *)currentNode->next;
free(currentNode);
currentNode = tmpNodePtr;
}
return 0;
}
What am I missing? :confused:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
struct link
{
struct link *next;
char word[25];
int numTimesAppear;
};
typedef struct link Link;
int CreateAllNodes(Link *, Link *, int *, int *);
void WalkNode(Link *);
int DestroyAllNodes(Link *);
void Compare();
int main(int argc, char *argv[])
{
struct link *Dhead = NULL;
struct link *Compare;
struct link *Ahead = NULL;
int Atotal = 0;
int Dtotal = 0;
int total;
//call function to creat linked list nodes
total = CreateAllNodes(Dhead, Ahead, &Atotal, &Dtotal);
printf("\nAtotal is: %d", Atotal);
printf("\nDtotal is: %d", Dtotal);
printf("\ntotal is: %d\n", total);
WalkNode(Dhead);
//Sort lists alphabetically using compare call
//Compare();
//Delete lists using DestroyAllNodes call; call twice, once for each list
DestroyAllNodes(Dhead);
DestroyAllNodes(Ahead);
return 0;
}
int CreateAllNodes(Link *Dhead, Link *Ahead, int *pAtotal, int *pDtotal)
{
int total = 0;
int x, y, z;
char NameBuffer[50];
char WordBuffer[25];
char a, b, c;
long iEndFile, iFSize, iBegFile;
Link *DnewNode = NULL;
Link *DtmpNode = NULL;
Link *AnewNode = NULL;
Link *AtmpNode = NULL;
Link *tmpNodePtr = NULL;
FILE *MyFile;
iFSize = 0;
//request what file to open
printf("What File would you like to open?");
scanf("%s", NameBuffer );
//check for file, open file
if ((MyFile = fopen( NameBuffer, "r+")) == NULL)
{
printf ("\ncould not open the file '%s'\n", NameBuffer);
printf ("I have failed you... I'm sorry.\n\n");
return 0;
}
//get file size
fseek(MyFile, 0L, SEEK_END);
iEndFile = ftell(MyFile); // the end of the file is the size of the file
iFSize = iEndFile;
rewind(MyFile);
//read file
while (ftell(MyFile) < iEndFile)
{
//read data from file
fscanf (MyFile,"%s",WordBuffer);
printf("Loading nodes %d\n", ftell(MyFile));
//load nodes
//Compare characters to load in proper node using an if-else statement
if (WordBuffer[0] == 'd' || WordBuffer[0] == 'D')
{
if (*pDtotal == 0)
{
//create head node
Dhead = (Link *)malloc(sizeof(Link));
memcpy(Dhead->word, WordBuffer,25);
Dhead->numTimesAppear = 0;
Dhead->next = NULL;
}
else
{
//create nodes following head node
DnewNode = (Link *)malloc(sizeof(Link));
memcpy(DnewNode->word, WordBuffer,25);
DnewNode->numTimesAppear = 0;
DnewNode->next = (Link *) Dhead;
Dhead = DnewNode;
}
*pDtotal = *pDtotal + 1;
}
else
{
*pAtotal = *pAtotal + 1;
}
}
total = *pAtotal + *pDtotal;
printf("\nAtotal function is: %d", *pDtotal);
printf("\nDtotal function is: %d", *pAtotal);
printf("\ntotal function is: %d", total);
fclose(MyFile);
return total;
}
void WalkNode(Link *headNode)
{
Link *currentNode = headNode;
printf("\n%d == zero\n", headNode->numTimesAppear); /* what is going on here??? */
//loop through nodes and print out the info
while (currentNode->next != NULL)
{
printf("\n%s, %d", (char*)headNode->word, headNode->numTimesAppear);
currentNode = (Link *)currentNode->next;
}
//testing to make sure I am out of the loop
printf("\nOut of the loop now.\n");
}
void compare()
{
printf("I am able to compare two words\n");
}
int DestroyAllNodes(Link *headNode)
{
Link *currentNode;
currentNode = headNode;
Link *tmpNodePtr = NULL;
while (currentNode->next != NULL)
{
//Delete the nodes
tmpNodePtr = (Link *)currentNode->next;
free(currentNode);
currentNode = tmpNodePtr;
}
return 0;
}