Discuss Printing List Reversing Lines in the C Programming forum on Dev Shed. Printing List Reversing Lines C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
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>}
Posts: 118
Time spent in forums: 3 Days 18 h 48 m 29 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 .....
Posts: 118
Time spent in forums: 3 Days 18 h 48 m 29 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;
}