what the second if is checking?
what the second if is checking?
So your trying to write your own strcmp() function?
no !!
why do you think that
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.
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); . . . . }
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); . . . . }
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); . . . . }
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)
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.
Hmm..the word "Software engineering" would soon lose its meaning and in its place we would have "Undefined Behavior engineering".(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
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