Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Must be in java and in correlation of the code I've attached at bottom /** * * Student: * */ public class A1LinkedList{ public static

Must be in java and in correlation of the code I've attached at bottom

image text in transcribed

/** * * Student: * */ public class A1LinkedList{ public static void main(String argc[]){ LinkedList sl = new LinkedList(); DLinkedList dl = new DLinkedList(); PolynomialLinkedList sum = new PolynomialLinkedList(); PolynomialLinkedList prod = new PolynomialLinkedList(); for (int i = 40; i >= -40; i-=2) { sl.add(i*i); dl.add(i); } try { sl.insert(111, sl.getNode(38), sl.getNode(40)); if (sl.detectLoop()) System.out.println("Loop! Wrong"); else System.out.println("No loop."); sl.insert(123, sl.getNode(38), sl.getNode(36)); if (sl.detectLoop()) System.out.println("Loop!"); else System.out.println("No loop. Wrong"); sl.insert(321, sl.getNode(38), sl.getNode(37)); if (sl.detectLoop()) System.out.println("Loop!"); else System.out.println("No loop. Wrong"); dl.print(); System.out.println("Index 20, has 0? " + dl.get(20)); System.out.println("Index 21, has -2? " + dl.get(21)); } catch(Exception e){ e.printStackTrace(); } PolynomialLinkedList p1, p2, p3, p4; p1 = new PolynomialLinkedList(2,3); p2 = new PolynomialLinkedList(3,2); p3 = p1.add(p2); p1 = new PolynomialLinkedList(1,0); p4 = p2.add(p1); sum = p3.add(p4); // extra credit //prod = p3.multiply(p4); p3.print(); p4.print(); sum.print(); //prod.print(); } } class LinkedList{ private static class Node{ private E element; private Node next; public Node(E e, Node n){ element = e; next = n; } public E getElement(){ return element; } public Node getNext(){ return next; } public void setElement(E e){ element = e; } public void setNext(Node n){ next = n; } } private Node head; public LinkedList(){ head = null; } public void add(E e){ Node temp = new Node(e, head); head = temp; } public void insert(E e, Node p, Node n){ p.setNext(new Node(e, n)); } public Node getNode(int i) throws Exception{ Node temp = head; while (i > 0){ if (temp == null) throw new Exception("Out of bound"); temp = temp.getNext(); i--; } return temp; } public boolean detectLoop(){ //implement this method return true; } } class DLinkedList{ private static class DNode{ private E element; private DNode prev; private DNode next; public DNode(E e){ this(e, null, null); } public DNode(E e, DNode p, DNode n){ element = e; prev = p; next = n; } public E getE(){ return element; } public DNode getPrev(){ return prev; } public DNode getNext(){ return next; } public void setE(E e){ element = e; } public void setPrev(DNode p){ prev = p; } public void setNext(DNode n){ next = n; } } private DNode header; private DNode trailer; private int size; public DLinkedList(){ header = new DNode(null); trailer = new DNode(null, header, null); header.setNext(trailer); size = 0; } public void add(E e){ DNode oLast = trailer.getPrev(); DNode temp = new DNode(e, oLast, trailer); oLast.setNext(temp); trailer.setPrev(temp); size++; } public E get(int i) throws Exception{ if (i = size) throw new Exception("Bad Index"); DNode ansNode; if (i  0){ ansNode = ansNode.getNext(); i--; } } //complete this method return null; } public void print(){ DNode temp = header.getNext(); while (temp != trailer){ System.out.print(temp.getE().toString() + ", "); temp = temp.getNext(); } System.out.println(); } } class PolynomialLinkedList{ private static class PNode{ private int coe; private int exp; private PNode next; public PNode(int c, int e){ this(c, e, null); } public PNode(int c, int e, PNode n){ coe = c; exp = e; next = n; } public void setCoe(int c){ coe = c;} public void setExp(int e){ exp = e;} public void setNext(PNode n){ next = n;} public int getCoe(){ return coe;} public int getExp(){ return exp;} public PNode getNext(){ return next;} } private PNode first; private PNode last; public PolynomialLinkedList(){ first = last = null; } public PolynomialLinkedList(int c, int e){ PNode tempn = new PNode(c, e); first = last = tempn; } public void print(){ if (first == null){ System.out.println(); return; } PNode temp = first; String ans = ""; while (temp != null){ if (temp.getCoe() > 0) { if (temp != first) ans = ans + " + "; ans = ans + temp.getCoe(); } else if (temp.getCoe()   Part A - coding 1. Implement Singly Linked List detectLoop which check whether the linked list contains a loop. It's an instance method that takes no parameter. It returns true if the linked list contains a loop that goes on forever if tracing the list from head. It returns false if it does not contain a loop. This method should be O(n), n as the number of nodes in the linked list. 2. Complete Doubly Linked List get method which behaves like the operator[] of array. It's an instance method that takes a position and an element, and then returns the element from this specific position. It throws an exception if the position is out of bound. It traverses the list from header if the position is closer to the header and traverses the list from trailer otherwise. Part of the method is given. 3. Implement Polynomial Linked List add method which adds two polynomials in standard form and returns sum polynomial which also has to be in standard form. Rewrite the instance method by merging the caller and parameter Polynomial Linked List into another sum Polynomial. Both caller and parameter Polynomials are expected to be in standard form, the method should return the sum Polynomial also in standard form. A standard form polynomial has no duplicate terms (each term's exponent is unique) and the exponent is in descending order. This method should be O(n), n as the number of terms in the sum polynomial

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Successful Keyword Searching Initiating Research On Popular Topics Using Electronic Databases

Authors: Randall MacDonald, Susan MacDonald

1st Edition

0313306761, 978-0313306761

Students also viewed these Databases questions

Question

Look up, smile and establish eye contact.

Answered: 1 week ago

Question

4. Who should be invited to attend?

Answered: 1 week ago

Question

7. How will you encourage her to report back on the findings?

Answered: 1 week ago

Question

Were the decisions based on appropriate facts?

Answered: 1 week ago