resolving a linked-list of numbers to a range

Discussion in 'C Programming' started by placid, Aug 23, 2005.

  1. placid

    placid Guest

    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
    placid, Aug 23, 2005
    #1
    1. Advertising

  2. placid

    AM Guest

    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
    AM, Aug 23, 2005
    #2
    1. Advertising

  3. placid

    placid Guest

    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
    placid, Aug 23, 2005
    #3
  4. In article <>,
    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


    > 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.
    Walter Roberson, Aug 23, 2005
    #4
  5. placid

    placid Guest

    wait now i didnt see that befor i thought i was using %d
    placid, Aug 23, 2005
    #5
  6. placid

    CBFalconer Guest

    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
    CBFalconer, Aug 23, 2005
    #6
  7. placid

    placid Guest

    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
    placid, Aug 24, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    479
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    464
    emerth
    Jul 10, 2003
  3. placid

    resolving a list of numbers to a range

    placid, Aug 18, 2005, in forum: C Programming
    Replies:
    13
    Views:
    462
    placid
    Aug 22, 2005
  4. fool
    Replies:
    14
    Views:
    504
    Barry Schwarz
    Jul 3, 2006
  5. joshd
    Replies:
    12
    Views:
    665
    John Carson
    Oct 2, 2006
Loading...

Share This Page