Try these while loops instead EDITED to note Chris' comment about checking for end of LL.
Try these while loops instead EDITED to note Chris' comment about checking for end of LL: while (pos! = NULL) { while (z! = NULL) { // if you've reached the line feed or the end of the linked list if ((z->val == '\n') || (z->next == NULL)) { pos = z->next; // store list position just after this char for next time through break; } z = z->next; } while (z!
= NULL) { printf("%c", z->val); z = z->prev; // don't print more than just this word! : if ((z! = NULL) && (z->val == '\n')) break; } // reset z for top inner while loop z = pos; } Basic issue was that z was not reset when the outer while loop wrapped around; second issue was that end of linked list didn't break out of first inner while loop; third issue was second inner while loop didn't check for end of the word it was printing.
You also need to free the linked list at the end or it'll be a memory leak. You should also check the return value of calloc() to be sure it didn't return null.
Definately progress, although when I enter such like Damien\n The output I get it that off . \n\n then the program crashes – user1048116 Nov 18 at 21:41 You need to handle the beginning of the input, which isn't terminated by a '\n' character, but still needs to be printed. But this is close.
– Chris Lutz Nov 18 at 21:43 Would that be something I would need to implement in a seperate loop, or something I could integrate into the above – user1048116 Nov 18 at 21:47 Looking at your linked list creation, won't all the characters be inserted in reverse order? Also, can you debug the program to locate the crash point? Also note you're not freeing the linked list when main() finishes which is a memory leak.
Oh, and not checking the return of calloc() that may fail if the OS runs out of memory. – Nick Shaw Nov 18 at 21:49 Chris, any little hints possible, Im really stumped on what IF statement to add in order to get that last word printed! – user1048116 Nov 18 at 21:56.
I think your structure needs to be changed and there is no reason to have a double linked list to solve your problem. Your struct should contain struct node { char *word; struct node *next; }; Then your main loop should be something like: 1) Read character data until delimiter into expandable buffer. Add NULL string terminator.2) When delimiter is reached create node that points to buffer.3) Insert NODE at HEAD of list.4) When '.' is reached print each string starting from head of list.
Sorry, but the task specifies I cannot use Arrays/Strings. – user1048116 Nov 18 at 22:30 How about instead then: a linked list where each entry in the list contains a word which is a linked list of characters? – Nick Shaw Nov 19 at 9:16.
Ok, I've had time to see why my first answer wouldn't work - a few little obvious things if you run the code through a debugger. So here's a fully working version. It could probably be optimised quite a bit, but it follows the same structure as your original code so hopefully you can follow it: typedef struct L { char val; struct L *next; struct L *prev; } List; List* insertList( char val, List *t1 ) { List *t = calloc(1, sizeof( List )); t->prev = NULL; t->val = val; t->next = t1; if (t1!
= NULL) t1->prev = t; return t; } List* createList( void ) { List *h = NULL; char c; do { c =(char)getchar(); h = insertList( c, h ); } while (c! = '. '); return h; } void freeList( List *list ) { // free the linked list if (list!
= NULL) { freeList( list->next ); free( list ); } } int main( void ) { // create list List *head = createList(); List *pos = head, *currentChar = NULL, *wordStart = NULL; while (pos! = NULL) { // find next word wordStart = NULL; while (pos! = NULL) { // gone past the beginning of a word yet?
If ((pos->val == '\n') || (pos->next == NULL)) { wordStart = (pos->next == NULL)? Pos : pos->prev; // note where this word starts pos = pos->next; // jump over \n, or go to end of list (null), for next time into this loop break; // jump out of this loop so we can output the word } else // not found end of word yet - on to next char pos = pos->next; } // have we found a word? If so, output it!
If (wordStart! = NULL) { currentChar = wordStart; // start at first char in the word while (currentChar! = NULL) { printf( "%c", currentChar->val ); // output this char currentChar = currentChar->prev; // on to next char in the word if ((currentChar!
= NULL) && (currentChar->val == '\n')) break; // stop after last char of the word } // print the line-feed just before wordStart (if there is one) if (wordStart->next! = NULL) printf( "%c", wordStart->next->val ); } else // we've reached the end - stop break; // not really necessary - pos should be NULL at this point anyway } freeList( head ); // free linked list memory return 0; } The main change is how I output the linefeed. I realised it's NOT the linefeed after each word you need, but the one just BEFORE it (not logical at all - I wonder if that is what the question originally intended?).
But it now outputs exactly what you require. And I've added a function to free the linked list memory at the end too for you. :).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.