B
Barry Schwarz
ok, I changed the name to string_copy
Avoid creating function names beginning with str.
snip
getword() will prompt the user and return a useful value.
The getword you provided contained no prompt.
snip
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 ---> ....
It does build a linked list like that. But it always returns the
address of N1 which is what you store in arr_psnodes. At the end each
element of arr_psnodes will contain the address of the first struct
which is not what you want.
what is the meaning of N2a and N2b, it is not a doubly-linked list. I
thought you mean this:
Since you call the same function individually for each new node and
recursively to determine if word already exists, I used this notation
to indicate which invocation of the function the comment applied to.
N1 means the comment applies to the initial call for the first node.
N2a means the comment applies to the initial call for the second node.
N2b means the recursive call for the second node. You should read the
N1 comments first, then the N2a ones, etc.
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".
This has nothing to do with linked lists and pointers. Your previous
if checked !match. This else if checked match. If the first if was
true, the else prevents the second if from being evaluated. If the
first if is false, then match must be true and you don't need to test
it.
so, should I return p->next ?
That only solves the problem for the second struct. The problem will
reappear for the third.
One way to solve the problem is to not fill in arr_psnodes until you
are done. Then simply walk through the list saving addresses in
arr_psnodes until p->next is NULL>
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?
strdup calls malloc. malloc can fail and strdup checks before calling
strcpy. However, strdup returns the malloc value whether it is valid
or NULL. You will then use this value in both your sort and print
routines which will lead to undefined behavior if they don't check.
Rather than check in multiple places, it is better if the routine that
calls strdup checks immediately and takes appropriate action when
strdup fails.
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 ?
No one mentioned doubly linked lists which seems like overkill for
this problem to me.
Remove del for email