473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

linked list help?

I have 2 singly linked list that contain 1 digit in each node like
this
A [ 1 ]--->[ 2 ]--->[ 3 ]---> NULL

B [ 8 ]---> [ 9 ]--->NULL

I want to add A+B ( 123 + 89) and the sum store in another linked list

sum [ 9 ]---> [ 1 ]---->[ 4 ]---->NULL

all i do are
int carry=0;
do{
int sum =0;

sum= A->value + B->value + carry;
if (sum > 9)
{
carry= sum/10;
sum->value=sum%10;

}
else
{
carry=0;
sum->value=sum%10;
}
// how to move form the first node to second node?

} while (A->next != NULL || B->next !=NULL);

can someone to me how to shift in this one? Thank
Jul 19 '05 #1
2 1570

"fighterman19" <vi******@hushmail.com> wrote in message news:uf********************************@4ax.com...

// how to move form the first node to second node?
To answer the question you asked,
A = A->next;
B = B->next;
} while (A->next != NULL || B->next !=NULL);


You have bigger logic problems. Your code doesn't handle the
example you gave.

1. Since your lists are different sizes, B->next will be NULL before A->next
is NULL. Your code since it does OR, it will go through the loop with
B set to NULL the last time, invoking undefined behavior trying to do NULL->value.

2. Your code doesn't do what you said it will. Note what it does the first time
through the loop. It add's 1 to 8, next time 2 to 9, ...

Jul 19 '05 #2
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...

"fighterman19" <vi******@hushmail.com> wrote in message
news:uf********************************@4ax.com...
I have 2 singly linked list that contain 1 digit in each node like
this
A [ 1 ]--->[ 2 ]--->[ 3 ]---> NULL

B [ 8 ]---> [ 9 ]--->NULL

I want to add A+B ( 123 + 89) and the sum store in another linked list

sum [ 9 ]---> [ 1 ]---->[ 4 ]---->NULL


You lost me. What is 9, 1, and 4?


It appears the digits are reversed, e.g.
321
+98
----
419

Jul 19 '05 #3

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

Similar topics

7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
1
by: Little | last post by:
Hello everyone. I am trying to do the following program and am unable to get the beginning portion to work correctly. The scanner works when I print the statements without the double linked list...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
2
by: phiefer3 | last post by:
Ok, first of all I'm not sure if this is the correct forum for this question or not. But hopefully someone can help me or at least point me in the direction of the forum this belongs. First of...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.