Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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;

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

 
Reply With Quote
 
 
 
 
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
> {
> 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

 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
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.
 
Reply With Quote
 
placid
Guest
Posts: n/a
 
      08-23-2005
wait now i didnt see that befor i thought i was using %d

 
Reply With Quote
 
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


 
Reply With Quote
 
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I transform source range to destination range that is thesame as source? Lambda C++ 2 07-16-2008 05:18 PM
range() is not the best way to check range? Summercoolness@gmail.com Python 46 07-25-2006 08:10 PM
DNS to certain IP range on internal network not resolving? atsadmin Cisco 0 07-25-2006 04:33 PM
resolving a list of numbers to a range placid C Programming 13 08-22-2005 03:40 AM
Scene range vs dynamic range Robert Feinman Digital Photography 2 07-04-2005 09:30 PM



Advertisments