473,796 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Subtracting large numbers usng linked list

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 5800
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

"simpleman" <kh*********@gm ail.com> wrote in message
news:32******** *************** ***@posting.goo gle.com...
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
"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
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

"simpleman" <kh*********@gm ail.com> schrieb im Newsbeitrag
news:32******** *************** ***@posting.goo gle.com...
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<B:
C= - (B-A)
else:
C= A-B

Regards
Michael
Jul 22 '05 #6

"simpleman" <kh*********@gm ail.com> wrote in message
news:32******** *************** ***@posting.goo gle.com...
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


You only get help with homework when you show that you have made some effort
yourself. Post your attempt to subtract one number from another. Then people
will help you with the code.

Also try to explain exactly what you are stuck on, it sounds like you are
stuck on what algorithm to use. So post your ideas on the algorithm, even if
it only half baked ideas like 'I'm going to use a loop'. The way you do this
problem, is very similar to the way you subtract two numbers one a piece of
paper, so have a think about that.

The bottom line is no-one is going to give you the answer. You have to show
that you are making an effort.

John
Jul 22 '05 #7
Here is the rough code I have done so far;
It works untill it prits out two linked list but after that don't have any
idea whats happening...
#include<iostre am.h>
struct link{
char ch;
link *next;};
int main(){
link *top1=0,*top2=0 ,*top3=0;
link *lptr;
char ch;
int one,two,temp;
cout<<"Input first number:";
while(cin.get(c h)){
if(ch == '\n')
break;
lptr =new link;
lptr->ch=ch;
lptr->next=top1;
top1=lptr;}
lptr=top1;
while(top1){
cout<<top1->ch;
top1=top1->next;}
cout<<"Input second number:";
while(cin.get(c h)){
if(ch=='\n')
break;
lptr =new link;
lptr->ch=ch;
lptr->next=top2;
top2=lptr;}
lptr=top2;
while(top2){
cout<<top2->ch;
top2=top2->next;}
while(top1!=NUL L && top2!=NULL){
one=top1->ch - '0';
two=top2->ch - '0';
if(one<two){
temp=-(two - one);
lptr=new link;
lptr->ch=temp;
lptr->next=top3;
top3=lptr;
top1=top1->next;
top2=top2->next;
cout<<top3->ch;}
else
one=top1->ch - '0';
two=top2->ch - '0';
temp=(one - two);
lptr=new link;
lptr->ch=temp;
lptr->next=top3;
top3=lptr;
top1=top1->next;
top2=top2->next;
cout<<top3->ch;}
return 0;
}
Jul 22 '05 #8

"simpleman" <kh*********@gm ail.com> wrote in message
news:32******** *************** ***@posting.goo gle.com...
Here is the rough code I have done so far;
It works untill it prits out two linked list but after that don't have any
idea whats happening...
#include<iostre am.h>
struct link{
char ch;
link *next;};
int main(){
link *top1=0,*top2=0 ,*top3=0;
link *lptr;
char ch;
int one,two,temp;
cout<<"Input first number:";
while(cin.get(c h)){
if(ch == '\n')
break;
lptr =new link;
lptr->ch=ch;
lptr->next=top1;
top1=lptr;}
lptr=top1;
while(top1){
cout<<top1->ch;
top1=top1->next;}
cout<<"Input second number:";
while(cin.get(c h)){
if(ch=='\n')
break;
lptr =new link;
lptr->ch=ch;
lptr->next=top2;
top2=lptr;}
lptr=top2;
while(top2){
cout<<top2->ch;
top2=top2->next;}
while(top1!=NUL L && top2!=NULL){
one=top1->ch - '0';
two=top2->ch - '0';
if(one<two){
temp=-(two - one);
lptr=new link;
lptr->ch=temp;
lptr->next=top3;
top3=lptr;
top1=top1->next;
top2=top2->next;
cout<<top3->ch;}
else
one=top1->ch - '0';
two=top2->ch - '0';
temp=(one - two);
lptr=new link;
lptr->ch=temp;
lptr->next=top3;
top3=lptr;
top1=top1->next;
top2=top2->next;
cout<<top3->ch;}
return 0;
}


Did you know that C++ has a built in linked list? Using it would certainly
tidy up your code.

Anyway there are at least four different things you aren't dealing with

1) Lists which are different length. This is actually quite easy, when you
subtract 123 from 34567 say, you just have to think of the smaller number as
having extra zeros, i.e. subtract 00123 from 34567. So when one list is
smaller than the other don't quit your loop early, carry on until both lists
are NULL but take zeros from the smaller list. Something like this

while(top1!=NUL L || top2!=NULL){
{
if (top1)
one=top1->ch - '0';
else
one = 0; // end of top1 list, pad out with zeros
if (top2)
two=top2->ch - '0';
else
two = 0; // end of top2 list, pad out with zeros
...
}
2) Borrowing when the top number is less than the bottom number. This is
also quite easy when you know how. Remember how you do subtract one paper
(you do know how to do that right? In this age of computers and calculators
its hard to be sure). If the top number is smaller than the bottom number
you have to borrow from the next column. This means that you have to
remember when you subtract two numbers whether you borrowed last time
around. So you need an extra variable to remember this. Something like this

bool borrow = false; // no borrow to start with
while (more digits)
{
int top_digit = digit off top list;
int bottom_digit = digit off bottom list;
if (borrow) // did we borrow last time?
--top_digit; // if we did then subtract one from top digit
int result_digit = top_digit - bottom_digit;
if (result_digit < 0) // is result less than zero?
{
result_digit += 10; // we need to borrow
borrow = true; // remember we borrowed for next time
}
else
{
borrow = false; // remember we didn't borrow for next time
}
...
}

Now a question, what do you need it means if at the end of the whole loop
borrow is true. What does that mean in terms of the two numbers?

3) Your final number ends up in the opposite direction from your first two
numbers. The first two lists are smallest number first. The final list is
largest number first. You are go to have to write some code to reverse a
list.

When you finish you will need to write some code to reverse the final list.

4) The code is a god-awful mess. Try to write a few functions. Things like

link* input_number();

void print_number(li nk* num);

link* subtract_number s(link* top, link* bottom);

link* reverse_list(li st* l);

Doing this will help you complete the assignment because it will force you
to organise your thoughts a little better. Also choosing better variable
names will help you for exactly the same reason.

john
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1996
by: Donald Firesmith | last post by:
We are converting the OPEN Process Framework Repository (www.donald-firesmith.com) of over 1,100 free open source reusable process components for building development methods for software-intensive systems from html to xml. The current html files are organized into a hierarchy of dozens of files based on the natural metamodel of process components on which the framework is based. I have the following questions: 1) What is the...
55
5193
by: Jonas Smithson | last post by:
I've seen a few attractive multi-column sites whose geometry is based on pure CSS-P, but they're what you might call "code afficionado" sites, where the subject matter of the site is "coding practices." (One example of this is alistapart.com.) However, the project/development realities for small boutique sites are completely different from those of large commercial or institutional sites -- and I was curious to see what coding approaches...
7
3540
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a segment fault. I Know it is in this line of code. How do i make the last pointer in the indirect sector that has another level of indirect pointer, and be defined recursively to support infinite large files? -code-
18
5643
by: Zero | last post by:
Hi, I am calculating an integer to the pwer of a large integer, e.g. 2^5000. It turns out that the result I always get is zero. I am sure that the result is too large to store in such type as u_int64_t and long int etc. My power function is as follows: for(i=0; i<5000; i++) result *= 2;
6
1615
by: placid | last post by:
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;
21
8154
by: Imran | last post by:
I have a vector of integers, such as and I want to find out the number which occurs most frequently.what is the quick method. My array size is huge. what I am doing is 1. find out the maximum value N 2. loop through 1...N 3. count # times each occurred
2
2793
JAMBAI
by: JAMBAI | last post by:
Hi, How to delete large numbers (100,000 - 1,000,000) records from linked table. I am trying to delete from MS Access Forms. Thanks Jambai
4
4272
by: Voltem | last post by:
Hey everyone I just registered. Seems like a nice community and all the other sentimental BS :). So my problem in a nutshell is I wanna write a programme that calculates Large factorials (lets say 1000!) using linked lists(to store the numbers like 4 digits in each node). My problem is putting the result of such large factorials in Linked Lists so if you know how to give me an Idea about it pretty pretty thank you with a cherry on top. (If you...
25
20576
by: tekctrl | last post by:
Anyone: I have a simple MSAccess DB which was created from an old ASCII flatfile. It works fine except for something that just started happening. I'll enter info in a record, save the record, and try to move to another record and get an Access error "Record is too large". The record is only half filled, with many empty fields. If I remove the added data or delete some older data, then it saves ok and works fine again. Whenever I'm...
0
9679
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.