How to do fibonacci by linked list ( not by recursive)

Discussion in 'C++' started by fighterman19, Sep 5, 2003.

  1. fighterman19

    fighterman19 Guest

    because in linked list each node contains only one digit like

    curr->nodevalue_1= 0
    curr->nodevalue_2=1
    sum->nodevalue = curr->nodevalue_1 + curr->nodevalue_2

    so when the number go up to >10 or >100 and the linked list need another
    node (I can add the node to the front ) but how can do the sum ?

    ex: 89+144 = 233
    how can I add it to the sum linked list?
    please help ( remember not a recursive problem)
     
    fighterman19, Sep 5, 2003
    #1
    1. Advertising

  2. "fighterman19" <> wrote...
    > because in linked list each node contains only one digit like


    Is that a requirement or just your understanding of linked lists?

    >
    > curr->nodevalue_1= 0
    > curr->nodevalue_2=1
    > sum->nodevalue = curr->nodevalue_1 + curr->nodevalue_2
    >
    > so when the number go up to >10 or >100 and the linked list need another
    > node (I can add the node to the front ) but how can do the sum ?
    >
    > ex: 89+144 = 233
    > how can I add it to the sum linked list?
    > please help ( remember not a recursive problem)


    What's the problem? Can't you implement addition with carry-over?

    0 => carry.
    label_1:
    next_digit1 + next_digit2 + carry => result.
    if result > 9 then
    1 => carry.
    result - 10 => result.
    else
    0 => carry.
    end-if
    store result
    if have_more_digits go to label_1

    I don't see C++ language issue here, BTW.

    Victor
     
    Victor Bazarov, Sep 5, 2003
    #2
    1. Advertising

  3. fighterman19 wrote:

    > because in linked list each node contains only one digit like
    >
    > curr->nodevalue_1= 0
    > curr->nodevalue_2=1
    > sum->nodevalue = curr->nodevalue_1 + curr->nodevalue_2
    >
    > so when the number go up to >10 or >100 and the linked list need another
    > node (I can add the node to the front ) but how can do the sum ?
    >
    > ex: 89+144 = 233
    > how can I add it to the sum linked list?
    > please help ( remember not a recursive problem)
    >
    >


    Why on earth would you use a linked list for this? Your question doesn't
    make much sense.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Sep 5, 2003
    #3
  4. fighterman19

    red floyd Guest

    fighterman19 wrote:
    > because in linked list each node contains only one digit like
    >
    > curr->nodevalue_1= 0
    > curr->nodevalue_2=1
    > sum->nodevalue = curr->nodevalue_1 + curr->nodevalue_2
    >
    > so when the number go up to >10 or >100 and the linked list need another
    > node (I can add the node to the front ) but how can do the sum ?
    >
    > ex: 89+144 = 233
    > how can I add it to the sum linked list?
    > please help ( remember not a recursive problem)
    >


    May I suggest that you take a stab at doing your own homework, and then
    post your code, instead of asking us to do it for you?
     
    red floyd, Sep 5, 2003
    #4
  5. fighterman19

    fighterman19 Guest

    Where the hell in my post that you see I YOU or other people to do my
    homework for me?

    I just want to know how to handle this logic. Did I ask for the entire
    program?
    and if YOU can't do it (or whatsoever that make you think you are good and
    whoever ask in this group is stupid) so SHUT YOUR FUCKING MOUTH UP

    if all the members in this group are like YOU, this group is not gonna exist
    because noone will ask any questions.

    by the way "THANK SO MUCH" even YOU are not helpful

    "red floyd" <> wrote in message
    news:%E76b.10658$...
    > fighterman19 wrote:
    > > because in linked list each node contains only one digit like
    > >
    > > curr->nodevalue_1= 0
    > > curr->nodevalue_2=1
    > > sum->nodevalue = curr->nodevalue_1 + curr->nodevalue_2
    > >
    > > so when the number go up to >10 or >100 and the linked list need

    another
    > > node (I can add the node to the front ) but how can do the sum ?
    > >
    > > ex: 89+144 = 233
    > > how can I add it to the sum linked list?
    > > please help ( remember not a recursive problem)
    > >

    >
    > May I suggest that you take a stab at doing your own homework, and then
    > post your code, instead of asking us to do it for you?
    >
     
    fighterman19, Sep 5, 2003
    #5
  6. fighterman19

    fighterman19 Guest

    !!!!!!!!!!!...........???

    Sorry is it C++?
    I have no idea what it is. May be I have not learned this one yet. Is there
    anyway to do with linked list?

    "Victor Bazarov" <> wrote in message
    news:dp76b.368085$o%2.165927@sccrnsc02...
    > "fighterman19" <> wrote...
    > > because in linked list each node contains only one digit like

    >
    > Is that a requirement or just your understanding of linked lists?
    >
    > >
    > > curr->nodevalue_1= 0
    > > curr->nodevalue_2=1
    > > sum->nodevalue = curr->nodevalue_1 + curr->nodevalue_2
    > >
    > > so when the number go up to >10 or >100 and the linked list need

    another
    > > node (I can add the node to the front ) but how can do the sum ?
    > >
    > > ex: 89+144 = 233
    > > how can I add it to the sum linked list?
    > > please help ( remember not a recursive problem)

    >
    > What's the problem? Can't you implement addition with carry-over?
    >
    > 0 => carry.
    > label_1:
    > next_digit1 + next_digit2 + carry => result.
    > if result > 9 then
    > 1 => carry.
    > result - 10 => result.
    > else
    > 0 => carry.
    > end-if
    > store result
    > if have_more_digits go to label_1
    >
    > I don't see C++ language issue here, BTW.
    >
    > Victor
    >
    >
     
    fighterman19, Sep 5, 2003
    #6
  7. fighterman19

    fighterman19 Guest

    Yes, I know it doesn't not make sence because the best way to do fibonacci
    is to use recursive, right?
    But because I am learning about linked list so my task is to use linked list
    to do fibonacci.
    That's why I don't know how to handle the logic when the number has too many
    digit.

    by the way do you understand what i'am asking? I know it is hard to explain
    w/o the picture.

    "Kevin Goodsell" <> wrote in message
    news:Ht76b.494$...
    > fighterman19 wrote:
    >
    > > because in linked list each node contains only one digit like
    > >
    > > curr->nodevalue_1= 0
    > > curr->nodevalue_2=1
    > > sum->nodevalue = curr->nodevalue_1 + curr->nodevalue_2
    > >
    > > so when the number go up to >10 or >100 and the linked list need

    another
    > > node (I can add the node to the front ) but how can do the sum ?
    > >
    > > ex: 89+144 = 233
    > > how can I add it to the sum linked list?
    > > please help ( remember not a recursive problem)
    > >
    > >

    >
    > Why on earth would you use a linked list for this? Your question doesn't
    > make much sense.
    >
    > -Kevin
    > --
    > My email address is valid, but changes periodically.
    > To contact me please use the address from a recent posting.
    >
     
    fighterman19, Sep 5, 2003
    #7
  8. > Yes, I know it doesn't not make sence because the best way to do fibonacci
    > is to use recursive, right?


    If you by this meant something like
    int fib(int n) { return n < 2 ? 1 : fib(n-1) + fib(n-2); }
    then wrong.

    It turns out counting the numbers in the opposite order is
    (significantly) more efficient, since you will notice that this
    solution would calculate small fibonacci number very many times if n
    would be greater than very small.

    Best regards
    Michael Stockman
     
    Michael Stockman, Sep 6, 2003
    #8
  9. fighterman19 wrote:

    Please do not top-post. Re-read section 5 of the FAQ for posting guidelines.

    http://www.parashift.com/c -faq-lite/

    > Yes, I know it doesn't not make sence because the best way to do fibonacci
    > is to use recursive, right?


    Definitely not. Recursive fibonacci is terribly inefficient, because it
    does the same calculations over and over.

    > But because I am learning about linked list so my task is to use linked list
    > to do fibonacci.
    > That's why I don't know how to handle the logic when the number has too many
    > digit.


    I don't know what the number of digits has to do with it.

    >
    > by the way do you understand what i'am asking? I know it is hard to explain
    > w/o the picture.
    >


    No, I don't understand.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Sep 6, 2003
    #9
  10. "fighterman19" <> wrote in message
    news:...
    <snip faintly ridiculous flame>

    Wow, you can swear at someone in a newsgroup. We'd all be very impressed, if
    it wasn't quite so stunningly lame. And to think I was actually about to
    help you and had written working code for you (assuming I understood your
    question correctly, it wasn't hugely clear). For the record, you'd be
    surprised at how many people post here expecting someone to do their
    homework. The result of which is, of course, that we prefer to answer
    questions where people have actually had a stab at the code themselves; it
    demonstrates a modicum of effort. Of course, if we're in a good mood and
    feeling kind (which is most of the time, in my experience), we'll try and
    help out anyway, even if we're not given a huge amount to go on. At any
    rate, we will until you decide to start yelling at people, at which stage
    most of us generally decide it's not worth the bother and just killfile you.
    FWIW, if you apologise in the immediate future (i.e. preferably before
    everybody's read the original post), I think people would still try and
    answer your question. I suspect you're hovering dangerously close to the
    edge of quite a few people's killfiles, though. And the only person who
    loses out if everyone ignores you is you, bottom line. Perhaps a damage
    limitation exercise is in order?

    Just a suggestion,

    Stuart.

    P.S. The site you're looking for is http://www.parashift.com/c -faq-lite/
    Perhaps reading Section 5 would be useful?

    <snip>
     
    Stuart Golodetz, Sep 8, 2003
    #10
  11. fighterman19

    red floyd Guest

    Stuart Golodetz wrote:

    > [redacted]


    Stuart,

    Thank you for your voice of reason. The request sounded rather like
    homework, and perhaps I was a bit abrupt. I appreciate your words.

    red floyd
     
    red floyd, Sep 8, 2003
    #11
  12. fighterman19 wrote:
    >
    > !!!!!!!!!!!...........???
    >
    > Sorry is it C++?
    > I have no idea what it is. May be I have not learned this one yet.


    ????
    This is basic math and you have lerned it in your second or third
    year in ground school.

    To add 2 numbers:
    start at the right most digit.
    Add those
    If the result of that addition is greater then 10, then
    remember to add a carry 1 to the next left column


    254
    + 683


    4 + 3 -> 7
    8 + 5 -> 13 that's 3 for this column and 1 for the next column
    |
    +----------------------------------------+
    v
    6 + 2 + 1 -> 9

    So the result is

    254
    + 683
    ---
    7
    13
    8
    -----
    937

    As said: very basic.
    The addition of linked list to represent each number
    is just 'organizational suger'

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 8, 2003
    #12
    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:
    514
    emerth
    Jul 10, 2003
  2. fool
    Replies:
    14
    Views:
    546
    Barry Schwarz
    Jul 3, 2006
  3. joshd
    Replies:
    12
    Views:
    704
    John Carson
    Oct 2, 2006
  4. RJB
    Replies:
    23
    Views:
    1,176
    Raymond Hettinger
    May 25, 2011
  5. Wanderer
    Replies:
    7
    Views:
    535
    Wanderer
    Mar 8, 2012
Loading...

Share This Page