Thread: Linked List Help!

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    1

    Linked List Help!

    Hi,

    I am trying to read in a file through a command line argument, then extract the words (no punctuation) and put each word into a node on a linked list, but am experiencing a few problems. Any help would be appreciated:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define LENGTH 30;
    
    //Typedef for linked list.
    typedef struct L {
    	char word[LENGTH] ;
    	struct L *next ; 
    } List;
    
    //function to set up the list.
    List *somelist( char word[], List *tl ) {
    	List *t = (List*) malloc (sizeof(List));
    	strcpy(t->word, word[LENGTH]) ;
    	t->next = tail;
    	return t;
    }
    
    //function to open and read the file and put data into the list.
    List *insert( char head[LENGTH], LIST *tl ) {
    
    	//start clock
    	clock_t start, end;
    	float result;
    	start = clock();
    	//Open file
    	FILE *fd;
    	fd = fopen("file.txt", "r");
    	//Insert words into the list
    	while(!EOF)
    		while(ispunct(word)==0 && isspace(word)==0) {
    			char word[LENGTH] = fgetc(fd);
    			word[LENGTH] = tolower(word[LENGTH]);
    			somelist(word, z);
    		}
    	}
    	//end clock
    	end = clock();
    	result = ((end-start)/CLOCK_PER_SEC);
    	return z ;
    }
    
    //function to find the given word.
    void *find( char *what[LENGTH], List *tlist ) {
    	int counter=0;
    	while( tlist != NULL ) {
    		if( tlist->x == what ) {
    			counter++;
    		}
    	tlist = tlist->next ;
    	}
    	printf("The number of occurunces of %s = %d", what[LENGTH], counter) ;
    }
    
    //main function.
    int main( void ) {
    	
    	//Initialise list
    	List *z = somelist();
    	List *pointer = NULL;
    	pointer = somelist(argv[1]);
    	char YesNo="y";
    	char c; 
    	char array[LENGTH];
    	int counter;
    	
    	printf("Enter word to be searched");
    	while(c!='\n') {
    		c=getchar();
    		c = tolower(c);
    		array[counter] = c;
    		counter++;
    	}
    
    	find(array[LENGTH], z);
    	
    }
    Thanks, mbk.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    To get command line arguments wouldent you need to use:
    Code:
    int main(int argc, char* argv[])
    instead of this:
    Code:
    int main( void )
    ?

    Then you would actually need to open the file and read from it. Theres a tutorial in the FAQ about that.

    [edit] Also you might want to use fgets to get a string from the user. Theres a section about that in the FAQ too.[/edit]
    Last edited by mike_g; 01-31-2008 at 11:47 AM.

  3. #3
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Code:
    //function to set up the list.
    List *somelist( char word[], List *tl ) {
    	List *t = (List*) malloc (sizeof(List));
    	strcpy(t->word, word[LENGTH]) ;
    	t->next = tail;
    	return t;
    }
    And in main:
    Code:
    int main( void ) {
    	
    	//Initialise list
    	List *z = somelist();
    You have defined 'somelist' to take in a char[] and a List *. But I don't see you passing them into the function. These are odd errors, have you tried to copy this from some where?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Lose the semicolon:
    Code:
    #define LENGTH 30;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM