Example #1
0
void sumLinks(Node* list1, Node* list2) {
	if (list1 == NULL && list2 == NULL) return;

	int carry = 0;
	int val = 0;
	Node* l1 = list1;
	Node* l2 = list2;

	/// determine if a list is longer and if so, pad that list
	int l1Len = lenList(l1);
	int l2Len = lenList(l2);

	if (l1Len > l2Len) {
		padList(l2, l1Len - l2Len);
	}

	if (l2Len > l1Len) {
		padList(l1, l2Len - l1Len);
	}


	while (l1 || l2) {
		l1->data += carry;

		val = l1->data + l2->data;

		if (val > 9) {
			carry = 1;
			val = val - 10;
		}
		else {
			carry = 0;
		}

		l1->data = val;

		l1 = l1->next;
		l2 = l2->next;
	}

	/// if the final digit had a carry we need to add one to the end
	if (carry) {
		/// reset the head and find the last element
		l1 = list1;
		while (l1->next) {
			l1 = l1->next;
		}

		l1->next = new Node();
		l1->next->data = 1;
		l1->next->next = nullptr;
	}
}
Example #2
0
void padLists(Node* head1, Node* head2) {
	int len_list1, len_list2, padlen = 0;

	if (head1 == NULL || head2 == NULL)
		return;

	len_list1 = get_list_length(head1);
	len_list2 = get_list_length(head2);

	if (len_list1 == len_list2)
		return;

	if (len_list1 < len_list2) {
		padlen = len_list2 - len_list1;
		padList(head1, padlen);
	} else {
		padlen = len_list1 - len_list2;
		padList(head2, padlen);
	}
}
Example #3
0
int main(int argc, char *argv[]) {
	/*
	printf("Argument count: %d\n", argc);
	for (int i = 0; i < argc; i++) {
		printf("Argument vector values:%s at %p memory\n", argv[i], argv[i]);
		for (char *j=argv[i]; *j!='\0'; j++) {
			printf("Another way to print argument vector values: "
                               "%c at %p memory\n", *j, j);
		}
	}
	*/

	//int num1[MAX_LEN] = {7,2,0,3,7};
	int num1[MAX_LEN-3] = {3,7};
	struct node *head1 = NULL; /* beginning */
	head1 = newNode(num1[0]);
	//head1 = addNodetoFront(head1,num1[0]);
	//head1 = addNodetoFront(head1,num1[2]);
	//head1 = addNodetoFront(head1,num1[1]);
	//head1 = addNodetoFront(head1,num1[0]);

	printf("Num1 : %d%d%d%d%d\n",num1[0],num1[1],num1[2],num1[3],num1[4]);
	iterateNodes(head1);

	//int num2[MAX_LEN] = {2,8,9,7,1};
	//int num2[MAX_LEN-1] = {8,9,7,1};
	int num2[MAX_LEN-2] = {9,7,1};
	struct node *head2 = NULL; /* beginning */
	head2 = newNode(num2[2]);
	head2 = addNodetoFront(head2,num2[1]);
	head2 = addNodetoFront(head2,num2[0]);
	//head2 = addNodetoFront(head2,num2[0]);
	//head2 = addNodetoFront(head2,num2[0]);

	printf("Num1 : %d%d%d%d%d\n",num2[0],num2[1],num2[2],num2[3],num2[4]);
	iterateNodes(head2);

	int lenNum1 = 0, lenNum2 = 0;
	lenNum1 = findLen(head1);
	printf("Lenght of num1: %d\n",lenNum1);
	lenNum2 = findLen(head2);
	printf("Lenght of num2: %d\n",lenNum2);

	if (lenNum1 < lenNum2) {
		head1 = padList(head1, lenNum1, lenNum2);
		iterateNodes(head1);
	}
	else {
		head2 = padList(head2, lenNum2, lenNum1);
		iterateNodes(head2);
	}

	struct node *sum = NULL; /* beginning */
	sum = addLists(head1,head2);

	printf("\nFreeing Linked List sum:\n");
	struct node *temp;
	while (sum!=NULL) {
		printf("Value: %d, Address: %p, Next Address: %p\n",sum->data,sum,sum->next);
		temp = sum;
		sum=sum->next;
		free(temp);
	}

	printf("\nFreeing Linked List num 1:\n");
	while (head1!=NULL) {
		printf("Value: %d, Address: %p, Next Address: %p\n",head1->data,head1,head1->next);
		temp = head1;
		head1=head1->next;
		free(temp);
	}

	printf("\nFreeing Linked List num 2:\n");
	while (head2!=NULL) {
		printf("Value: %d, Address: %p, Next Address: %p\n",head2->data,head2,head2->next);
		temp = head2;
		head2=head2->next;
		free(temp);
	}

	//head = NULL;     /* Mark list as empty*/
	return 0;
}