Posted by Driver on 01:44:00 03-25-2002
k, I'm working on this program for my programming class, and I've hit a wall, and was wondering if someone could explain why this doesn't work they way I think it should.
I have to write a very simple "ed" program, using linked-lists, and my append doesn't work very well.
When I call it, and then try to print it, only the first line works.
Heres my code that I'm having trouble with (I took out the extra stuff to clean it up):
#include
#include
#include
#include
typedef struct line {
char letter;
struct line * next;
}words;
typedef struct col_rows {
words * content;
struct col_rows * next;
}rows;
int main( int argc, char *argv[] )
{
rows *data, *first_row;
FILE *input=NULL;
if( (data=( rows * )malloc( sizeof( rows ))) == NULL )
exit( -1 );
if( (data--->content = (words *) malloc( sizeof( words ))) == NULL )
{
free( data );
exit( -1 );
}
data--->next=NULL;
data--->content--->next = NULL;
first_row=data;
do{
fgets( menu, 256, stdin );
switch( menu[0] )
{
case 'q': break;
case '-': if( current_row-1 content;
do
{
printf( "Please enter data: " );
fgets(line,256,infile);
if( temp_ptr == NULL ){
if( (temp_ptr = (words *) malloc( sizeof( words ) )) == NULL )
break;
else
printf( "Malloc'd \n" );}
for( i=0; inext = NULL;
new_row--->content=NULL;
temp_ptr = new_row--->content;
(*data_ptr)--->next = new_row;
(*data_ptr)=(*data_ptr)--->next;
break;
default:
insert_row( &temp_ptr, c );
temp_ptr=temp_ptr--->next;
break;
}
if( cont == 1 )
break;
}
}while( (line != ".") && !feof( infile ) && !cont );
return( counter );
}
void insert_row( words **current_ptr, char value )
{
words *next_ptr;
if( (next_ptr = (words *) malloc( sizeof( words ))) != NULL )
{
next_ptr--->next = NULL;
next_ptr--->letter=value;
(*current_ptr)--->next=next_ptr;
}
}
void find_row( rows **start_ptr, int row_seek )
{
int i;
if( (*start_ptr) == NULL )
return;
else
for( i=0; inext != NULL )
(*start_ptr)=(*start_ptr)--->next;
}
output_lines( (*start_ptr), 0 );
}
void output_lines( rows *data_ptr, int mode )
{
words *temp;
temp = data_ptr--->content;
while( temp != NULL )
{
printf( "%c", temp--->letter );
if( temp--->next != NULL )
temp=temp--->next;
else
break;
}
printf( "\n" );
}
Any help would be greatly appreciated!! Thx guys.
Posted by Driver on 01:48:00 03-25-2002
Sorry, forgot to mention the prob.
The linked list seems to work for the first time through, but then when I try to print out the other stuff, it doesn't.
INPUT:
a
hi there
how's it going
.
/*stops on period*/
-
/*suppose to print previous line*/
finding. . .
-
/*same thing */
finding. . . .hi there
PS - the # includes were stdio, stdlib, ctype & string
Posted by MoX on 13:27:00 03-25-2002
Some things could be made better in your code
1.) You can make the memory allocation for data and data_content much easier using 'new' like this: rows *data = new rows;
2.) Your insert_line() func will not work...It will always be the end of the single-linked list, 'cause its next-pointer is NULL. Hey...I just saw that you use insert() in append() to do the actual appending...That will be the source of your problem!!!
3.) Why does your append() work with a rows**, which is an array of rows* what you probably don'T want here...Wouldn't it be enough to just give it a rows*? Then you can at least replace the dereferencing (*data_ptr)--->content with a simpler data_ptr--->content
Don't feel too critized from all my advices! I just hope that I could help you out a bit.
Just don't give up. Linked lists are powerful but their usage is full of traps! Believe me, I had to try a long time 'till I was able to handle them properly!
[addsig]
Posted by MoX on 14:36:00 03-25-2002
btw: Perhaps you like my tutorial on this topic! When I look at your code I think that you already got the theory but maybe you find it useful though. Don't forget to rate it if you read it through!
[addsig]
Posted by seunosewa on 18:06:00 03-31-2002
I used to be a fan of C until I discovered the C++ Standard Template Library. Its easy to use once you get the hang of it, and most times its as efficient as any hand-crafted C code. More advantages: You don't have to struggle with bugs like this one any longer, and your code will be less bulky and easier to read by other C++ programmers who understand the Sandard Template Library!
----
The best code is the one you don't write!
----
Sorry, I got the context of your earlier post wrong - I wanted to make that point so badly!
[ This Message was edited by: seunosewa on 2002-03-31 18:07 ]
Posted by MoX on 18:50:00 03-31-2002
Quote:
----
The best code is the one you don't write!
Yes, it's true. The STL is indeed powerful and useful. But I like to know what's going on behind the scenes and to get my hands dirty. So I try to code it myself rather than using other ppl's stuff. My code may become less efficient this way, but I learn a lot.
And if I always wanted to use code written by other ppl I had never started to code...
[addsig]