C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 7th, 2012, 06:10 AM
EffX EffX is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 38 EffX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
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.

Reply With Quote
  #2  
Old December 7th, 2012, 09:45 AM
abhiakhi abhiakhi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 abhiakhi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 32 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #3  
Old December 7th, 2012, 10:00 AM
abhiakhi abhiakhi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 abhiakhi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 32 sec
Reputation Power: 0
Quote:
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>}

Reply With Quote
  #4  
Old December 7th, 2012, 11:21 AM
EffX EffX is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 38 EffX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
That just prints
dlrowolleh

I need it to print:
world
Hello

Reply With Quote
  #5  
Old December 7th, 2012, 07:17 PM
BobS0327 BobS0327 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 120 BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 19 h 7 m 57 sec
Reputation Power: 44
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:

Quote:
Hello World and all the aliens out there.

is reversed to:
Quote:
there out aliens the all and World Hello

Reply With Quote
  #6  
Old December 7th, 2012, 11:52 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,907 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 39 m 56 sec
Reputation Power: 1774
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

Reply With Quote
  #7  
Old December 8th, 2012, 11:34 AM
EffX EffX is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 38 EffX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
Thanks, I can't really get my head round how to change the insertlist function to incorporate a double linked list though.

Any help?

Reply With Quote
  #8  
Old December 8th, 2012, 03:05 PM
BobS0327 BobS0327 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 120 BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 19 h 7 m 57 sec
Reputation Power: 44
Quote:
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;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Printing List Reversing Lines

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap