Help with multiplying linked lists

Im writing a program for my CS215 Class using vigesimal (base 20) numbers. one of the functions is multiplying 2 user input linked lists. after about 5 hours of trying to figure out how to get these lists to multiply i think i found a solution. The problem is its stored in a double. My question is, is there any way to read this value into a linked list. Also, any recommendations on making this method better would be greatly appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
void LList::Multiply(){
        LList d;
        LList e;
        d.ReadBackwards();
        d.ConvertAscii();
        listnode* temp1 = head;
        listnode* temp2 = d.head;


        int product;
        int carry = 0;
	double sum = 0;
	double extra;
	float n = 0.0;
	float i = 0.0;
	while (temp2 != NULL){
		product = temp1 ->data;
		product = product * temp2 -> data;
	        product = product + carry;
		if (product >19){
			carry = product/20;
			product = product % 20; 
		}
		else{
		carry = 0;
		product = product % 20;
		}
		n = n + 1.0;
		temp1 = temp1 -> next;
		extra = product *pow(20,i);

		sum = sum+extra;

			if (temp1 == NULL){
				n = 0;
				i = i + 1.0;
				temp1 = head;
				temp2 = temp2-> next;


			}
		}

	cout << sum << endl; // Sum is where the value is stored
	
	

        Clean();
	Duplicate(e);
        Menu();
	
Why don't you just create the product list in the exact same way you do a multiplication of numbers? It's easier to explain in base 10

145x53

put them in reverse lists

5->4->1 and 3->5

and product=NULL

Start from the second list and multiply it with the first using this procedure:

3*5=15, product=5->1
3*4=12, take the 2, add it to 1, and move 1 to the next, so product=5->(1+2)->1=5->3->1
3*1=3, add it to the last element so product becomes 5->3->4

5*5=25, take the 5, add it to the second element of the list, and the two to the third element so product=5->(3+5)->(4+2)=5->8->6
5*4=20, take the 0, add it to the third element in the list, and 2 to the next element, so product=5->8->6->2
5*1=5, add it to the 4th element in the list, so the product becomes 5->8->6->7, which now you need to reverse
also, if you don't use pow and instead just multiply 20 enough times, you can store the values in long ints
First and foremost i want to thank you for commenting, Ive done more work since i posted this and Ive got it multiplying by single digits just fine. when multiplying by multiple digits i cant seem to comprehend how to incorporate the 0 that gets added in.

for example:
22 * 22 = 44+440 <- this 0 ... = 484

Ive actually got it broken into 2 functions now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void LList::Multiply(){
        LList d;
        LList e;
	LList f;
        d.ReadBackwards();
        d.ConvertAscii();
        listnode* temp1 = head;
        listnode* temp2 = d.head;


        int product;
        int carry = 0;
	double sum = 0;
	double extra;
	float n = 0.0;
	float i = 0.0;
	while (temp2 != NULL){
		product = temp1 ->data;
		product = product * temp2 -> data;
	        product = product + carry;
		if (product >19){
			carry = product/20;
			product = product % 20; 
		}
		else{
		carry = 0;
		product = product%20;
		}
		n = n + 1.0;
		temp1 = temp1 -> next;
		e.inserttail(product);

		if (temp1 == NULL){
			addM(f,e);

			n = 0;
			i = i + 1.0;
			temp1 = head;
			temp2 = temp2-> next;
		}
		}
	Clean();
	Duplicate(f);

	Menu();


and

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void LList::addM (LList &a, LList &b){
	LList copy;
	listnode* temp1 = a.head;
	listnode* temp2 = b.head;

	int sum = 0;
	int carry = 0;
  	while(temp1 || temp2){
   		if(temp1)
      			sum = sum + temp1->data;
		else;
    		if(temp2)
      			sum = sum + temp2->data;
		else;
 		sum = sum + carry;
    		carry = 0;
    		
    		while (sum > 19 ){
      			sum = sum - 20;
			carry++;
    		}
		
		copy.inserttail(sum);
		sum = 0;
    		if (temp1)
    			temp1 = temp1->next;
		if (temp2)
    		temp2 = temp2->next;
	}
	a.Clean();
	a.Duplicate(copy);
	b.Clean();
}
Last edited on
To incorporate the 0, you can do one of two things:

1. Insert a 0 in the list using InsertHead. For example:
123
x 23

= 3*123 + (InsertHead(0) here) 2*1230

2. Multiply a variable by 20 each time you go through the loop:
In base 10 it would look like:
123
x 23

= variable * 3 * 123
+ (variable*10) * 2 * 123

this way what you're doing is multiplying 3 by 123 then 20 by 123.

Last edited on
Topic archived. No new replies allowed.