/*******************************************************
2.4
You have two numbers represented by a linked list, where each node contains a sin-
gle digit The digits are stored in reverse order, such that the 1’s digit is at the head of
the list Write a function that adds the two numbers and returns the sum as a linked
list
EXAMPLE
Input: (3 -> 1 -> 5) + (5 -> 9 -> 2)
Output: 8 -> 0 -> 8
513 + 295 = 808
Complexity: O(n)
Mod Time: 5.7, 2012
Copyright: Ben
*******************************************************/
#include<stdio.h>
#include<malloc.h>
#define OK 0
#define ERROR -1
#define MYDEBUG 1
typedef int ElemType;
typedef int Status;
struct Node;
typedef Node ListNode;
typedef ListNode * Position;
typedef Position LinkList;
struct Node
{
ElemType elem; //this field of header should be NULL.
Position next;
};
LinkList New_LinkList(void);
Status Print_LinkList(LinkList ll);
Status Append_LinkList(LinkList ll, ElemType val);
Status Insert_LinkList(Position posi, ElemType val);
Status Destroy_LinkList(LinkList ll);
ElemType DeleteNextNode(Position p);
int main(void)
{
int readInt;
int Len1, Len2, Len3;
int i, sum;
bool Len1IsLonger;
int *Carry, CarryLen;
//ElemType val;
Position TravNode1, TravNode2, TravNode3;
LinkList list1 = New_LinkList();
LinkList list2 = New_LinkList();
LinkList list3 = New_LinkList(); //result list
if((!list1) || (!list2) || (!list3))
{
return ERROR;
}
freopen("array.txt", "r", stdin);
scanf("%d", &Len1);
for(i = 0; i < Len1; i++)
{
scanf("%d", &readInt);
Append_LinkList(list1, readInt); //This is bad and needs improving since every time we traverse the whole list.
}
scanf("%d", &Len2);
for(i = 0; i < Len2; i++)
{
scanf("%d", &readInt);
Append_LinkList(list2, readInt);
}
Print_LinkList(list1);
Print_LinkList(list2);
Len1IsLonger = (Len1 > Len2);
//CarryLen is less one.
CarryLen = (Len1IsLonger) ? (Len2) : (Len1);
Carry = (int *)malloc(CarryLen * sizeof(int));
TravNode1 = list1->next;
TravNode2 = list2->next;
TravNode3 = list3;
//first step, i.e. i = 0.
sum = TravNode1->elem + TravNode2->elem;
*Carry = sum / 10;
Insert_LinkList(TravNode3, sum % 10); //empty header node.
TravNode1 = TravNode1->next;
TravNode2 = TravNode2->next;
TravNode3 = TravNode3->next;
for(i = 1; i < CarryLen; i++)
{
sum = TravNode1->elem + TravNode2->elem;
*(Carry + i) = sum / 10;
Insert_LinkList(TravNode3, sum % 10 + (*(Carry + i - 1)));
TravNode1 = TravNode1->next;
TravNode2 = TravNode2->next;
TravNode3 = TravNode3->next;
}
//last step with carry, i.e. i = CarryLen
if(Len1IsLonger)
{
TravNode2 = TravNode1; //use TravNode2.
}
Insert_LinkList(TravNode3, TravNode2->elem + (*(Carry + CarryLen - 1)));
TravNode2 = TravNode2->next;
TravNode3 = TravNode3->next;
#if 0
if(Len1IsLonger)
{
Insert_LinkList(TravNode3, TravNode1->elem + (*(Carry + CarryLen - 1)));
TravNode1 = TravNode1->next;
}
else
{
Insert_LinkList(TravNode3, TravNode2->elem + (*(Carry + CarryLen - 1)));
TravNode2 = TravNode2->next;
}
TravNode3 = TravNode3->next;
#endif
Len3 = (Len1IsLonger) ? (Len1) : (Len2);
if(Len1 == Len2)
{
Len3++;
}
//the rest is history, copy and paste.
for(i = CarryLen + 1; i < Len3; i++)
{
Insert_LinkList(TravNode3, TravNode2->elem);
TravNode2 = TravNode2->next;
TravNode3 = TravNode3->next;
}
fclose(stdin);
free(Carry);
#if MYDEBUG
printf("CarryLen = %d\n", CarryLen);
printf("Len3 = %d\n", Len3);
#endif
Print_LinkList(list3);
Destroy_LinkList(list1);
Destroy_LinkList(list2);
Destroy_LinkList(list3);
return OK;
}
LinkList New_LinkList(void)
{
LinkList ll = (LinkList)malloc(sizeof(ListNode));
ll->elem = 0;
ll->next = NULL;
return ll;
}
Status Print_LinkList(LinkList ll)
{
Position p = ll;
if(!ll) //trying to delete a linklist that does not exist
{
printf("trying to print a linklist that does not exist\n");
return ERROR;
}
printf("The elements in link list: \n");
while(p->next != NULL)
{
p = p->next;
printf("%d\t", p->elem);
}
printf("\n");
return OK;
}
Status Append_LinkList(LinkList ll, ElemType val)
{
Position posi;
if(!ll) //trying to delete a linklist that does not exist
{
printf("trying to insert to a linklist that does not exist\n");
return ERROR;
}
posi = ll;
while(posi->next)
{
posi = posi->next;
}
Insert_LinkList(posi, val);
return OK;
}
//insert element to end of list, where posi is the tail node.
Status Insert_LinkList(Position posi, ElemType val)
{
posi->next = (Position)malloc(sizeof(ListNode));
posi->next->next = NULL;
posi->next->elem = val;
return OK;
}
Status Destroy_LinkList(LinkList ll)
{
Position posi, temp;
if(!ll) //trying to delete a linklist that does not exist
{
printf("trying to delete a linklist that does not exist\n");
return ERROR;
}
posi = ll;
ll->next = NULL;
while(posi->next)
{
temp = posi->next;
free(posi);
posi = temp;
}
return OK;
}
ElemType DeleteNextNode(Position p)
{
ElemType val;
Position temp = p->next;
val = temp->elem;
p->next = p->next->next;
free(temp);
return val;
}
2.4
You have two numbers represented by a linked list, where each node contains a sin-
gle digit The digits are stored in reverse order, such that the 1’s digit is at the head of
the list Write a function that adds the two numbers and returns the sum as a linked
list
EXAMPLE
Input: (3 -> 1 -> 5) + (5 -> 9 -> 2)
Output: 8 -> 0 -> 8
513 + 295 = 808
Complexity: O(n)
Mod Time: 5.7, 2012
Copyright: Ben
*******************************************************/
#include<stdio.h>
#include<malloc.h>
#define OK 0
#define ERROR -1
#define MYDEBUG 1
typedef int ElemType;
typedef int Status;
struct Node;
typedef Node ListNode;
typedef ListNode * Position;
typedef Position LinkList;
struct Node
{
ElemType elem; //this field of header should be NULL.
Position next;
};
LinkList New_LinkList(void);
Status Print_LinkList(LinkList ll);
Status Append_LinkList(LinkList ll, ElemType val);
Status Insert_LinkList(Position posi, ElemType val);
Status Destroy_LinkList(LinkList ll);
ElemType DeleteNextNode(Position p);
int main(void)
{
int readInt;
int Len1, Len2, Len3;
int i, sum;
bool Len1IsLonger;
int *Carry, CarryLen;
//ElemType val;
Position TravNode1, TravNode2, TravNode3;
LinkList list1 = New_LinkList();
LinkList list2 = New_LinkList();
LinkList list3 = New_LinkList(); //result list
if((!list1) || (!list2) || (!list3))
{
return ERROR;
}
freopen("array.txt", "r", stdin);
scanf("%d", &Len1);
for(i = 0; i < Len1; i++)
{
scanf("%d", &readInt);
Append_LinkList(list1, readInt); //This is bad and needs improving since every time we traverse the whole list.
}
scanf("%d", &Len2);
for(i = 0; i < Len2; i++)
{
scanf("%d", &readInt);
Append_LinkList(list2, readInt);
}
Print_LinkList(list1);
Print_LinkList(list2);
Len1IsLonger = (Len1 > Len2);
//CarryLen is less one.
CarryLen = (Len1IsLonger) ? (Len2) : (Len1);
Carry = (int *)malloc(CarryLen * sizeof(int));
TravNode1 = list1->next;
TravNode2 = list2->next;
TravNode3 = list3;
//first step, i.e. i = 0.
sum = TravNode1->elem + TravNode2->elem;
*Carry = sum / 10;
Insert_LinkList(TravNode3, sum % 10); //empty header node.
TravNode1 = TravNode1->next;
TravNode2 = TravNode2->next;
TravNode3 = TravNode3->next;
for(i = 1; i < CarryLen; i++)
{
sum = TravNode1->elem + TravNode2->elem;
*(Carry + i) = sum / 10;
Insert_LinkList(TravNode3, sum % 10 + (*(Carry + i - 1)));
TravNode1 = TravNode1->next;
TravNode2 = TravNode2->next;
TravNode3 = TravNode3->next;
}
//last step with carry, i.e. i = CarryLen
if(Len1IsLonger)
{
TravNode2 = TravNode1; //use TravNode2.
}
Insert_LinkList(TravNode3, TravNode2->elem + (*(Carry + CarryLen - 1)));
TravNode2 = TravNode2->next;
TravNode3 = TravNode3->next;
#if 0
if(Len1IsLonger)
{
Insert_LinkList(TravNode3, TravNode1->elem + (*(Carry + CarryLen - 1)));
TravNode1 = TravNode1->next;
}
else
{
Insert_LinkList(TravNode3, TravNode2->elem + (*(Carry + CarryLen - 1)));
TravNode2 = TravNode2->next;
}
TravNode3 = TravNode3->next;
#endif
Len3 = (Len1IsLonger) ? (Len1) : (Len2);
if(Len1 == Len2)
{
Len3++;
}
//the rest is history, copy and paste.
for(i = CarryLen + 1; i < Len3; i++)
{
Insert_LinkList(TravNode3, TravNode2->elem);
TravNode2 = TravNode2->next;
TravNode3 = TravNode3->next;
}
fclose(stdin);
free(Carry);
#if MYDEBUG
printf("CarryLen = %d\n", CarryLen);
printf("Len3 = %d\n", Len3);
#endif
Print_LinkList(list3);
Destroy_LinkList(list1);
Destroy_LinkList(list2);
Destroy_LinkList(list3);
return OK;
}
LinkList New_LinkList(void)
{
LinkList ll = (LinkList)malloc(sizeof(ListNode));
ll->elem = 0;
ll->next = NULL;
return ll;
}
Status Print_LinkList(LinkList ll)
{
Position p = ll;
if(!ll) //trying to delete a linklist that does not exist
{
printf("trying to print a linklist that does not exist\n");
return ERROR;
}
printf("The elements in link list: \n");
while(p->next != NULL)
{
p = p->next;
printf("%d\t", p->elem);
}
printf("\n");
return OK;
}
Status Append_LinkList(LinkList ll, ElemType val)
{
Position posi;
if(!ll) //trying to delete a linklist that does not exist
{
printf("trying to insert to a linklist that does not exist\n");
return ERROR;
}
posi = ll;
while(posi->next)
{
posi = posi->next;
}
Insert_LinkList(posi, val);
return OK;
}
//insert element to end of list, where posi is the tail node.
Status Insert_LinkList(Position posi, ElemType val)
{
posi->next = (Position)malloc(sizeof(ListNode));
posi->next->next = NULL;
posi->next->elem = val;
return OK;
}
Status Destroy_LinkList(LinkList ll)
{
Position posi, temp;
if(!ll) //trying to delete a linklist that does not exist
{
printf("trying to delete a linklist that does not exist\n");
return ERROR;
}
posi = ll;
ll->next = NULL;
while(posi->next)
{
temp = posi->next;
free(posi);
posi = temp;
}
return OK;
}
ElemType DeleteNextNode(Position p)
{
ElemType val;
Position temp = p->next;
val = temp->elem;
p->next = p->next->next;
free(temp);
return val;
}