Thread: please help..linked list homework assignment due tomorrow..

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    Question please help..linked list homework assignment due tomorrow..

    hey im trying to read a sequence of strings from the user and store them in different nodes..my code reads the line and stores it as a whole sentence , how do i split the line into individual strings and store them into different nodes ? if i use strtok it only stores the first word and ignores the rest..please help!

    Code:
            getline(words)
            p=(Node *) malloc(sizeof(Node));
    	strcpy(p->word,words);
    	p->next = head;
    	head = p;
    Code:
     char *getline( char *text)
    {
    	if( fgets( word, 128, stdin ) == NULL ) return NULL;
    	if( ( temp = strchr( words, '\n' ) ) )
    		*temp = 0;
    	else while( getchar() != '\n' );
    	return words;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Use a loop where you call strtok() with NULL as the first parameter. Do this until you get a NULL back from strtok() [whcih means "no more to find"].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    thanks i used a while loop instead
    Code:
            tok = strtok(word, " ");
    	while (tok != NULL) {
    	p=(Node *) malloc(sizeof(Node));
        	strcpy(p->word,tok);
    	p->next = head;
    	head = p;
    	tok = strtok(NULL," ");}

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. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM