#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    2

    Arrow Printing List Reversing Lines


    I'm trying to print a list where the lines of text are reversed. For example entering:
    Hello
    world.

    would print:
    world
    Hello

    So far I've got:
    Code:
    #include <stdio.h>
    
    typedef struct list {
    	char c;
    	struct list *next;
    } list ;
    
    void printlist(list *b) {
    	list *newline=NULL;
    	while( b!=NULL ) {
    		if (b->c=='\n') {
    	   		return;
    	  	}
    	  	printlist(b->next);
    	  	printf("%c",b->c);
    		return;
    	  }
    }
    
    list *insertlist( char ch, list *temp ) {
    	list *t = calloc( 1, sizeof( list ) );
    	t->c = ch; 
    	t->next = temp;
    	return t;
    }
    
    int main ( void ) {
    	char c;
    	list *b = NULL;
    	while(1) {
    		c=getchar();
    		if( c == '.' ) {
    			break;
    		} else {
    			b = insertlist( c, b ); 
    		}
    	} 
    	printlist(b);
    	printf("\n");
    	return 0;
    }
    which, using the example input, prints:
    world.

    I'm sure it's to do with the printlist function but I'm not entirely sure how to update it so it loops to the next new line.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by EffX
    I'm trying to print a list where the lines of text are reversed. For example entering:
    Hello
    world.

    would print:
    world
    Hello

    So far I've got:
    Code:
    #include <stdio.h>
    
    typedef struct list {
    	char c;
    	struct list *next;
    } list ;
    
    void printlist(list *b) {
    	list *newline=NULL;
    	while( b!=NULL ) {
    		if (b->c=='\n') {
    	   		return;
    	  	}
    	  	printlist(b->next);
    	  	printf("%c",b->c);
    		return;
    	  }
    }
    
    list *insertlist( char ch, list *temp ) {
    	list *t = calloc( 1, sizeof( list ) );
    	t->c = ch; 
    	t->next = temp;
    	return t;
    }
    
    int main ( void ) {
    	char c;
    	list *b = NULL;
    	while(1) {
    		c=getchar();
    		if( c == '.' ) {
    			break;
    		} else {
    			b = insertlist( c, b ); 
    		}
    	} 
    	printlist(b);
    	printf("\n");
    	return 0;
    }
    which, using the example input, prints:
    world.

    I'm sure it's to do with the printlist function but I'm not entirely sure how to update it so it loops to the next new line.


    try using following function:

    void printlist(list *b)
    {
    list *e=b;
    while( (e!=NULL) && ((e->c)!='\n') )
    e=e->next;
    while(b!=e)
    {
    printf("%c",b->c)
    b=b->next;
    }
    if(b!=NULL)
    printlist(b->nxt);
    return;
    }



    and i think u should use getche() instead of getchar()

    Comments on this post

    • bdb disagrees : you need a special library (and OS) to use getche(); getchar() is available for all C implementations.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by abhiakhi
    try using following function:

    void printlist(list *b)
    {
    list *e=b;
    while( (e!=NULL) && ((e->c)!='\n') )
    e=e->next;
    while(b!=e)
    {
    printf("%c",b->c)
    b=b->next;
    }
    if(b!=NULL)
    printlist(b->nxt);
    return;
    }



    and i think u should use getche() instead of getchar()
    drawback of getchar() {in <stdio.h> is that you will have to press enter key each time you enter a character, this is not the case with getche() {in <conio.h>}
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    2
    That just prints
    dlrowolleh

    I need it to print:
    world
    Hello
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    83
    I would suggest that you rethink your printlist function. You're using a singlular linked list which would make it extremely difficult to reverse a string. I would suggest that you use a doubly linked list. Using this type of linked list will allow you to traverse backwards from the end of the string to the first space character. At that point you would traverse forward toward the end of the string, printing out each character as you go. You would then traverse back to the position of original space character and then continue to traverse back to the next space character or the beginning of the string, whichever comes first. At which point you would traverse forward to the first space character, printing out each character as you go. And on and on .....

    My test code produces the following:

    Hello World and all the aliens out there.
    is reversed to:
    there out aliens the all and World Hello
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    The first step would be to store actual words in your linked list, rather than individual letters.
    Code:
    typedef struct list {
    	char word[30];
    	struct list *next;
    } list ;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    2
    Thanks, I can't really get my head round how to change the insertlist function to incorporate a double linked list though.

    Any help?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    83
    Thanks, I can't really get my head round how to change the insertlist function to incorporate a double linked list though.

    Any help?
    Listed below is a basic double linked list example which is heavily commented to serve as a tutorial.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // prev points to the memory location of the previous node in the list.
    //  If there are none we point to NULL. Also, the first node prev pointer will point to NULL
    // next points to memory location of the next node in the list.
    //  If there are no next nodes, next will point to NULL.  For example, the last node in the 
    //  linked list will have its next point point to NULL since there are obviously no nodes
    //  after the last node.
    typedef struct list {
    	char c;
    	struct list *prev;
    	struct list *next;
    } list;
    
    // The head pointer marks the starting point of the link list
    // The tail pointer marks the end of the link list.
    // These two pointers allow you to traverse the doubly linked
    //  list from beginning to end and vice versa
    
    struct list *head = NULL, *tail = NULL;
    
    
    void insertlist( list *lnode )
    {
    	// Add the node to an empty list
    	if(head == NULL)
    	{
    		// head pointer points to first and only node
    		head = lnode;
    		// prev pointer points to null since ther is no previous node
    		lnode->prev = NULL;
    	}
    	// Append node to end of list
    	else
    	{
    		// Set the current tail's next pointer to the new node
    		tail->next = lnode;
    		// Set new node prev pointer to current tail
    		lnode->prev = tail;
    	}
    	// Point the tail to the new node;
    	tail = lnode;
    	// Finally point the new node next to NULl since ther is no next record
    	lnode->next = NULL;
    }
    
    int main ( void ) 
    {
    	list *b = NULL;
    	// Allocate memory for the new node
    	if(( b = ( struct list * )malloc( sizeof(struct list ))) == NULL )
    	{
    		printf( "malloc failed\n" );
    		return -1;
    	}
    	b->c = 'a';
    	insertlist( b ); 
    	// Allocate memory for another new node;
    	if(( b = ( struct list * )malloc( sizeof(struct list ))) == NULL )
    	{
    		printf( "malloc failed\n" );
    		return -1;
    	}
    		b->c = 'b';
    	insertlist( b ); 
    
    	// Traverse forward
    	for( b = head; b != NULL; b = b->next ) 
    		printf( "%c\n", b->c );
    
    	printf("\n\n\n");
    	// Traverse backward
    	for( b = tail; b != NULL; b = b->prev ) 
    		printf( "%c\n", b->c );
    
    	return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo