Thread: complete the gaps

  1. #16
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what the second if is checking?

  2. #17
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    So your trying to write your own strcmp() function?

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    no !!

    why do you think that

  4. #19
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    The first block is going to handle cases where the new element goes at the head of the list. The other block handles cases where the new block goes elsewhere.

    The conditional statements in each case are going to be similar, using this rule - "its supposed to be sorted by length and if two words have equal length then by dictionary order" Note that there are two conditions here and they match up with the two conditional statements in each block.

    The best way to figure out how to make the pointers line up is to draw up an example for yourself. The new node's going to be inserted between temp and temp2. You'll need to adjust the next pointers of both the temp node and the new node to make that happen.

  5. #20
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried the first block
    the next of head shoud be temp,i cant do malloc and assogng values and change head in two lines
    Code:
    #include <stdio.h>	
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct wordStruct{
    	char string[256];
    	struct wordStruct *next;
    } wordStruct;
    
    wordStruct * wordAlloc(char *str){
    	wordStruct *temp;
    	temp = (wordStruct *)malloc(sizeof(wordStruct));
    	temp->next=NULL;
    	strcpy(temp->string,str);
    	return temp;
    } 
    
    wordStruct *insert( wordStruct *head, char *st){
    	wordStruct* temp, *temp2;
    	
    	if(head == NULL) return wordAlloc(st);
    	
    	if((strlen(st)>strlen(head->string))|| ((strlen(st)==strlen(head->string) &&(strcmp(st,head->string))>1)){
    		temp =(wordStruct *) malloc(sizeof(wordStruct));
    		temp->string=st;
    		return temp;
    	}
    	
    	for(temp=head; temp->next ; temp = temp -> next){
    		if(?? 11 ??)break;
    		if( ?? 12 ??&& ?? 13 ??)break;
    	}	
    	
    	temp2 = temp->next;
    	?? 14 ??;
    	?? 15 ??;
    	return head;	
    }
    
    void main(){	
    	char word[256];
    	wordStruct *list = NULL;
    	
    	while(scanf("%s", word)==1)
    		list = insert(list, word);				
    	. . . .	
    }

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    the this part is harder
    Code:
    #include <stdio.h>	
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct wordStruct{
    	char string[256];
    	struct wordStruct *next;
    } wordStruct;
    
    wordStruct * wordAlloc(char *str){
    	wordStruct *temp;
    	temp = (wordStruct *)malloc(sizeof(wordStruct));
    	temp->next=NULL;
    	strcpy(temp->string,str);
    	return temp;
    } 
    
    wordStruct *insert( wordStruct *head, char *st){
    	wordStruct* temp, *temp2;
    	
    	if(head == NULL) return wordAlloc(st);
    	
    	if((strlen(st)>strlen(head->string))|| ((strlen(st)==strlen(head->string) &&(strcmp(st,head->string))>0)){
    		temp = insert(NULL,st);
    		temp->next=head;
    		return temp;
    	}
    	
    	for(temp=head; temp->next ; temp = temp -> next){
    		if(?? 11 ??)break;
    		if( ?? 12 ??&& ?? 13 ??)break;
    	}	
    	
    	temp2 = temp->next;
    	?? 14 ??;
    	?? 15 ??;
    	return head;	
    }
    
    void main(){	
    	char word[256];
    	wordStruct *list = NULL;
    	
    	while(scanf("%s", word)==1)
    		list = insert(list, word);				
    	. . . .	
    }

  7. #22
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    is it correct?
    Code:
    #include <stdio.h>	
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct wordStruct{
    	char string[256];
    	struct wordStruct *next;
    } wordStruct;
    
    wordStruct * wordAlloc(char *str){
    	wordStruct *temp;
    	temp = (wordStruct *)malloc(sizeof(wordStruct));
    	temp->next=NULL;
    	strcpy(temp->string,str);
    	return temp;
    } 
    
    wordStruct *insert( wordStruct *head, char *st){
    	wordStruct* temp, *temp2;
    	
    	if(head == NULL) return wordAlloc(st);
    	
    	if((strlen(st)>strlen(head->string))|| ((strlen(st)==strlen(head->string) &&(strcmp(st,head->string))>0)){
    		temp = insert(NULL,st);
    		temp->next=head;
    		return temp;
    	}
    	
    	for(temp=head; temp->next ; temp = temp -> next){
    		if(strlen(temp->string)>strlen(st))break;
    		if( (strlen(temp->string)==strlen(st))&&(strcmp(st,temp->string))>0) )break;
    	}	
    	
    	temp2 = temp->next;
    	temp->next=insert(NULL,st);
    	temp->next->next=temp2;
    	return head;	
    }
    
    void main(){	
    	char word[256];
    	wordStruct *list = NULL;
    	
    	while(scanf("%s", word)==1)
    		list = insert(list, word);				
    	. . . .	
    }

  8. #23
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Have you ever noticed that you're actually capable of working through a problem yourself when the first thing you do isn't post here and give up?

    In any case - does the above code do what it's supposed to do? If it does, I guess it's correct. (Though you're still using void main, which means your code won't compile, and if I wanted to test it for you, I'd have to change it)

  9. #24
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    actually the big break threw came after
    KCfromNC 's post

    i just need to know in general
    about each part
    like he did

    (regarding the void main its working purfectly well
    and its a question from a test of my course
    so my teacher has a problem not me)

    he told us to write main that way
    so i am just going after a good grade
    i realy dont care of anything else
    Last edited by transgalactic2; 07-03-2009 at 09:41 AM.

  10. #25
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    (regarding the void main its working purfectly well
    and its a question from a test of my course
    so my teacher has a problem not me)

    he told us to write main that way
    so i am just going after a good grade
    i realy dont care of anything else
    Hmm..the word "Software engineering" would soon lose its meaning and in its place we would have "Undefined Behavior engineering".
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  11. #26
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    is it correct?
    Code:
    void main(){	
    	char word[256];
    	wordStruct *list = NULL;
    	
    	while(scanf("%s", word)==1)
    		list = insert(list, word);
    	. . . .
    }
    Looks ok barring items in red. Unusual sort though ie one which sorts lengthwise and then uses lexicography for a tie-breaker - must be a pedagogical sort .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. First phases complete for space combat
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 01-22-2009, 09:42 AM
  2. binary tree complete check
    By ichijoji in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2004, 05:48 PM
  3. Replies: 14
    Last Post: 04-27-2003, 06:37 PM
  4. Doom Lord engine complete!
    By Leeman_s in forum Game Programming
    Replies: 8
    Last Post: 05-12-2002, 12:44 AM
  5. doom lord engine complete!
    By Leeman_s in forum C++ Programming
    Replies: 4
    Last Post: 04-25-2002, 08:41 PM