On Mon, 05 May 2008 18:55:44 -0700, Barry Schwarz wrote:
ok, I changed the name to string_copy
yes, I could have used array-indexing too but I am quite weak at
understanding pointers, so I used them to get an idea about them.
getword() will prompt the user and return a useful value.
I am from C++ background and I never ever used Arrays in C++, neither
any linked list. At present I don't understand much how these linked lists
work. I am just confused about them but I try to learn them. I have read
the section 6.5 8 times but still don't get the basic idea of why used
a linked list and how they work.
yes, right, anything NULL needs a new node as the word ha snot seen
before.
I though that code will make this:
N1 -- N2 --N3 ---....
what is the meaning of N2a and N2b, it is not a doubly-linked list. I
thought you mean this:
N2
/ \
/ \
N2a N2b
I simply have a single linked list:
N1 -- N2 --N3 --N4 --....
I don't know. SInce mt program is quite raw and my understanding of linked
lists and pointers is quite weak, so I was just avoiding any garbage value
by using "else if".
so, should I return p->next ?
that is not what I want. I want to return the address of latest node
created or address of the last node, if the word is already in the list.
I use strcpy from library . Library does not do any check for me?
oh.. I am getting frustrated now.. :(
okay, at least one thing in my program is good but getword() is just
a modification of K&R2's getword, it is not my creation :(
can you provide some hints on coding this program ?
or
you think a doubly-linked list is a good idea ?
--
my email ID is @ the above blog.
just check the "About Myself" page :)
>On Tue, 06 May 2008 10:18:12 +0500, arnuld <sunrise@see.si gs.invalid>
>>char *strdup( char * );
>>char *strdup( char * );
This function name belongs to the implementation but that's also just
another nit for the time being.
another nit for the time being.
> psnode = arr_psnodes;
> psnode_last = psnode + (ARRSIZE - 1);
> psnode_last = psnode + (ARRSIZE - 1);
This works but you could accomplish the same thing by simply keeping a
count of the number of elements of arr_psnodes that are in use.
count of the number of elements of arr_psnodes that are in use.
understanding pointers, so I used them to get an idea about them.
> while( getword( word, WORDSIZE ) && (psnode != psnode_last) )
Nowhere do you prompt the user for input.
> if( isalpha(word[0]) )
>> {
>> root_node = add_to_snode( root_node, word ); *(psnode++) = root_node;
>> {
>> root_node = add_to_snode( root_node, word ); *(psnode++) = root_node;
This does not do what you want. add_to_snode always adds new nodes at
the end but returns the address of the first node.
the end but returns the address of the first node.
any linked list. At present I don't understand much how these linked lists
work. I am just confused about them but I try to learn them. I have read
the section 6.5 8 times but still don't get the basic idea of why used
a linked list and how they work.
> if( p == NULL )
For the first node (N1), this is true immediately.
>
On initial entry for the second node (N2a), this is false.
>
On the recursive entry for the second node (N2b), this is true.
>
On initial entry for the second node (N2a), this is false.
>
On the recursive entry for the second node (N2b), this is true.
yes, right, anything NULL needs a new node as the word ha snot seen
before.
> if( !p )
>> {
>> fprintf( stderr, "out of memory\n" ); exit( EXIT_FAILURE );
>> }
>> }
> p->word = strdup( w );
> p->count = 1;
> p->next = NULL;
>> {
>> fprintf( stderr, "out of memory\n" ); exit( EXIT_FAILURE );
>> }
>> }
> p->word = strdup( w );
> p->count = 1;
> p->next = NULL;
(N1) You build struct_1 with "a", 1, and NULL.
>
(N2b) You build struct_2 with "b", 1, and NULL.
>
(N2b) You build struct_2 with "b", 1, and NULL.
I though that code will make this:
N1 -- N2 --N3 ---....
what is the meaning of N2a and N2b, it is not a doubly-linked list. I
thought you mean this:
N2
/ \
/ \
N2a N2b
I simply have a single linked list:
N1 -- N2 --N3 --N4 --....
Is there any possibility the if could evaluate to false? If it is
always true, a simple else would work just as well.
always true, a simple else would work just as well.
I don't know. SInce mt program is quite raw and my understanding of linked
lists and pointers is quite weak, so I was just avoiding any garbage value
by using "else if".
> p->next = add_to_snode( p->next, w );
(N2a) So you call this function recursively with NULL and "b".
>
When N2b returns here, you change struct_1 to point to struct_2 (instead
of NULL) but p still points to struct_1.
>
When N2b returns here, you change struct_1 to point to struct_2 (instead
of NULL) but p still points to struct_1.
so, should I return p->next ?
(N1) And you return the address of struct_1 which main will store in
root_node.
>
(N2b) And you return the address of struct_2 to N2a.
>
(N2a) And you return the address of struct_1 which main will store again
in root_node.
root_node.
>
(N2b) And you return the address of struct_2 to N2a.
>
(N2a) And you return the address of struct_1 which main will store again
in root_node.
that is not what I want. I want to return the address of latest node
created or address of the last node, if the word is already in the list.
The routine that calls strdup does not check if it succeeded. You
either need to check here or everywhere
either need to check here or everywhere
I use strcpy from library . Library does not do any check for me?
Actually, the extra letters are not discarded. They are left in the
input buffer and will be processed on the next call to getword as if
they were the start of a new word.
input buffer and will be processed on the next call to getword as if
they were the start of a new word.
But bullet-proofing can come later and this is not an unreasonable
assumption to start with. By isolating getword in a separate function
you have made it easier to fix this problem when you get around to it.
assumption to start with. By isolating getword in a separate function
you have made it easier to fix this problem when you get around to it.
okay, at least one thing in my program is good but getword() is just
a modification of K&R2's getword, it is not my creation :(
can you provide some hints on coding this program ?
or
you think a doubly-linked list is a good idea ?
--
my email ID is @ the above blog.
just check the "About Myself" page :)
Comment