resolving a linked-list of numbers to a range

P

placid

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
{
IntNode *head;
int count;

}ListHead

.....

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
 
A

AM

placid said:
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
{
IntNode *head;
int count;

}ListHead

....

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
 
P

placid

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
 
W

Walter Roberson

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.
 
C

CBFalconer

placid said:
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:
 
P

placid

CBFalconer said:
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
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top