Objectives To create a linked-chain data structure To understand how to apply ma
ID: 3860906 • Letter: O
Question
Objectives To create a linked-chain data structure To understand how to apply mathematical algorithms Instructions polynomial. The Polynomial class must support the following: For this program, you will implement two classes, one representing a single term, and one representing a 2 constructors o A constructor that takes two parameters: exponent and coefficient Recall that if we have, for example, the term 3x2 then 3 is the coefficient, and 2 is the exponent o A copy constructor This constructor will take, as its argument, a reference to the Polynomial which it is supposed to copy .A print method o Operations The method should print the polynomial o clone method -This method is inherited from Object You must override it to work correctly It copies and returns a new Polynomial object, which is a (deep) copy of the original that it is cloningExplanation / Answer
public class CrunchifyLinkedListTest {
public static CrunchifyLinkedList crunchifyList;
public static void main(String[] args) {
crunchifyList = new CrunchifyLinkedList();
crunchifyList.add("1");
crunchifyList.add("2");
crunchifyList.add("3");
crunchifyList.add("4");
crunchifyList.add("5");
System.out.println("Print: crunchifyList: " + crunchifyList);
System.out.println(".size(): " + crunchifyList.size());
System.out.println(".get(3): " + crunchifyList.get(3) + " (get element at index:3 - list starts from 0)");
System.out.println(".remove(2): " + crunchifyList.remove(2) + " (element removed)");
System.out.println(".get(3): " + crunchifyList.get(3) + " (get element at index:3 - list starts from 0)");
System.out.println(".size(): " + crunchifyList.size());
System.out.println("Print again: crunchifyList: " + crunchifyList);
}
}
class CrunchifyLinkedList {
private static int counter;
private Node head;
// Default constructor
public CrunchifyLinkedList() {
}
public void add(Object data) {
if (head == null) {
head = new Node(data);
}
Node crunchifyTemp = new Node(data);
Node crunchifyCurrent = head;
if (crunchifyCurrent != null) {
while (crunchifyCurrent.getNext() != null) {
crunchifyCurrent = crunchifyCurrent.getNext();
}
crunchifyCurrent.setNext(crunchifyTemp);
}
incrementCounter();
}
private static int getCounter() {
return counter;
}
private static void incrementCounter() {
counter++;
}
private void decrementCounter() {
counter--;
}
public void add(Object data, int index) {
Node crunchifyTemp = new Node(data);
Node crunchifyCurrent = head;
if (crunchifyCurrent != null) {
for (int i = 0; i < index && crunchifyCurrent.getNext() != null; i++) {
crunchifyCurrent = crunchifyCurrent.getNext();
}
}
crunchifyTemp.setNext(crunchifyCurrent.getNext());
crunchifyCurrent.setNext(crunchifyTemp);
incrementCounter();
}
public Object get(int index)
{
if (index < 0)
return null;
Node crunchifyCurrent = null;
if (head != null) {
crunchifyCurrent = head.getNext();
for (int i = 0; i < index; i++) {
if (crunchifyCurrent.getNext() == null)
return null;
crunchifyCurrent = crunchifyCurrent.getNext();
}
return crunchifyCurrent.getData();
}
return crunchifyCurrent;
}
public boolean remove(int index) {
if (index < 1 || index > size())
return false;
Node crunchifyCurrent = head;
if (head != null) {
for (int i = 0; i < index; i++) {
if (crunchifyCurrent.getNext() == null)
return false;
crunchifyCurrent = crunchifyCurrent.getNext();
}
crunchifyCurrent.setNext(crunchifyCurrent.getNext().getNext());
decrementCounter();
return true;
}
return false;
}
public int size() {
return getCounter();
}
public String toString() {
String output = "";
if (head != null) {
Node crunchifyCurrent = head.getNext();
while (crunchifyCurrent != null) {
output += "[" + crunchifyCurrent.getData().toString() + "]";
crunchifyCurrent = crunchifyCurrent.getNext();
}
}
return output;
}
private class Node {
Node next;
Object data;
public Node(Object dataValue) {
next = null;
data = dataValue;
}
@SuppressWarnings("unused")
public Node(Object dataValue, Node nextValue) {
next = nextValue;
data = dataValue;
}
public Object getData() {
return data;
}
@SuppressWarnings("unused")
public void setData(Object dataValue) {
data = dataValue;
}
public Node getNext() {
return next;
}
public void setNext(Node nextValue) {
next = nextValue;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.