Addition of two polynomials using Linked Lists
Write a C++ program for Addition of two polynomials using Linked Lists. Here’s simple C++ program for Addition of two polynomials using Linked Lists in C++ Programming Language.
What is Linked List ?
Linked list is a linear data structure that contains sequence of elements such that each element links to its next element in the sequence. Each link contains a connection to another link.
Following are the important terms to understand the concept of Linked List.
- Link − Each link of a linked list can store a data called an element.
- Next − Each link of a linked list contains a link to the next link called Next.
Each element in a linked list is called as “Node”. Each node consists of its own data and the address of the next node and forms a chain. Linked Lists are used to create trees and graphs.
Below is the source code for C++ program for Addition of two polynomials using Linked Lists which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
/* C++ program for Addition of two polynomials using Linked Lists */ #include <iostream> using namespace std; class poly { private : struct polynode { float coeff ; int exp ; polynode *link ; } *p ; public : poly( ) ; void poly_append ( float c, int e ) ; void display_poly( ) ; void poly_add( poly &l1, poly &l2 ) ; ~poly( ) ; } ; poly :: poly( ) { p = NULL ; } void poly :: poly_append ( float c, int e ) { polynode *temp = p ; if ( temp == NULL ) { temp = new polynode ; p = temp ; } else { while ( temp -> link != NULL ) temp = temp -> link ; temp -> link = new polynode ; temp = temp -> link ; } temp -> coeff = c ; temp -> exp = e ; temp -> link = NULL ; } void poly :: display_poly( ) { polynode *temp = p ; int f = 0 ; cout << endl ; while ( temp != NULL ) { if ( f != 0 ) { if ( temp -> coeff > 0 ) cout << " + " ; else cout << " " ; } if ( temp -> exp != 0 ) cout << temp -> coeff << "x^" << temp -> exp ; else cout << temp -> coeff ; temp = temp -> link ; f = 1 ; } } void poly :: poly_add ( poly &l1, poly &l2 ) { polynode *z ; if ( l1.p == NULL && l2.p == NULL ) return ; polynode *temp1, *temp2 ; temp1 = l1.p ; temp2 = l2.p ; while ( temp1 != NULL && temp2 != NULL ) { if ( p == NULL ) { p = new polynode ; z = p ; } else { z -> link = new polynode ; z = z -> link ; } if ( temp1 -> exp < temp2 -> exp ) { z -> coeff = temp2 -> coeff ; z -> exp = temp2 -> exp ; temp2 = temp2 -> link ; } else { if ( temp1 -> exp > temp2 -> exp ) { z -> coeff = temp1 -> coeff ; z -> exp = temp1 -> exp ; temp1 = temp1 -> link ; } else { if ( temp1 -> exp == temp2 -> exp ) { z -> coeff = temp1 -> coeff + temp2 -> coeff ; z -> exp = temp1 -> exp ; temp1 = temp1 -> link ; temp2 = temp2 -> link ; } } } } while ( temp1 != NULL ) { if ( p == NULL ) { p = new polynode ; z = p ; } else { z -> link = new polynode ; z = z -> link ; } z -> coeff = temp1 -> coeff ; z -> exp = temp1 -> exp ; temp1 = temp1 -> link ; } while ( temp2 != NULL ) { if ( p == NULL ) { p = new polynode ; z = p ; } else { z -> link = new polynode ; z = z -> link ; } z -> coeff = temp2 -> coeff ; z -> exp = temp2 -> exp ; temp2 = temp2 -> link ; } z -> link = NULL ; } poly :: ~poly( ) { polynode *q ; while ( p != NULL ) { q = p -> link ; delete p ; p = q ; } } int main( ) { poly p1 ; p1.poly_append ( 1.4, 5 ) ; p1.poly_append ( 1.5, 4 ) ; p1.poly_append ( 1.7, 2 ) ; p1.poly_append ( 1.8, 1 ) ; p1.poly_append ( 1.9, 0 ) ; cout << "\nFirst polynomial:" ; p1.display_poly( ) ; poly p2 ; p2.poly_append ( 1.5, 6 ) ; p2.poly_append ( 2.5, 5 ) ; p2.poly_append ( -3.5, 4 ) ; p2.poly_append ( 4.5, 3 ) ; p2.poly_append ( 6.5, 1 ) ; cout << "\nSecond polynomial:" ; p2.display_poly( ) ; poly p3 ; p3.poly_add ( p1, p2 ) ; cout << "\nResultant polynomial: " ; p3.display_poly( ) ; return 0; } |
OUTPUT :
1 2 3 4 5 6 7 8 9 10 |
/* C++ program for Addition of two polynomials using Linked Lists */ First polynomial: 1.4x^5 + 1.5x^4 + 1.7x^2 + 1.8x^1 + 1.9 Second polynomial: 1.5x^6 + 2.5x^5 -3.5x^4 + 4.5x^3 + 6.5x^1 Resultant polynomial: 1.5x^6 + 3.9x^5 -2x^4 + 4.5x^3 + 1.7x^2 + 8.3x^1 + 1.9 |
Above is the source code and output for C++ program for Addition of two polynomials using Linked Lists which is successfully compiled and run on Windows System to produce desired output.
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.
Thanks for reading the post….
ths is vrry booring yaaa……toooo much..jst shrt th prgrm