PHP Data Structure 4: The addition of a polynomial PHP single-chain implementation
GetIsEmpty ()&&! $ B-> getIsEmpty () {$ pa = $ a-> mNext; $ pb = $ B-> mNext; while ($ pa! = Null & $ pb! = Null) {if ($ pa-> mElem ['expn '] <$ pb-> mElem ['expn']) {$ c-> getInsertElem ($ c-> getLength (), $ pa-> mElem); $ pc = $ pa; $ pa = $ pa-> mNext ;} if ($ pa-> mElem ['exp']> $ pb-> mElem ['exp']) {$ c-> getInertElem ($ c-> getLength (), $ pb-> mElem); $ pc = $ pb; $ pb = $ pb-> mNext ;} if ($ pa-> mElem ['exp'] = $ pa-> mElem ['exp']) {$ ccoef = $ pa-> mElem ['coef '] + $ pb-> mElem ['coef']; if (abs ($ ccoef) <1.0E-10) {$ pa = $ pa-> mNext; $ pb = $ pb-> mNext;} else {$ pa-> mElem ['coe F'] = $ ccoef; $ c-> getInsertElem ($ c-> getLength (), $ pa-> mElem); $ pa = $ pa-> mNext; $ pb = $ pb-> mNext ;}}if ($ pa = null) {while ($ pb! = Null) {$ c-> getInsertElem ($ c-> getLength (), $ pb-> mElem); $ pb = $ pb-> mNext ;}} elseif ($ pb = null) {while ($ pa! = Null) {$ c-> getInsertElem ($ c-> getLength (), $ pa-> mElem); $ pa = $ pa-> mNext ;}}}} // coef indicates coefficient // expn indicates index $ adata = array ('coef '=> 5, 'expn' => 0 ,), array ('coef '=> 2, 'expn' => 1,), array ('coef '=> 4, 'expn' => 2 ,), array ('coef '=> 13, 'expn' => 3,), array ('coef '=> 160, 'expn' => 4 ,), array ('coef '=> 12, 'expn' => 5), array ('coef '=> 23, 'expn' => 6 ,), array ('coef '=> 34, 'expn' => 7,), array ('coef '=> 22, 'expn' => 8 ,),); $ bdata = array ('coef '=> 16, 'expn' => 0,), array ('coef '=> 9, 'expn '=> 2,), array ('coef' => 17, 'expn '=> 4,), array ('coef' =>-12, 'expn '=> 5,),); $ a = new SingleLinkedList (); $ B = new SingleLinkedList (); $ c = new SingleLinkedList (); $ a-> getTailCreateSLL ($ adata); $ B-> getTailCreateSLL ($ bdata); polynomialaddition ($ a, $ B, $ c ); echo "\ $ a polynomial data:";var_dump($a->getAllElem());echo "
"; Echo" \ $ B polynomial data:";var_dump($b->getAllElem());echo "
"; Echo" result of adding a polynomial:";var_dump($c->getAllElem());echo "
";?>