Y
yudi1226
Oh,sorry!
Ben Pfaff said:Please don't post C++ to comp.lang.c.
Ben Pfaff said:Please don't post C++ to comp.lang.c.
nrk said:If space and recursion are issues, consider reversing the list
(non-recursive, O(1) space, O(n) time), then printing the contents, then
reversing it again. Better than the O(n^2) stop element solution you've
proposed.
-nrk.
Peter said:assert(head != NULL); /* !! */
Morris Dovey said:My DS-9000 could do it without filling its top-level cache. (-:
For any list being procesed by someone who needs to ask this
question, the scenario you posit seems improbable - and for
reasonable list sizes, I think it'd be a not-too-difficult and
instructive exercise - and hardly cruel.
Of course, it's also possible to count the list elements and
malloc an appropriate-sized array that could be filled backwards,
puts'd, and freed - I guess it'd all depend on whether the
exercise is intended to focus on memory management functions or
to explore solution techniques.
So, what does junky_fellow do if there isn't room to build this
horrendous string and malloc fails? Well, he could also consider
making a pass through the list for each character, moving the
stop point back by one element for each pass...
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.
junky_fellow said:nrk <[email protected]> wrote in message
seems to be a nice solution. This is what i exactly wanted. i would also
suggest one improvement i.e. first reverse it, then reverse it while you
are printing. No need to first print and then reverse.
anyway, thanx a lot.
pete said:I don't swing that way.
Peter said:You don't think the function should handle
the case where 'head' is an empty list (i.e. a null pointer)?
pete said:No. There's no need to pass NULL to such a function.
You can check for NULL prior to calling the function,
if you have an algorithm which might attempt something like that.
Peter said:pete said:No. There's no need to pass NULL to such a function.
You can check for NULL prior to calling the function,
if you have an algorithm which might attempt something like that.
I see a _requirement_ for handling empty lists here on the grounds of
completeness.
[For instance, I'm glad strlen() works on strings where the
first byte is null.]
pete said:Peter said:pete said:Peter Nilsson wrote:
[re: node *list_reverse(node *head) ]
You don't think the function should handle
the case where 'head' is an empty list (i.e. a null pointer)?
No. There's no need to pass NULL to such a function.
You can check for NULL prior to calling the function,
if you have an algorithm which might attempt something like that.
I see a _requirement_ for handling empty lists here on the grounds
of completeness.
[For instance, I'm glad strlen() works on strings where the
first byte is null.]
The function takes a pointer to a list.
NULL doesn't point to an empty list.
Peter said:[For instance, I'm glad strlen() works on strings where the
first byte is null.]
I just don't understand your philosophy of excluding
a perfectly consistent
and unambiguous null pointer as an empty list.
yudi1226 said:fflush(stdin);
yudi1226 said:I'm so sorry!
The follow :
/***************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct _linklist
{
char data ;
struct _linklist *next ;
} LINKLIST;
int main()
{
LINKLIST * ll_begin,* ll_end ;
int i ;
LINKLIST *ll_store_end,*ll_store_begin ,*ll_cur;
LINKLIST *ll_temp ;
ll_begin = NULL ;
ll_end = NULL ;
for(i = 0 ; i < 5 ; i++ )
{
ll_temp = (LINKLIST *)malloc(sizeof(LINKLIST)) ;
printf("Pls input :") ;
scanf("%c",&ll_temp->data);
if(ll_begin == NULL )
{
ll_begin = ll_temp ;
ll_end = ll_temp ;
}
else
{
ll_end->next = ll_temp ;
ll_end = ll_temp ;
ll_end->next = NULL ;
}//if(ll_begin == NULL )
fflush(stdin);
} //for(int = 0 ; i < 5 ; i++ )
ll_store_end = ll_end ;
ll_store_begin = ll_begin;
//Loop LINKLIST
ll_store_end->next = ll_store_begin ;
ll_cur = ll_store_end ;
ll_store_begin = ll_store_end ;
do
{
printf("%c", ll_cur->data) ;
while(ll_cur->next != ll_store_end)
{
ll_cur = ll_cur->next ;
}
ll_store_end = ll_cur ;
}while(ll_cur != ll_end) ;
printf("\n");
//destructor loop LINKLIST
ll_end->next = NULL ;
return 0 ;
}
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.