Velocity Reviews > resolving a linked-list of numbers to a range

resolving a linked-list of numbers to a range

placid
Guest
Posts: n/a

 08-23-2005
Thanks everyone i got it to work at last but how would we resolve
numbers inside a integer linked-list and ive been writing out
flow-chart for this problem but i cant seem to be getting my head
around it

here is a code that i wanted

typedef struct intnode
{
int data;
struct intnode *next;

}IntNode;

{
int count;

.....

void printList(ListHead * lh)
{
IntNode *current = lh->head;
int prevNum =0, x=0;
while (current != NULL)
{
if (prevNum > 0)
{
if( (prevNum+1) == current->data)
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != current->data)
{
printf("%s,%s",prevnum,current->data);
x=0; /* To display '-' for next series of numbers */
}
}
}
prevNum = current->data;
current = current->next;
}

}

the problem i think was i was thinking of a solution to this problem if
arrays where used now that i want to change it to LL structure im
confused, so any help will be appreciated again

AM
Guest
Posts: n/a

 08-23-2005

placid wrote:
> Thanks everyone i got it to work at last but how would we resolve
> numbers inside a integer linked-list and ive been writing out
> flow-chart for this problem but i cant seem to be getting my head
> around it
>
> here is a code that i wanted
>
> typedef struct intnode
> {
> int data;
> struct intnode *next;
>
> }IntNode;
>
> typedef struct listhead
> {
> int count;
>
>
> ....

If I understand your problem correctly then following data-structure
implementation may be more efficient.

typedef struct intnode
{
int start;
int count;
IntNode *next;
} IntNode;

So, for the input:
1 2 3 4 5 7 8 9 13

Link list nodes would be:
{1, 5} --> {7, 3} --> {13, 0} --> NULL

Thanks
-AM

placid
Guest
Posts: n/a

 08-23-2005
No, if the input is 1 2 4 5 21 30
{1} -> {3} -> {4} -> {5} -> {21}->{30}->NULL

my question is when i print it out i want it in a range so something
like

1-5,21,30

Walter Roberson
Guest
Posts: n/a

 08-23-2005
In article <(E-Mail Removed). com>,
placid <(E-Mail Removed)> wrote:
>Thanks everyone i got it to work at last but how would we resolve
>numbers inside a integer linked-list and ive been writing out
>flow-chart for this problem but i cant seem to be getting my head
>around it

> while (current != NULL)
> {
> if (prevNum > 0)
> {
> if( (prevNum+1) == current->data)
> {
> if(x==0)
> {
> printf("-");
> x=1;
> }
> }
> else if( (prevNum+1) != current->data)

That test is redundant unless you are working with double precision
where values might be "NaN" (Not A Number), in which case IEEE 754
specifies that a NaN does not compare equal to itself. But other than
that case, this if is just the negation of the previous if and so
is automatically true if you get there at all, because of the 'else'.

> printf("%s,%s",prevnum,current->data);

prevnum and current->data are integers, but %s is a format
specifier that expects a pointer to a null-terminated character array.
Try %d instead of %s .

Your logic is kind of strange, but strange algorithms are the
province of comp.programming not of comp.lang.c, which tries
to restrict itself to discussion of language features.

--
Ceci, ce n'est pas une idée.

placid
Guest
Posts: n/a

 08-23-2005
wait now i didnt see that befor i thought i was using %d

CBFalconer
Guest
Posts: n/a

 08-23-2005
placid wrote:
>
> No, if the input is 1 2 4 5 21 30
> {1} -> {3} -> {4} -> {5} -> {21}->{30}->NULL
>
> my question is when i print it out i want it in a range so
> something like
>
> 1-5,21,30

What, if anything, are you talking about? Include proper context.
Each usenet article should stand by itself. Even on google you can
do it, see below:

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

placid
Guest
Posts: n/a

 08-24-2005

CBFalconer wrote:
> placid wrote:
> >
> > No, if the input is 1 2 4 5 21 30
> > {1} -> {3} -> {4} -> {5} -> {21}->{30}->NULL
> >
> > my question is when i print it out i want it in a range so
> > something like
> >
> > 1-5,21,30

>
> What, if anything, are you talking about? Include proper context.
> Each usenet article should stand by itself. Even on google you can
> do it, see below:
>
> --
> "If you want to post a followup via groups.google.com, don't use
> the broken "Reply" link at the bottom of the article. Click on
> "show options" at the top of the article, then click on the
> "Reply" at the bottom of the article headers." - Keith Thompson

thanks for the tip