There are two linked List, each node of the list carrying a digit. Write program to add these two linked list and create a third list with the result value
e.g:
1st List : 1->2->3->4->NULL
2nd List : 3->4->5->6->NULL
Resultant List : 4->6->9->0->NULL
Code:struct linkedList { int data; struct linkedList* next; }; typedef struct linkedList* List; List addList(List L1, List L2) { int carry = 0,sum=0; List L3 = NULL, tmp, bkp, rest = NULL; if(!L1 && !L2) return NULL; if(!L1) return L2; if(!L2) return L1; L1 = reverseList(L1); L2 = reverseList(L2); while(L1 && L2) { sum = L1->data + L2->data + carry; carry = sum/10; sum = sum%10; tmp = (List) malloc(sizeof(struct linkedList)); tmp->data = sum; tmp->next = NULL; if(!L3) bkp = L3 = tmp; else { L3->next = tmp; L3 = L3->next; } L1 = L1->next; L2 = L2->next; } if (L1) rest = L1; else if (L2) rest = L2; while(carry) { if(rest) { sum = carry + rest->data; carry = sum/10; L3->next = (List) malloc(sizeof(struct linkedList)); L3->next->data = sum%10; L3 = L3->next; rest = rest->next; } else { L3->next = (List) malloc(sizeof(struct linkedList)); L3->next->data = carry; L3->next->next = NULL; carry = 0; } } bkp = reverseList(bkp); return bkp; }
Last edited by surfer; 17th September 2010 at 22:40.
#include<iostream.h>
#include<conio.h>
struct list1
{
int num;
list1 *next;
}*start1,*rear1,*newptr1,*ptr1;
struct list2
{
int num;
list2 *next;
}*start2,*rear2,*newptr2,*ptr2;
struct list3
{
int num;
list3 *next;
}*start3,*rear3,*ptr3;
list1 *createNewList1(int);
list2 *createNewList2(int);
list3 *createNewList3(int);
void insertNewList1(list1 *);
void insertNewList2(list2 *);
void insertNewList3(list3 *);
void main()
{
int choice,number=0;
char ans;
do
{
clrscr();
cout<<"Welcome to linked list program.\n";
cout<<"1. Enter numbers in list 1.\n";
cout<<"2. Enter numbers in list 2.\n";
cout<<"3. Add numbers from list 1 and list 2 sequentially into list 3.\n";
cout<<"4. Display list 1.\n";
cout<<"5. Display list 2.\n";
cout<<"6. Display list 3.\n";
cout<<"7. Exit\n";
cout<<"Enter choice:";
cin>>choice;
switch(choice)
{
case 1:
do
{
cout<<"Enter the number:";
cin>>number;
newptr1=createNewList1(number);
insertNewList1(newptr1);
cout<<"\nWant to enter more numbers(y or n):";
cin>>ans;
}
while(ans=='y');
break;
case 2:
do
{
cout<<"Enter the number:";
cin>>number;
newptr2=createNewList2(number);
insertNewList2(newptr2);
cout<<"Want to enter more numbers(y or n):";
cin>>ans;
}
while(ans=='y');
break;
case 3:
ptr1=start1;ptr2=start2;
while(ptr1!=NULL && ptr2!=NULL)
{
int num=ptr1->num+ptr2->num;
num=num;
list3 *newptr=createNewList3(num);
insertNewList3(newptr);
ptr1=ptr1->next;
ptr2=ptr2->next;
}
break;
case 4:
clrscr();
ptr1=start1;
while(ptr1!=NULL)
{
cout<<ptr1->num<<"->";
ptr1=ptr1->next;
}
getch();
break;
case 5:
clrscr();
ptr2=start2;
while(ptr2!=NULL)
{
cout<<ptr2->num<<"->";
ptr2=ptr2->next;
}
getch();
break;
case 6:
clrscr();
ptr3=start3;
while(ptr3!=NULL)
{
cout<<ptr3->num<<"->";
ptr3=ptr3->next;
}
getch();
break;
case 7:
break;
default:
cout<<"Enter only 1-7";
break;
}
}
while(choice!=7);
}
list1 *createNewList1(int num)
{
list1 *ptr=new list1();
ptr->num=num;
ptr->next=NULL;
return ptr;
}
list2 *createNewList2(int num)
{
list2 *ptr=new list2();
ptr->num=num;
ptr->next=NULL;
return ptr;
}
list3 *createNewList3(int num)
{
list3 *ptr=new list3();
ptr->num=num;
ptr->next=NULL;
return ptr;
}
void insertNewList1(list1 *ptr)
{
if(start1==NULL)
{
start1=rear1=ptr;
return;
}
rear1->next=ptr;
rear1=ptr;
}
void insertNewList2(list2 *ptr)
{
if(start2==NULL)
{
start2=rear2=ptr;
return;
}
rear2->next=ptr;
rear2=ptr;
}
void insertNewList3(list3 *ptr)
{
if(start3==NULL)
{
start3=rear3=ptr;
return;
}
rear3->next=ptr;
rear3=ptr;
}
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks