Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.99 KB | None | 0 0
  1. package eg.edu.alexu.csd.datastructure.linkedList;
  2.  
  3. import java.awt.*;
  4.  
  5. class main{
  6. public static void main(String[] args) {
  7.  
  8.  
  9.  
  10.  
  11. PolynomialSolver solver = new PolynomialSolver();
  12.  
  13.  
  14. DLinkedList list = new DLinkedList();
  15.  
  16. solver.setPolynomial('A',new int[][]{new int[] {1,2}, new int[] {-1,1}});
  17. solver.setPolynomial('B',new int[][]{new int[] {4,2}, new int[] {-9,3}});
  18. solver.setPolynomial('C',new int[][]{new int[] {4,3}, new int[] {-1,0}});
  19. solver.setPolynomial('X',new int[][]{new int[] {-14,2}, new int[] {11,0},new int[]{ 13,7}} );
  20. solver.setPolynomial('N',new int[][]{new int[] {114,-2}, new int[] {-151,19}});
  21.  
  22.  
  23. int [][] ans =solver.multiply('A','B');
  24.  
  25. for(int i=0;i<ans.length;i++){
  26. System.out.println("co is"+ans[i][0]);
  27. System.out.println("po is"+ans[i][1]);
  28. }
  29.  
  30.  
  31. }
  32.  
  33. }
  34.  
  35. class PolynomialSolver implements IPolynomialSolver {
  36. private DLinkedList list = new DLinkedList();
  37. private Polynomial ans = new Polynomial('R', null);
  38.  
  39. @Override
  40. public void setPolynomial(char poly, int[][] terms) {
  41. boolean found = false;
  42. if (!list.isEmpty()) {
  43. list.setCursor(0);
  44. if (((Polynomial) list.getCurrent()).getName() == poly) {
  45. ((Polynomial) list.getCurrent()).setTerms(terms);
  46. found = true;
  47. } else {
  48. while (list.hasNext()) {
  49. if (((Polynomial) list.getNext()).getName() == poly) {
  50. ((Polynomial) list.getCurrent()).setTerms(terms);
  51. found = true;
  52. }
  53. }
  54. }
  55.  
  56. }
  57. if (list.isEmpty() || !found) {
  58. list.add(new Polynomial(poly, terms));
  59. }
  60. } // done and tested
  61.  
  62. @Override
  63. public String print(char poly) {
  64. if (list.isEmpty()) return "No Polynomial found";
  65. Polynomial polynomial = getPolynomial(poly);
  66. if (polynomial == null) return null;
  67. return polynomial.toString();
  68. } // done and tested
  69.  
  70. private Polynomial getPolynomial(char poly) {
  71. if (list.isEmpty()) return null;
  72. list.setCursor(0);
  73. if (((Polynomial) list.getCurrent()).getName() == poly) {
  74. return (Polynomial) list.getCurrent();
  75. }
  76. while (list.hasNext()) {
  77. if (((Polynomial) list.getNext()).getName() == poly) {
  78. return (Polynomial) list.getCurrent();
  79. }
  80. }
  81. return null;
  82. } // done and tested
  83.  
  84. @Override
  85. public void clearPolynomial(char poly) {
  86. if (list.isEmpty()) return;
  87. list.setCursor(0);
  88. if (((Polynomial) list.getCurrent()).getName() == poly) {
  89. list.removeCurrent();
  90. } else {
  91. while (list.hasNext()) {
  92. if (((Polynomial) list.getNext()).getName() == poly) {
  93. list.removeCurrent();
  94. break;
  95. }
  96. }
  97. }
  98. } // done and tested
  99.  
  100. @Override
  101. public float evaluatePolynomial(char poly, float value) {
  102. Polynomial polynomial = getPolynomial(poly);
  103. if (polynomial == null) return Float.MAX_VALUE;
  104. if (polynomial.getTerms().isEmpty()) return 0;
  105. DLinkedList list = polynomial.getTerms();
  106. float sum = 0;
  107. list.setCursor(0);
  108. while (true){
  109. sum += ((float) ((Point)list.getCurrent()).x * (float) Math.pow((double) value, (double) ((Point)list.getCurrent()).y));
  110. if (list.hasNext()) {
  111. list.getNext();
  112. } else {
  113. break;
  114. }
  115. }
  116.  
  117. return sum;
  118. } // done and tested
  119.  
  120.  
  121. private int[][] theEndOfWar(char poly1, char poly2, int sign) {
  122. Polynomial var1 = null;
  123. Polynomial var2 = null;
  124. Polynomial temp;
  125. for (int i = 0; i < list.size(); i++) {
  126. temp = (Polynomial) list.get(i);
  127. if (temp.name == poly1) {
  128. var1 = temp;
  129. }
  130. if (temp.name == poly2) {
  131. var2 = temp;
  132. }
  133. } if (var1 == null || var2 == null)
  134. return null;
  135.  
  136. DLinkedList con = new DLinkedList();
  137. DLinkedList death = new DLinkedList();
  138. DLinkedList warPower = new DLinkedList();
  139. for (int i = 0; i < var1.listOfTerms.size(); i++) {
  140. boolean found = false;
  141. int die = 0;
  142. for (int j = 0; j < var2.listOfTerms.size(); j++) {
  143. if (((Point)var1.listOfTerms.get(i)).y == ((Point)var2.listOfTerms.get(j)).y) {
  144. found = true;
  145. die = j;
  146. break;
  147. }
  148. }
  149. if (found) {
  150. con.add(((Point)var1.listOfTerms.get(i)).y );
  151. int fire = 1;
  152. if (sign == 2)
  153. fire = -1;
  154. death.add(((Point)var1.listOfTerms.get(i)).x + fire * (((Point)var2.listOfTerms.get(die)).x ));
  155. if ((int) death.get(death.size() - 1) == 0) {
  156. death.remove(death.size() - 1);
  157. } else {
  158. warPower.add(((Point)var2.listOfTerms.get(i)).y);//var2.[i][1]
  159. }
  160. }
  161. }
  162.  
  163. for (int i = 0; i < var1.listOfTerms.size(); i++) {
  164. if (!con.contains(((Point)var1.listOfTerms.get(i)).y)) {
  165. death.add(((Point)var1.listOfTerms.get(i)).x);
  166. warPower.add(((Point)var1.listOfTerms.get(i)).y);
  167. }
  168. }
  169. int fire = 1;
  170. if (sign == 2)
  171. fire = -1;
  172. for (int j = 0; j <var2.listOfTerms.size(); j++) {
  173. if (!con.contains(((Point)var2.listOfTerms.get(j)).y)) {
  174. death.add(fire * ((Point)var2.listOfTerms.get(j)).x);
  175. warPower.add(((Point)var2.listOfTerms.get(j)).y);
  176. }
  177. }
  178. int[][] ans = new int[death.size()][2];
  179. for (int i = 0; i < ans.length; i++) {
  180. ans[i][0] = (int) death.get(i);
  181. ans[i][1] = (int) warPower.get(i);
  182. }
  183.  
  184. for (int i = 0; i < death.size(); i++) {
  185. if ((int) death.get(i) == 0) {
  186. death.remove(i);
  187. warPower.remove(i);
  188. i--;
  189. }
  190. }
  191.  
  192. int p = 0;
  193. int c = 0;
  194. for (int i = 0; i < ans.length; i++) {
  195. for (int j = 0; j < ans.length - i - 1; j++) {
  196. if (ans[j][1] < ans[j + 1][1]) {
  197. p = ans[j][1];
  198. c = ans[j][0];
  199. ans[j][1] = ans[j + 1][1];
  200. ans[j][0] = ans[j + 1][0];
  201. ans[j + 1][1] = p;
  202. ans[j + 1][0] = c;
  203. }
  204. }
  205. }
  206.  
  207. if (ans.length == 0) {
  208. int[][] ansh = new int[1][2];
  209. ansh[0][0] = 0;
  210. ansh[0][1] = 0;
  211. setPolynomial('R', ansh);
  212. return ansh;
  213. }
  214. setPolynomial('R', ans);
  215.  
  216. return ans;
  217. }
  218.  
  219.  
  220. @Override
  221. public int[][] add(char poly1, char poly2) {
  222.  
  223. return this.theEndOfWar(poly1, poly2, 1);// 1 for add two for substraction
  224. }
  225.  
  226. @Override
  227. public int[][] subtract(char poly1, char poly2) {
  228. return this.theEndOfWar(poly1, poly2, 2);// 1 for add two for substraction
  229.  
  230. }
  231.  
  232.  
  233.  
  234.  
  235. @Override
  236. public int[][] multiply(char poly1, char poly2) {
  237. Polynomial var1 = null;
  238. Polynomial var2 = null;
  239. Polynomial temp;
  240. for (int i = 0; i < list.size(); i++) {
  241. temp = (Polynomial) list.get(i);
  242. if (temp.name == poly1) {
  243. var1 = temp;
  244. }
  245. if (temp.name == poly2) {
  246. var2 = temp;
  247. }
  248. }
  249. if (var1 == null || var2 == null)
  250. return null;
  251. int p1 = 0;
  252. int c1 = 0;
  253.  
  254. DLinkedList death = new DLinkedList();
  255. DLinkedList warPower = new DLinkedList();
  256.  
  257. for (int i = 0; i < var1.listOfTerms.size(); i++) {
  258. for (int j = 0; j < var2.listOfTerms.size(); j++) {
  259. p1 = ((Point)var1.listOfTerms.get(i)).y + ((Point)var2.listOfTerms.get(j)).y;
  260. c1 = ((Point)var1.listOfTerms.get(i)).x * ((Point)var2.listOfTerms.get(j)).x;
  261. boolean found = false;
  262. for (int k = 0; k < warPower.size(); k++) {
  263. if (p1 == (int) warPower.get(k)) {
  264. death.set(k, (int) death.get(k) + c1);
  265. found = true;
  266. break;
  267. }
  268. }
  269. if (!found) {
  270. death.add(c1);
  271. warPower.add(p1);
  272. }
  273. }
  274. }
  275.  
  276.  
  277. for (int i = 0; i < death.size(); i++) {
  278. if ((int) death.get(i) == 0) {
  279. death.remove(i);
  280. warPower.remove(i);
  281. i--;
  282. }
  283. }
  284.  
  285. int[][] ans = new int[death.size()][2];
  286. for (int i = 0; i < ans.length; i++) {
  287. ans[i][0] = (int) death.get(i);
  288. ans[i][1] = (int) warPower.get(i);
  289. }
  290. int p = 0;
  291. int c = 0;
  292. for (int i = 0; i < ans.length; i++) {
  293. for (int j = 0; j < ans.length - i - 1; j++) {
  294. if (ans[j][1] < ans[j + 1][1]) {
  295. p = ans[j][1];
  296. c = ans[j][0];
  297. ans[j][1] = ans[j + 1][1];
  298. ans[j][0] = ans[j + 1][0];
  299. ans[j + 1][1] = p;
  300. ans[j + 1][0] = c;
  301. }
  302. }
  303. }
  304. if (ans.length == 0) {
  305. int[][] ansh = new int[1][2];
  306. ansh[0][0] = 0;
  307. ansh[0][1] = 0;
  308. setPolynomial('R', ansh);
  309.  
  310. return ansh;
  311. }
  312. setPolynomial('R', ans);
  313. return ans;
  314.  
  315.  
  316. }
  317. @Override
  318. public String toString() {
  319. if (list.isEmpty()) return "No Polynomials found";
  320. StringBuilder s = new StringBuilder("");
  321. list.setCursor(0);
  322. s.append(list.getCurrent()).append('\n');
  323. while (list.hasNext()) {
  324. s.append(list.getNext()).append('\n');
  325.  
  326. }
  327. return s.toString();
  328. } // done and tested
  329.  
  330.  
  331. private class Polynomial {
  332. private final char name;
  333. private DLinkedList listOfTerms = new DLinkedList();
  334.  
  335. Polynomial(char name, int[][] terms) {
  336. this.name = name;
  337. setTerms(terms);
  338. }
  339.  
  340. char getName() {
  341. return name;
  342. }
  343.  
  344. DLinkedList getTerms() {
  345. return listOfTerms;
  346. }
  347.  
  348. public void setTerms(int[][] terms) {
  349. this.listOfTerms.clear();
  350. if (terms == null) return;
  351. for (int[] term : terms) {
  352. this.listOfTerms.add(new Point(term[0], term[1]));
  353. }
  354. }
  355.  
  356. @Override
  357. public boolean equals(Object obj) {
  358. Polynomial polynomial = (Polynomial) obj;
  359. if (this.getTerms().size() != polynomial.getTerms().size()) return false;
  360. if (this.listOfTerms.size() != 0){
  361. this.listOfTerms.setCursor(0);
  362. polynomial.getTerms().setCursor(0);
  363. if (!((Point)this.listOfTerms.getCurrent()).equals((Point)polynomial.listOfTerms.getCurrent())){
  364. return false;
  365. }
  366. }
  367. return this.getName() == polynomial.getName();
  368. }
  369.  
  370. private int[][] linkedListToArray(DLinkedList list){
  371. int[][] arr = new int[list.size()][2];
  372. list.setCursor(0);
  373. int i = 0;
  374. if (list.getCurrent() != null) {
  375. arr[i][0] = ((Point) list.getCurrent()).x;
  376. arr[i++][1] = ((Point) list.getCurrent()).y;
  377. }
  378. while (list.hasNext()){
  379. Point point = ((Point) list.getNext());
  380. arr[i][0] = point.x;
  381. arr[i++][1] = point.y;
  382. }
  383. return arr;
  384. }
  385.  
  386. @Override
  387. public String toString() {
  388. int[][] terms = linkedListToArray(this.listOfTerms);
  389. StringBuilder s = new StringBuilder(this.name + ": ");
  390. for (int i = 0; i < terms.length; i++) {
  391. if (terms[i][0] != 0) { // total
  392. if (terms[i][1] == 0) { // absolute value
  393. if (i == 0) { // first term
  394. s.append(terms[i][0]);
  395. } else { // not first term
  396. if (terms[i][0] < 0) s.append("- ").append(Math.abs(terms[i][0]));
  397. else if (terms[i][0] > 0) s.append("+ ").append(terms[i][0]);
  398. }
  399. continue;
  400. }
  401. if (i == 0) { // first term
  402. if (terms[i][0] != 1) s.append(terms[i][0]);
  403. s.append('X').append('^').append(terms[i][1]);
  404. } else {
  405. if (terms[i][0] > 0) { //+ ve coefficient meddle term
  406. s.append("+ ");
  407. if (terms[i][0] != 1) s.append(terms[i][0]);
  408. s.append('X').append('^').append(terms[i][1]);
  409. } else { // -ve coefficient meddle term
  410. s.append("- ");
  411. if (terms[i][0] != -1) s.append(Math.abs(terms[i][0]));
  412. s.append('X').append('^').append(terms[i][1]);
  413. }
  414. }
  415. s.append(' ');
  416. }
  417. }
  418. return s.toString();
  419. } // done and tested
  420. } // done and tested
  421. }
  422.  
  423. interface ILinkedList {
  424. /**
  425. * Inserts a specified element at the specified position in the list.
  426. * @param index
  427. * @param element */
  428. public void add(int index, Object element);
  429. /**
  430. * Inserts the specified element at the end of the list.
  431. * @param element
  432. */
  433. public void add(Object element);
  434. /**
  435. * @param index
  436. * @return the element at the specified position in this list.
  437. */
  438. public Object get(int index);
  439.  
  440. /**
  441. * Replaces the element at the specified position in this list with the * specified element. * @param index * @param element */
  442. public void set(int index, Object element);
  443. /**
  444. * Removes all of the elements from this list. */
  445. public void clear();
  446. /**
  447. * @return true if this list contains no elements. */
  448. public boolean isEmpty();
  449. /**
  450. * Removes the element at the specified position in this list. * @param index */
  451. public void remove(int index);
  452. /**
  453. * @return the number of elements in this list. */
  454. public int size();
  455. /**
  456. * @param fromIndex * @param toIndex * @return a view of the portion of this list between the specified * fromIndex and toIndex, inclusively. */
  457. public ILinkedList sublist(int fromIndex, int toIndex);
  458. /**
  459. * @param o * @return true if this list contains an element with the same value as the * specified element. */
  460. public boolean contains(Object o);
  461.  
  462. }
  463.  
  464. interface IPolynomialSolver {
  465. /** * Set polynomial terms (coefficients & exponents)
  466. * @param poly
  467. * name of the polynomial
  468. * @param terms
  469. * array of [coefficients][exponents]
  470. */
  471. void setPolynomial(char poly, int[][] terms);
  472. /**
  473. * Print the polynomial in ordered human readable representation
  474. * @param poly
  475. * name of the polynomial
  476. * @return polynomial in the form like 27x^2+x-1
  477. */
  478. String print(char poly);
  479. /**
  480. * Clear the polynomial
  481. * @param poly
  482. * name of the polynomial
  483. */
  484. void clearPolynomial(char poly);
  485. /** Evaluate the polynomial @param poly
  486. * name of the polynomial
  487. * @param
  488. * poly the polynomial constant value
  489. * @return
  490. * the value of the polynomial
  491. */
  492. float evaluatePolynomial(char poly, float value);
  493. /**
  494. * Add two polynomials
  495. * @param poly1
  496. * first polynomial
  497. * @param poly2
  498. * second polynomial
  499. * @return the result polynomial
  500. */
  501. int[][] add(char poly1, char poly2);
  502. /**
  503. * Subtract two polynomials
  504. * @param poly1
  505. * first polynomial
  506. * @param poly2
  507. * second polynomial
  508. * @return the result polynomial
  509. */
  510. int[][] subtract(char poly1, char poly2);
  511. /**
  512. * Multiply two polynomials
  513. * @param poly1
  514. * first polynomial
  515. * @param poly2
  516. * second polynomial
  517. * @return the result polynomial
  518. */
  519. int[][] multiply(char poly1, char poly2);
  520. }
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567. class DLinkedList implements ILinkedList {
  568. private DNode head;
  569. private DNode tail;
  570. private int size = 0;
  571. private int cursorIndex = 0;
  572. private DNode cursorNode;
  573.  
  574.  
  575. public DLinkedList() {
  576. this(null);
  577. }
  578.  
  579. public DLinkedList(Object obj) {
  580. head = new DNode(obj,null,null);
  581. tail = new DNode(null,null,head);
  582. head.setNext(tail);
  583. cursorNode = head;
  584. }
  585.  
  586. @Override
  587. public void add(int index, Object element) throws RuntimeException{
  588. if (index > size || index < 0){
  589. throw new ArrayIndexOutOfBoundsException("index out of bound, index = " + index + " , size = " + size );
  590. } else if ( element == null ){
  591. throw new RuntimeException("null value isn't allowed to be added to to list");
  592. } else if (index == size) {
  593. add(element);
  594. return;
  595. } else if (index == 0){
  596. DNode newNode = new DNode(element,head,null);
  597. head.setPrevious(newNode);
  598. head = newNode;
  599. } else {
  600. DNode currentNode = head;
  601. for (int i = index; i > 0 ; i--) {
  602. currentNode = currentNode.getNext();
  603. }
  604. DNode newNode = new DNode(element,currentNode,currentNode.getPrevious());
  605. currentNode.getPrevious().setNext(newNode);
  606. currentNode.setPrevious(newNode);
  607. }
  608. size++;
  609. } // done and tested
  610.  
  611. public int indexOf(Object obj){
  612. DNode currentNode = head;
  613. for (int i = 0; i < size ; i++) {
  614. if (currentNode.getData().equals(obj)) return i;
  615. currentNode = currentNode.getNext();
  616. }
  617. return -1;
  618. }
  619.  
  620. @Override
  621. public void add(Object element) throws RuntimeException{
  622. if (element == null) {
  623. throw new RuntimeException("null value isn't allowed to be added to to list");
  624. }
  625. if (this.isEmpty()){
  626. head.setData(element);
  627. } else if (this.tail.getData() == null){
  628. tail.setData(element);
  629. } else {
  630. DNode newNode = new DNode(element,null,tail);
  631. tail.setNext(newNode);
  632. tail = newNode;
  633. }
  634. this.size++;
  635. } // done and tested
  636.  
  637. @Override
  638. public Object get(int index) throws RuntimeException{
  639. DNode node = this.findNode(index);
  640. if (node == null){
  641. throw new RuntimeException("didn't find any element in that index " + index);
  642. }
  643. return node.getData();
  644. } // done and tested
  645.  
  646. private DNode findNode(int index) throws RuntimeException{
  647. if (index >= size || index < 0){
  648. throw new RuntimeException("index out of bound, index = " + index + " , size = " + size);
  649. } else if (size == 1){
  650. return head;
  651. } else if (index == size - 1) return tail;
  652. DNode currentNode = head;
  653. for (int i = 0; i < index ; i++) {
  654. currentNode = currentNode.getNext();
  655. }
  656. return currentNode;
  657. } // done and tested
  658.  
  659. private DNode findNode(Object obj){
  660. DNode currentNode = head;
  661. for (int i = 0; i < size ; i++) {
  662. currentNode = currentNode.getNext();
  663. if (currentNode.getData().equals(obj)) return currentNode;
  664. }
  665. return null;
  666. }
  667.  
  668. @Override
  669. public void set(int index, Object element) {
  670. DNode node = findNode(index);
  671. node.setData(element);
  672. } // done and tested
  673.  
  674. @Override
  675. public void clear() { // tested and worked
  676. this.head = new DNode(null,null,null);
  677. this.tail = new DNode(null,null,this.head);
  678. this.head.setNext(this.tail);
  679. size = 0;
  680. } // tested and worked
  681.  
  682. @Override
  683. public boolean isEmpty() {
  684. return size == 0 || head.getData() == null;
  685. } // done and tested
  686.  
  687. @Override
  688. public void remove(int index) {
  689. DNode node = findNode(index);
  690. remove(node);
  691. } // done and tested
  692.  
  693. private void remove(DNode node){
  694. if (head.equals(node)) {
  695. if (head.getNext().getData() == null || head.getNext() == tail){
  696. head.setData(head.getNext().getData());
  697. head.getNext().setData(null);
  698. } else {
  699. head = head.getNext();
  700. head.setPrevious(null);
  701. }
  702. } else if (tail.equals(node)){
  703. if (tail.getPrevious() == head){
  704. tail.setData(null);
  705. } else {
  706. tail = tail.getPrevious();
  707. tail.setNext(null);
  708. }
  709. } else {
  710. node.getNext().setPrevious(node.getPrevious());
  711. node.getPrevious().setNext(node.getNext());
  712. }
  713. size--;
  714. }
  715.  
  716. @Override
  717. public int size() {
  718. return this.size;
  719. } // done and tested
  720.  
  721. @Override
  722. public ILinkedList sublist(int fromIndex, int toIndex) throws RuntimeException {
  723. if ((fromIndex > toIndex) || (fromIndex < 0 || fromIndex >= size) || (toIndex >= size)) {
  724. throw new RuntimeException("wrong indexes , start = " + fromIndex + " , end = " + toIndex + " , size = " + size);
  725. }
  726. DLinkedList list = new DLinkedList();
  727. DNode currentNode = findNode(fromIndex);
  728. for (int i = fromIndex ; i <= toIndex ; i++ ){
  729. list.add(currentNode);
  730. currentNode = currentNode.getNext();
  731. }
  732. return list;
  733. } // done and tested
  734.  
  735. public void setCursor (int index){
  736. cursorNode = findNode(index);
  737. } // done and tested
  738.  
  739. public Object getCurrent(){
  740. return this.cursorNode.getData();
  741. } // done and tested
  742.  
  743. public Object getNext(){
  744. if (cursorNode.getNext() != null){
  745. cursorNode = cursorNode.getNext();
  746. cursorIndex++;
  747. return cursorNode.getData();
  748. }
  749. return null;
  750. } // done and tested
  751.  
  752. public Object getPrevious(){
  753. if (cursorNode.getPrevious() != null){
  754. cursorNode = cursorNode.getPrevious();
  755. cursorIndex--;
  756. return cursorNode.getData();
  757. }
  758. return null;
  759. } // done and tested
  760.  
  761. public boolean hasNext(){
  762. if (cursorNode.getNext() == null) return false;
  763. return cursorNode.getNext().getData() != null;
  764. } // done and tested
  765.  
  766. public boolean hasPrevious(){
  767. if (cursorNode.getPrevious() == null) return false;
  768. return cursorNode.getPrevious().getData() != null;
  769. } // done and tested
  770.  
  771. public int getCursorIndex (){
  772. return cursorIndex;
  773. } // done and tested
  774.  
  775. public void removeCurrent(){
  776. if (this.isEmpty()) return;
  777. remove(cursorNode);
  778. if (this.isEmpty()) return;
  779. setCursor(0);
  780. } // done and tested
  781.  
  782. @Override
  783. public boolean contains(Object o) {
  784. if (this.isEmpty()) return false;
  785. if (o == null) return false;
  786. DNode currentNode = head;
  787. while (currentNode != null) {
  788. if (currentNode.getData().equals(o)) return true;
  789. currentNode = currentNode.getNext();
  790. }
  791. return false;
  792. } // done and tested
  793.  
  794. @Override
  795. public String toString() {
  796. StringBuilder s = new StringBuilder("[");
  797. DNode currentNode = head;
  798. for (int i = 0 ; i < size ; i++){
  799. s.append(currentNode.getData());
  800. if (i < size - 1) s.append(",");
  801. currentNode = currentNode.getNext();
  802. }
  803. s.append("]");
  804. return s.toString();
  805. } // done and tested
  806.  
  807. private class DNode {
  808. private Object data;
  809. private DNode next;
  810. private DNode previous;
  811.  
  812. public DNode(Object data, DNode next, DNode previous) {
  813. this.data = data;
  814. this.next = next;
  815. this.previous = previous;
  816. }
  817.  
  818. public Object getData() {
  819. return data;
  820. }
  821.  
  822. public void setData(Object data) {
  823. this.data = data;
  824. }
  825.  
  826. public DNode getNext() {
  827. return next;
  828. }
  829.  
  830. public void setNext(DNode next) {
  831. this.next = next;
  832. }
  833.  
  834. public DNode getPrevious() {
  835. return previous;
  836. }
  837.  
  838. public void setPrevious(DNode previous) {
  839. this.previous = previous;
  840. }
  841.  
  842. @Override
  843. public String toString() {
  844. if (this.data == null) return null;
  845. return this.data.toString();
  846. }
  847.  
  848. @Override
  849. public boolean equals(Object obj) {
  850. if (this == obj) return true;
  851. DNode node = (DNode) obj;
  852. return this.getData().equals(node.getData()) &&
  853. this.getNext().equals(node.getNext())&&
  854. this.getPrevious().equals(node.getPrevious());
  855. }
  856. } // done and tested
  857. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement