428,586 Members | 623 Online
Need help? Post your question and get tips & solutions from a community of 428,586 IT Pros & Developers. It's quick & easy.

# Subtracting large numbers usng linked list

 P: n/a Hi, I have an assignment that requires me to subtract two very large unsigned integers using linked list.AFAIK I have to manipulate data as character. But I dont know how to get input into the linked list since the input will all be in one line. eg. 1234567890. Also I have to use one integer per node. Any help/suggestion will be greatly appreciated. Thanks in advance Jul 22 '05 #1
8 Replies

 P: n/a simpleman wrote: Hi, I have an assignment that requires me to subtract two very large unsigned integers using linked list.AFAIK I have to manipulate data as character. But I dont know how to get input into the linked list since the input will all be in one line. eg. 1234567890. Also I have to use one integer per node. Any help/suggestion will be greatly appreciated. Thanks in advance Get the input as string. And then tokenize the string into parts such that you can fit each part into a node in the linked list. -- Karthik. http://akktech.blogspot.com . ' Remove _nospamplz from my email to mail me. ' Jul 22 '05 #2

 P: n/a "simpleman" wrote in message news:32**************************@posting.google.c om... Hi, I have an assignment that requires me to subtract two very large unsigned integers using linked list.AFAIK I have to manipulate data as character. But I dont know how to get input into the linked list since the input will all be in one line. eg. 1234567890. Also I have to use one integer per node. Any help/suggestion will be greatly appreciated. Thanks in advance Well that seems the easier part of your assignment. You could read the input one character at a time. E.g. char ch; while (cin.get(ch)) { if (ch == '\n') // stop at end of line break; // add char to list here } You could read a line at a time and then split the line up into individual characters. string line; getline(cin, line); for (size_t i = 0; i < line.size(); ++i) { char ch = line[i]; // add char to list here } john Jul 22 '05 #3

 P: n/a "simpleman" writes: I have an assignment that requires me to subtract two very large unsigned integers using linked list.AFAIK I have to manipulate data as character. But I dont know how to get input into the linked list since the input will all be in one line. eg. 1234567890. Also I have to use one integer per node. Any help/suggestion will be greatly appreciated. A simple linked list gives you easy access to only one end of the list. So you want to put the data in backwards, so the '0' will be put in last. Then you can conveniently work in a right to left fashion, as you learned in elementary school. Convert the digits to positive BCD digits, 0000 to 1001 as you insert them. Do any necessary sign finagling as a separate step. You might want to use nine or tens complement arithmetic. But my guess is that the instructor will let you get by if you only handle positive integers. Note that you can do anything that needs to be done, +, -, *, /, with a proper subtractor [sp?]. Even though the data are one line, it doesn't mean that you can't read and process a single character. That is: char ch; cin >> ch; works OK. Jul 22 '05 #4

 P: n/a I got the input into the string with one integer per node. Now the problem is subtracting two linked lists from one another as it involves carry over in case the bottom number is larger than corresponding integer. Also since they are in the linked list, I have to subtract one integer from another. Any help will be greatly appreciated Thans in Advance Jul 22 '05 #5

 P: n/a "simpleman" schrieb im Newsbeitrag news:32**************************@posting.google.c om...I got the input into the string with one integer per node. Now the problem is subtracting two linked lists from one another as it involves carry over in case the bottom number is larger than corresponding integer. Also since they are in the linked list, I have to subtract one integer from another. Any help will be greatly appreciated Thans in Advance Do you know how to do a difference manually (on paper)? You can do the same with the computer. As you are using your own representation you can use an additional flag for the sign. 2 3 4 5 6 7 8 9 (A) -1 2 3 4 5 6 7 8 (B) --------------- 1 1 1 1 1 1 1 1 (C) Remark: if A