Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To use linked lists in a class. To implement binary numbers using a class. Creat

ID: 3531605 • Letter: T

Question

To use linked lists in a class. To implement binary numbers using a class. Create a linked list to store binary numbers one bit at a time. Use unordered list in Chapter 18. If the input number is 11010, it should be stored with each bit in a separate node. As you read the number (one character at a time), store each digit at the tail of a linked list. You will need to translate the character into a number. Overload the operator as a friend function to read in the number as follows: input number; // Store the number in a linked list. Overload the operator as a friend function. Overload the = operator. 5. Overload the + operator as a friend function. The + operator will add two numbers one bit at a time and return the result. You will need to reverse each input list to do the arithmetic (or use a doubly-linked list). Assign each input to a local list and do the reversal on the local copy. The result will be in reverse order. You will need to reverse it before returning it from the function. You will also need to handle the carry. As an example, to add the two numbers 11101 and 1101. (The number on the left is the carry in; set the carry in for the LSB to zero) 0 + 1 + 1 -> 0 (sum) carry 1 1 + 0 + 0 -> 1 carry 0 0 + 1 + 1 -> 0 carry 1 1 + 1 + 1 -> 1 carry 1 1 + 1 + (0) -> 0 carry 1 1 + (0) + (0) -> 1 carry 0 (The final carry will need an extra node at the end of the list.) You can simplify the class from the book by not using a template class. You can use a typedef for the data type as follows: typedef int datatype Read and write to disk. The main program will need an End-of-File loop. The input will consist of two numbers on a single line, with a space in between. The output will be the sum for each set of two numbers. Code the sum and carry functions as eloquently as possible, that is, use low-level or high-level logic rather than if else structures. Consider creating a doubly-linked list. TO HAND IN: Hard copy of the C ++ code including the specification and Implementation files Hard copy of the output A thumb drive Data for Program ^ indicates a space 101101^1001 1^11111 1011^1001110

Explanation / Answer

#include<stdio.h> #include<conio.h> #include<alloc.h> typedef struct linkedlist { int d; struct linkedlist *next; struct linkedlist *prev; }node; node * createnode(int data) { node *newnode; newnode = (node *)malloc(sizeof(node)); newnode->d=data; newnode->next=newnode->prev=NULL; return newnode; } void main() { int n1,n2,temp,i,sum,carry; node *head1,*head2,*head3,*move,*move1,*move2,*move3; clrscr(); head1=head2=head3=NULL; printf(" Enter no of bits of first no: "); scanf("%d",&n1); printf(" Enter Binary no: "); for(i=0;i<n1;i++) { scanf("%d",&temp); move=createnode(temp); if(head1==NULL) { head1=move; } else { move->next=head1; head1->prev=move; head1=move; } } printf(" Enter no of bits of second no: "); scanf("%d",&n2); printf(" Enter Binary no: "); for(i=0;i<n2;i++) { scanf("%d",&temp); move=createnode(temp); if(head2==NULL) { head2=move; } else { move->next=head2; head2->prev=move; head2=move; } } /* *********** create linkedlist for addition ********** */ move1=head1; move2=head2; move3=head3; while(head1!=NULL || head2 !=NULL) { if(head3==NULL) { head3=createnode(0); } else { move=head3; while(move->next!=NULL) move=move->next; move->next=createnode(0); } move1=move1->next; move2=move2->next; } /* *********** binary addition ********** */ move1=head1; move2=head2; move3=head3; carry=0; while(move3!=NULL) { sum=(move1->d)+(move2->d)+carry; if(sum==3) { move3->d=1; carry=1; } else if(sum==2) { move3->d=0; carry=1; } else if(sum==1) { move3->d=1; carry=0; } else { move3->d=0; carry=0; } move1=move1->next; move2=move2->next; move3=move3->next; } /* *********** Display binary addition ********** */ move=head3; printf(" Addition is : "); while(move!=NULL) { printf(" %d",move->d); } getch(); } #include<stdio.h> #include<conio.h> #include<alloc.h> typedef struct linkedlist { int d; struct linkedlist *next; struct linkedlist *prev; }node; node * createnode(int data) { node *newnode; newnode = (node *)malloc(sizeof(node)); newnode->d=data; newnode->next=newnode->prev=NULL; return newnode; } void main() { int n1,n2,temp,i,sum,carry; node *head1,*head2,*head3,*move,*move1,*move2,*move3; clrscr(); head1=head2=head3=NULL; printf(" Enter no of bits of first no: "); scanf("%d",&n1); printf(" Enter Binary no: "); for(i=0;i<n1;i++) { scanf("%d",&temp); move=createnode(temp); if(head1==NULL) { head1=move; } else { move->next=head1; head1->prev=move; head1=move; } } printf(" Enter no of bits of second no: "); scanf("%d",&n2); printf(" Enter Binary no: "); for(i=0;i<n2;i++) { scanf("%d",&temp); move=createnode(temp); if(head2==NULL) { head2=move; } else { move->next=head2; head2->prev=move; head2=move; } } /* *********** create linkedlist for addition ********** */ move1=head1; move2=head2; move3=head3; while(head1!=NULL || head2 !=NULL) { if(head3==NULL) { head3=createnode(0); } else { move=head3; while(move->next!=NULL) move=move->next; move->next=createnode(0); } move1=move1->next; move2=move2->next; } /* *********** binary addition ********** */ move1=head1; move2=head2; move3=head3; carry=0; while(move3!=NULL) { sum=(move1->d)+(move2->d)+carry; if(sum==3) { move3->d=1; carry=1; } else if(sum==2) { move3->d=0; carry=1; } else if(sum==1) { move3->d=1; carry=0; } else { move3->d=0; carry=0; } move1=move1->next; move2=move2->next; move3=move3->next; } /* *********** Display binary addition ********** */ move=head3; printf(" Addition is : "); while(move!=NULL) { printf(" %d",move->d); } getch(); }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote