Developing a linked list

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 3366 Views - Last Post: 23 November 2010 - 04:55 PM Rate Topic: -----

#1 jrayos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 10

Developing a linked list

Posted 23 November 2010 - 11:42 AM

Hello,

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)



Is This A Good Question/Topic? 0
  • +

Replies To: Developing a linked list

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,435
  • Joined: 25-December 09

Re: Developing a linked list

Posted 23 November 2010 - 12:25 PM

Does your code compile without error/warning messages?

If not please post the entire error/warning message.

Quote

I have gotten the basic outline of the program, I just need help completing the rest.


This is not a very good explanation of what is wrong with your code. Please ask specific questions. We will not "fill in the blanks" for you.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Yates  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 10

Re: Developing a linked list

Posted 23 November 2010 - 12:30 PM

It seems you and I have the exact same program to do. I really don't like our professor, he kinda just threw us into the water with this project.

I made a topic asking for help on this too. I'll let you know if anyone posts anything.
Was This Post Helpful? 0
  • +
  • -

#4 jrayos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 10

Re: Developing a linked list

Posted 23 November 2010 - 12:39 PM

thanks, ill let you know if i get any help as well
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Developing a linked list

Posted 23 November 2010 - 12:49 PM

You two do realize we're not going to DO your assignments for you two, you know. You both posted the EXACT SAME CODE, which means all you've done is copy your assignment and what was given to you by the professor and thrown up your hands saying "I don't know what to do!" That's just sad. How about putting in an actual effort before throwing in the towel huh?
Was This Post Helpful? 0
  • +
  • -

#6 Yates  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 10

Re: Developing a linked list

Posted 23 November 2010 - 12:53 PM

View PostJackOfAllTrades, on 23 November 2010 - 11:49 AM, said:

You two do realize we're not going to DO your assignments for you two, you know. You both posted the EXACT SAME CODE, which means all you've done is copy your assignment and what was given to you by the professor and thrown up your hands saying "I don't know what to do!" That's just sad. How about putting in an actual effort before throwing in the towel huh?


I implemented some of the things that our professor gave us, and I've started to read the file in mine. The MakeNewNode and InsertAtBeginning are in my code too.
Was This Post Helpful? 0
  • +
  • -

#7 jrayos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 10

Re: Developing a linked list

Posted 23 November 2010 - 01:07 PM

i have finished the functions, but i am now lost on where to continue.

current code:

#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 ;
	}


	newptr = MakeNewNode(word); // function call for MakeNewNode

	/* read a word from the file */
	GetWord( fileptr, word);
	
	
	/* read words and process until end of file */


	/* Set current pointer to the beginning of the list */

	start = InsertAtBeginning( start, newptr); // fuction call InsertAtBeginning
				
	/* Search to see if the word already exists in the list */
	while(current != NULL && strcmp( current->Word, word) < 0)
	{
		current = current->Next;
		previous=previous->Next;
	}
	//if(current == NULL)
	//else

	if (strcmp (current-> Word, word == 0))
	{
		current->Count ++;
	}
	else
	newptr = MakeNewNode;
	InsertInMiddle( newptr, current, previous );

	

	/* 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 ); // Function call for GetWord
	
	PrintList( start ); // Function call for PrintList
	printf("\n-------------------------------\n\n");
	fclose( fileptr );
	return;
}// end Main function

////////////////////////////////// Start Functions ////////////////////////////////////////////

//Fuction Definition for GetWord Function 
void GetWord( FILE *fileptr, char word[] )// function call for GetWord
{
	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;
}// end GetWord Function

// Fuction Definition for InsertAtBeginning Function 
struct Node* InsertAtBeginning( struct Node *start, struct Node *newptr )          
{
	newptr->Next = start;
	start = newptr;
	return start;
}// end InsertAtBeginning Function

// Fuction Definition for InsertInMiddle Function 
void InsertInMiddle( struct Node *newptr, struct Node *current, struct Node *previous )
{
	previous->Next = newptr;
	newptr->Next = current;
	return;

}// end InsertInMiddle Function 

// Fuction Definition for MakeNewNode Function 
struct Node* MakeNewNode( char word[] )
{
	struct Node * ptr;
	ptr = malloc( sizeof ( struct Node ));
	strcpy( ptr->Word, word);
	ptr->Count = 1;
	ptr->Next=NULL;
	return ptr;
}// end MakeNewNode Function 

// Fuction Definition for PrintList Function 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;
}// end PrintList Function




compiling errors:

Compiler: Default compiler
Executing gcc.exe...
gcc.exe "C:\Users\Jon\Desktop\LinkedListStarterfile.c" -o "C:\Users\Jon\Desktop\LinkedListStarterfile.exe" -ansi -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\Users\Jon\Desktop\LinkedListStarterfile.c:10: warning: useless keyword or type name in empty declaration

C:\Users\Jon\Desktop\LinkedListStarterfile.c: In function `main':
C:\Users\Jon\Desktop\LinkedListStarterfile.c:37: error: syntax error before '/' token
C:\Users\Jon\Desktop\LinkedListStarterfile.c:48: error: syntax error before '/' token
C:\Users\Jon\Desktop\LinkedListStarterfile.c: At top level:
C:\Users\Jon\Desktop\LinkedListStarterfile.c:56: error: syntax error before '/' token
C:\Users\Jon\Desktop\LinkedListStarterfile.c:65: warning: parameter names (without types) in function declaration
C:\Users\Jon\Desktop\LinkedListStarterfile.c:65: error: conflicting types for 'InsertInMiddle'
C:\Users\Jon\Desktop\LinkedListStarterfile.c:16: error: previous declaration of 'InsertInMiddle' was here
C:\Users\Jon\Desktop\LinkedListStarterfile.c:65: error: conflicting types for 'InsertInMiddle'
C:\Users\Jon\Desktop\LinkedListStarterfile.c:16: error: previous declaration of 'InsertInMiddle' was here
C:\Users\Jon\Desktop\LinkedListStarterfile.c:65: warning: data definition has no type or storage class
C:\Users\Jon\Desktop\LinkedListStarterfile.c:76: warning: parameter names (without types) in function declaration
C:\Users\Jon\Desktop\LinkedListStarterfile.c:76: error: conflicting types for 'GetWord'
C:\Users\Jon\Desktop\LinkedListStarterfile.c:13: error: previous declaration of 'GetWord' was here
C:\Users\Jon\Desktop\LinkedListStarterfile.c:76: error: conflicting types for 'GetWord'
C:\Users\Jon\Desktop\LinkedListStarterfile.c:13: error: previous declaration of 'GetWord' was here
C:\Users\Jon\Desktop\LinkedListStarterfile.c:76: warning: data definition has no type or storage class
C:\Users\Jon\Desktop\LinkedListStarterfile.c:76: error: syntax error before '/' token
C:\Users\Jon\Desktop\LinkedListStarterfile.c:80: warning: parameter names (without types) in function declaration
C:\Users\Jon\Desktop\LinkedListStarterfile.c:80: warning: data definition has no type or storage class
C:\Users\Jon\Desktop\LinkedListStarterfile.c:81: error: syntax error before "return"
C:\Users\Jon\Desktop\LinkedListStarterfile.c:91: error: syntax error before string constant

C:\Users\Jon\Desktop\LinkedListStarterfile.c:91: error: conflicting types for 'fscanf'
C:\Users\Jon\Desktop\LinkedListStarterfile.c:91: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
C:\Users\Jon\Desktop\LinkedListStarterfile.c:91: error: conflicting types for 'fscanf'
C:\Users\Jon\Desktop\LinkedListStarterfile.c:91: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
C:\Users\Jon\Desktop\LinkedListStarterfile.c:91: warning: data definition has no type or storage class
C:\Users\Jon\Desktop\LinkedListStarterfile.c:100: error: syntax error before string constant
C:\Users\Jon\Desktop\LinkedListStarterfile.c:100: error: conflicting types for 'printf'
C:\Users\Jon\Desktop\LinkedListStarterfile.c:100: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
C:\Users\Jon\Desktop\LinkedListStarterfile.c:100: error: conflicting types for 'printf'
C:\Users\Jon\Desktop\LinkedListStarterfile.c:100: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
C:\Users\Jon\Desktop\LinkedListStarterfile.c:100: warning: data definition has no type or storage class
C:\Users\Jon\Desktop\LinkedListStarterfile.c:109: error: `newptr' undeclared here (not in a function)
C:\Users\Jon\Desktop\LinkedListStarterfile.c:109: warning: data definition has no type or storage class
C:\Users\Jon\Desktop\LinkedListStarterfile.c:110: error: syntax error before "return"
C:\Users\Jon\Desktop\LinkedListStarterfile.c:126: warning: initialization makes integer from pointer without a cast
C:\Users\Jon\Desktop\LinkedListStarterfile.c:126: error: initializer element is not constant
C:\Users\Jon\Desktop\LinkedListStarterfile.c:126: warning: data definition has no type or storage class
C:\Users\Jon\Desktop\LinkedListStarterfile.c:127: error: syntax error before '->' token
C:\Users\Jon\Desktop\LinkedListStarterfile.c:127: error: conflicting types for 'strcpy'
C:\Users\Jon\Desktop\LinkedListStarterfile.c:127: error: conflicting types for 'strcpy'
C:\Users\Jon\Desktop\LinkedListStarterfile.c:127: warning: data definition has no type or storage class
C:\Users\Jon\Desktop\LinkedListStarterfile.c:128: error: syntax error before '->' token
C:\Users\Jon\Desktop\LinkedListStarterfile.c:136: error: syntax error before string constant
C:\Users\Jon\Desktop\LinkedListStarterfile.c:136: warning: data definition has no type or storage class
C:\Users\Jon\Desktop\LinkedListStarterfile.c:141: error: redefinition of 'start'
C:\Users\Jon\Desktop\LinkedListStarterfile.c:109: error: previous definition of 'start' was here
C:\Users\Jon\Desktop\LinkedListStarterfile.c:141: error: invalid type argument of `->'
C:\Users\Jon\Desktop\LinkedListStarterfile.c:141: warning: data definition has no type or storage class
C:\Users\Jon\Desktop\LinkedListStarterfile.c:142: error: syntax error before '}' token

Execution terminated


sorry for the initial lack of effort. the instructor confuses us to the max.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,435
  • Joined: 25-December 09

Re: Developing a linked list

Posted 23 November 2010 - 02:11 PM

For the error:

Quote

C:\Users\Jon\Desktop\LinkedListStarterfile.c:37: error: syntax error before '/' token


You are using C++ type of comments, you should use "/* */" comments in your code.

For the error:

Quote

C:\Users\Jon\Desktop\LinkedListStarterfile.c:10: warning: useless keyword or type name in empty declaration


Is complaining about your structure definition.

typedef struct Node {
	char Word[25];
	int Count;
	struct Node *Next;
};



I believe that you need to add "Node" after the closing brace and before the semicolon.

Quote

C:\Users\Jon\Desktop\LinkedListStarterfile.c:65: warning:
parameter names (without types) in function declaration


Lets look at the code around line 65:

	else
	newptr = MakeNewNode;
	InsertInMiddle( newptr, current, previous );




Look at the second line above, do you notice anything missing?

Fix these errors and then see where you are.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 jrayos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 10

Re: Developing a linked list

Posted 23 November 2010 - 02:31 PM

I wasnt quite sure what to do about the structure definition. With the other problems fixed i get this:

#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 ;
	}


	newptr = MakeNewNode(word); /* function call for MakeNewNode */

	/* read a word from the file */
	GetWord( fileptr, word);
	
	
	/* read words and process until end of file */


	/* Set current pointer to the beginning of the list */

	start = InsertAtBeginning( start, newptr); /* fuction call InsertAtBeginning */
				
	/* Search to see if the word already exists in the list */
	while(current != NULL && strcmp( current->Word, word) < 0)
	{
		current = current->Next;
		previous=previous->Next;
	}
	/*if(current == NULL)
	else */

	if (strcmp (current-> Word, word == 0))
	{
		current->Count ++;
	}
	else
	{
	newptr = MakeNewNode;
	InsertInMiddle( newptr, current, previous )};

	

	/* 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 ); /* Function call for GetWord */
	
	PrintList( start ); // Function call for PrintList
	printf("\n-------------------------------\n\n");
	fclose( fileptr );
	return;
}/* end Main function */

/*Start Functions */

/*Fuction Definition for GetWord Function  */
void GetWord( FILE *fileptr, char word[] )/* function call for GetWord */
{
	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;
}/* end GetWord Function

 Fuction Definition for InsertAtBeginning Function  */
struct Node* InsertAtBeginning( struct Node *start, struct Node *newptr )          
{
	newptr->Next = start;
	start = newptr;
	return start;
}/* end InsertAtBeginning Function

 Fuction Definition for InsertInMiddle Function  */
void InsertInMiddle( struct Node *newptr, struct Node *current, struct Node *previous )
{
	previous->Next = newptr;
	newptr->Next = current;
	return;

}/* end InsertInMiddle Function 

 Fuction Definition for MakeNewNode Function  */
struct Node* MakeNewNode( char word[] )
{
	struct Node * ptr;
	ptr = malloc( sizeof ( struct Node ));
	strcpy( ptr->Word, word);
	ptr->Count = 1;
	ptr->Next=NULL;
	return ptr;
}/* end MakeNewNode Function 

// Fuction Definition for PrintList Function  */
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;
}/* end PrintList Function */





followed by these errors:

Compiler: Default compiler
Executing gcc.exe...
gcc.exe "C:\Users\Jon\Desktop\LinkedListStarterfile.c" -o "C:\Users\Jon\Desktop\LinkedListStarterfile.exe" -ansi -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\Users\Jon\Desktop\LinkedListStarterfile.c:10: warning: useless keyword or type name in empty declaration

C:\Users\Jon\Desktop\LinkedListStarterfile.c: In function `main':
C:\Users\Jon\Desktop\LinkedListStarterfile.c:59: warning: passing arg 2 of `strcmp' makes pointer from integer without a cast
C:\Users\Jon\Desktop\LinkedListStarterfile.c:65: warning: assignment from incompatible pointer type
C:\Users\Jon\Desktop\LinkedListStarterfile.c:66: error: syntax error before '}' token
C:\Users\Jon\Desktop\LinkedListStarterfile.c:79: error: syntax error before '/' token

Execution terminated
Was This Post Helpful? 0
  • +
  • -

#10 jrayos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 10

Re: Developing a linked list

Posted 23 November 2010 - 02:45 PM

edit:: i think ive fixed a few things..heres where i am currently :

#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 ;
	}


	newptr = MakeNewNode(word); /* function call for MakeNewNode */

	/* read a word from the file */
	GetWord( fileptr, word);
	
	
	/* read words and process until end of file */


	/* Set current pointer to the beginning of the list */

	start = InsertAtBeginning( start, newptr); /* fuction call InsertAtBeginning */
				
	/* Search to see if the word already exists in the list */
	while(current != NULL && strcmp( current->Word, word) < 0)
	{
		current = current->Next;
		previous=previous->Next;
	}
	/*if(current == NULL)
	else */
for (current = start; current; current = current->Next, previous = current) {
	if (strcmp (current-> Word, word) >= 0)
	{
		current->Count ++;
	}
	else
	{
	newptr = MakeNewNode(word);
	InsertInMiddle( newptr, current, previous )};

	

	/* 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 ); /* Function call for GetWord */
	
	PrintList( start ); /* Function call for PrintList */
	printf("\n-------------------------------\n\n");
	fclose( fileptr );
	return;
}/* end Main function */

/*Start Functions */

/*Fuction Definition for GetWord Function  */
void GetWord( FILE *fileptr, char word[] )/* function call for GetWord */
{
	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;
}/* end GetWord Function

 Fuction Definition for InsertAtBeginning Function  */
struct Node* InsertAtBeginning( struct Node *start, struct Node *newptr )          
{
	newptr->Next = start;
	start = newptr;
	return start;
}/* end InsertAtBeginning Function

 Fuction Definition for InsertInMiddle Function  */
void InsertInMiddle( struct Node *newptr, struct Node *current, struct Node *previous )
{
	previous->Next = newptr;
	newptr->Next = current;
	return;

}/* end InsertInMiddle Function 

 Fuction Definition for MakeNewNode Function  */
struct Node* MakeNewNode( char word[] )
{
	struct Node * ptr;
	ptr = malloc( sizeof ( struct Node ));
	strcpy( ptr->Word, word);
	ptr->Count = 1;
	ptr->Next=NULL;
	return ptr;
}/* end MakeNewNode Function 

// Fuction Definition for PrintList Function  */
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;
} }/* end PrintList Function */








Compiler: Default compiler
Executing gcc.exe...
gcc.exe "C:\Users\Jon\Desktop\LinkedListStarterfile.c" -o "C:\Users\Jon\Desktop\LinkedListStarterfile.exe" -ansi -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\Users\Jon\Desktop\LinkedListStarterfile.c:10: warning: useless keyword or type name in empty declaration

C:\Users\Jon\Desktop\LinkedListStarterfile.c: In function `main':
C:\Users\Jon\Desktop\LinkedListStarterfile.c:66: error: syntax error before '}' token

Execution terminated
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,435
  • Joined: 25-December 09

Re: Developing a linked list

Posted 23 November 2010 - 02:46 PM

Quote

I wasnt quite sure what to do about the structure definition.


Quote

I believe that you need to add "Node" after the closing brace and before the semicolon.


So now lets look at the next error:

Quote

C:\Users\Jon\Desktop\LinkedListStarterfile.c:59: warning: passing arg 2 of `strcmp' makes pointer from integer without a cast


and the code on line 59 is:

if (strcmp (current-> Word, word == 0))


How many parameters does strcmp() have? What type are they? What is wrong with arg 2?
(hint check your () locations).

The next errors are:

Quote

C:\Users\Jon\Desktop\LinkedListStarterfile.c:65: warning: assignment from incompatible pointer type


and the code for these lines are:

newptr = MakeNewNode;



What is MakeNewNode?

Is it a variable?

Is it a function?

If it is a function is there something missing?

The next error will probably be fixed when you fix this line.

The last error was explained in my last post (comments).

You have noticed how I found which line in your code was causing the problems I hope.

Jim

This post has been edited by jimblumberg: 23 November 2010 - 02:48 PM

Was This Post Helpful? 0
  • +
  • -

#12 jrayos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 10

Re: Developing a linked list

Posted 23 November 2010 - 03:13 PM

This is really starting to bug me lol

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct Node {
	char Word[25];
	int Count;
	struct Node *Next;
}Node;

/* 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 ;
	}


	newptr = MakeNewNode(word); /* function call for MakeNewNode */

	/* read a word from the file */
	GetWord( fileptr, word);
	
	
	/* read words and process until end of file */


	/* Set current pointer to the beginning of the list */

	start = InsertAtBeginning( start, newptr); /* fuction call InsertAtBeginning */
				
	/* Search to see if the word already exists in the list */
	while(current != NULL && strcmp( current->Word, word) < 0)
	{
		current = current->Next;
		previous=previous->Next;
	
	/*if(current == NULL)
	else */
for (current = start; current; current = current->Next, previous = current) {
	if (strcmp (current-> Word, word) >= 0)
	{
		current->Count ++;
	}
	else
	{
	newptr = MakeNewNode(word);

	InsertInMiddle( newptr, current, previous );
	GetWord( fileptr, word ); /* Function call for GetWord */
	PrintList( start ); /* Function call for PrintList */
	printf("\n-------------------------------\n\n");
	fclose( fileptr ); 
	return;
}/* end Main function */

/*Start Functions */

/*Fuction Definition for GetWord Function  */
void GetWord( FILE *fileptr, char word[] )/* function call for GetWord */
{
	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;
}/* end GetWord Function

 Fuction Definition for InsertAtBeginning Function  */
struct Node* InsertAtBeginning( struct Node *start, struct Node *newptr )          
{
	newptr->Next = start;
	start = newptr;
	return start;
}/* end InsertAtBeginning Function

 Fuction Definition for InsertInMiddle Function  */
void InsertInMiddle( struct Node *newptr, struct Node *current, struct Node *previous )
{
	previous->Next = newptr;
	newptr->Next = current;
	return;

}/* end InsertInMiddle Function 

 Fuction Definition for MakeNewNode Function  */
struct Node* MakeNewNode( char word[] )
{
	struct Node * ptr;
	ptr = malloc( sizeof ( struct Node ));
	strcpy( ptr->Word, word);
	ptr->Count = 1;
	ptr->Next=NULL;
	return ptr;
}/* end MakeNewNode Function 

// Fuction Definition for PrintList Function  */
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;
}






compiler:

Compiler: Default compiler
Executing gcc.exe...
gcc.exe "C:\Users\Jon\Desktop\LinkedListStarterfile.c" -o "C:\Users\Jon\Desktop\LinkedListStarterfile.exe" -ansi -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\Users\Jon\Desktop\LinkedListStarterfile.c: In function `main':
C:\Users\Jon\Desktop\LinkedListStarterfile.c:137: error: syntax error at end of input



ive searched where my last closing bracket should go but if i put it anywhere it makes a "Linker Error" Undefined reference...
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,435
  • Joined: 25-December 09

Re: Developing a linked list

Posted 23 November 2010 - 03:35 PM

Quote

C:\Users\Jon\Desktop\LinkedListStarterfile.c:137: error: syntax error at end of input


Look at lines 136, 137, 138, Now count and match up your braces. Do they match?

Is that last return; on line 136 inside of any function?

Do you need the last 3 braces?

Jim

This post has been edited by jimblumberg: 23 November 2010 - 03:36 PM

Was This Post Helpful? 0
  • +
  • -

#14 jrayos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 10

Re: Developing a linked list

Posted 23 November 2010 - 03:48 PM

Alright. Now i have a code that completely compiles without errors or warnings and the program freezes and then crashes.. now i am stumped lol


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct Node {
	char Word[25];
	int Count;
	struct Node *Next;
}Node;

/* 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 */
	    GetWord(fileptr, word);
	     
	     
	    /* read words and process until end of file */
	    while (!feof(fileptr)){
	        printf("s%\n", word);
	        fscanf(fileptr, "%s", word);
	    }
	 
	        /* Set current pointer to the beginning of the list */
	        start = InsertAtBeginning(start, newptr);
	        /* Search to see if the word already exists in the list */
	        while(current != NULL && strcmp( current->Word, word) < 0){
	            current = current->Next;
	            previous=previous->Next;
	        }
	 
	      if (strcmp (current-> Word, word) >= 0)
	        {
	            current->Count ++;
	        }
	        else
	        {
	        newptr = MakeNewNode(word);
	        InsertInMiddle( newptr, current, previous );
	        }
	        /* 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 0;
	}

/*Start Functions */

/*Fuction Definition for GetWord Function  */
void GetWord( FILE *fileptr, char word[] )/* function call for GetWord */
{
	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;
}/* end GetWord Function

 Fuction Definition for InsertAtBeginning Function  */
struct Node* InsertAtBeginning( struct Node *start, struct Node *newptr )          
{
	newptr->Next = start;
	start = newptr;
	return start;
}/* end InsertAtBeginning Function

 Fuction Definition for InsertInMiddle Function  */
void InsertInMiddle( struct Node *newptr, struct Node *current, struct Node *previous )
{
	previous->Next = newptr;
	newptr->Next = current;
	return;

}/* end InsertInMiddle Function 

 Fuction Definition for MakeNewNode Function  */
struct Node* MakeNewNode( char word[] )
{
	struct Node * ptr;
	ptr = malloc( sizeof ( struct Node ));
	strcpy( ptr->Word, word);
	ptr->Count = 1;
	ptr->Next=NULL;
	return ptr;
}/* end MakeNewNode Function 

// Fuction Definition for PrintList Function  */
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;
    }
}




Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,435
  • Joined: 25-December 09

Re: Developing a linked list

Posted 23 November 2010 - 03:54 PM

Check out line 42:

printf("s%\n", word);


Look closely at your format specifier. Does it look right?

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2