I am having trouble on developing a linked list from a file. The requirements of this project are as follows:
• Start with a basic Node structure.
• In a separate file, make a simple program that defines a pointer to a Node, uses malloc() to define a new Node, inputs a string in the variable Word, and sets the Count to 1. Output the data.
• Make a function MakeNewNode that takes in a word, builds a Node and returns the address of the Node.
• Make a function to InsertAtBeginning where the function creates the Node and returns the address of the new Start pointer.
• Make a function to InsertInMiddle, which is different because it does not have to return a new address for the start pointer.
• Write some code to search through the list for a word. If it exists, add 1 to Count. If not, print out a message.
Program Requirements:
• Read each word from the file document.txt,
• Build a linked list keeping the words in alphabetical order,
• Update the Count of each word,
• Print out the list and frequency of each word,
I have gotten the basic outline of the program, I just need help completing the rest.
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct Node {
char Word[25];
int Count;
struct Node *Next;
};
/* Function Prototypes */
void GetWord( FILE *fileptr, char word[] );
struct Node* MakeNewNode( char word[] );
struct Node* InsertAtBeginning( struct Node *start, struct Node *newptr );
void InsertInMiddle( struct Node *newptr, struct Node *current, struct Node *previous );
void PrintList( struct Node *start );
int main()
{
/* declare and initialize variables */
FILE *fileptr;
char word[25];
struct Node *start = NULL;
struct Node *newptr = NULL;
struct Node *current = NULL;
struct Node *previous = NULL;
printf("Openning file...\n");
if( ( fileptr = fopen( "document.txt", "r" ) ) == NULL )
{
printf("Unable to open file...\n");
return;
}
/* read a word from the file */
/* read words and process until end of file */
/* Set current pointer to the beginning of the list */
/* Search to see if the word already exists in the list */
/* First check to see if it goes before the first word */
/* check if it matches the first word */
/* if not at the beginning, it goes in the middle or end */
/* read next word from the file */
GetWord( fileptr, word );
}
PrintList( start );
printf("\n-------------------------------\n\n");
fclose( fileptr );
return;
}
void GetWord( FILE *fileptr, char word[] )
{
char tempword[25] = "";
int i = 0;
fscanf( fileptr, "%s", tempword );
if( !feof(fileptr) )
{
strcpy( word, strtok( tempword, " ,.;:!?-()" ));
while( word[i] != '\0' )
{
word[i] = tolower(word[i]);
i++;
}
printf( "%s\n", word );
}
return;
}
struct Node* InsertAtBeginning( struct Node *start, struct Node *newptr )
{
}
void InsertInMiddle( struct Node *newptr, struct Node *current, struct Node *previous )
{
}
struct Node* MakeNewNode( char word[] )
{
}
void PrintList( struct Node *start )
{
printf(" Word Count\n");
printf("--------------- -----\n");
while( start != NULL )
{
printf("%15s %5d\n", start->Word, start->Count);
start = start->Next;
}
return;
}
Thank you for any and all help that you can provide! I will attach the document.txt that is supposed to be used in the file as well.
Attached File(s)
-
document.txt (1.33K)
Number of downloads: 45

New Topic/Question
Reply




MultiQuote





|