Thread: multiply link list

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    7

    Unhappy multiply link list

    can anybody tell me how to multiply two linked lists??

    i want to multiply large nos., consisting of hundreds of digit.
    i have stored the nos in link list form as one digit per node.
    now how to perform the multiplication???

    or

    is there any other way to multiply large nos???

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well do you know how to multiply on paper?

    Eg.

    12
    34

    Multiply these two digits first, they'll be the ones at the end of your two linked lists.
    The result you store at the end of a third linked list.

    Rinse and repeat, according to the rules of multiplication.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    ya i understand but its highly complicated to implement!!!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How so?

    You have a couple of doubly linked lists, and you work your way all to the end of one, for each step of the other.

    Try it with fixed length arrays first if you're unsure of the algorithm.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you're just as willing to use C++ instead of C then I've already written an efficient variable-sized large integer class. "varbigint" on my useful classes page of the below site.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by mradul.exe View Post
    ya i understand but its highly complicated to implement!!!
    Write down how to do it. Given a list of digits An through A0, and Bm through B0, how do I get the product of A and B?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 10:28 PM
  2. reading data from a file - link list
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2006, 09:11 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread