Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following code results in an OutOfMemory Error from both methods. Please help fix this error. private SpecializedList addLists(SpecializedList list1, SpecializedList list2) { SpecializedList result

The following code results in an OutOfMemory Error from both methods. Please help fix this error.

private SpecializedList addLists(SpecializedList list1, SpecializedList list2) { SpecializedList result = new SpecializedList();

int carry = 0;

list1.resetForward();

list2.resetForward();

while (list1.forward != null || list2.forward != null) {

int digit1 = list1.forward != null ? list1.getNextItem() : 0;

int digit2 = list2.forward != null ? list2.getNextItem() : 0;

int sum = carry + digit1 + digit2;

int digitSum = sum % 10;

carry = sum / 10;

result.insertEnd((byte) digitSum);

} if (carry > 0) { result.insertEnd((byte) carry); } return result; // Addition logic here } private SpecializedList subtractLists(SpecializedList list1, SpecializedList list2) { SpecializedList result = new SpecializedList();

int carry = 0;

list1.resetForward();

list2.resetForward();

while (list1.forward != null || list2.forward != null) {

int digit1 = list1.forward != null ? list1.getNextItem() : 0;

int digit2 = list2.forward != null ? list2.getNextItem() : 0;

int difference = carry + digit1 - digit2;

int digitDifference = (difference + 10) % 10;

carry = (difference +10) / 10 - 1;

result.insertEnd((byte) digitDifference);

} if (carry > 0) { result.insertEnd((byte) carry); } return result; // Subtraction logic here } private int compareLists(SpecializedList list1, SpecializedList list2) { list1.resetForward();

list2.resetForward();

while (list1.forward != null || list2.forward != null) {

int digit1 = list1.forward != null ? list1.getNextItem() : 0;

int digit2 = list2.forward != null ? list2.getNextItem() : 0;

if (digit1 > digit2) { return 1; } else if (digit2 > digit1) { return -1; }

}

return 0; // Comparison logic here }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions