Question

Class PNode { //JAVA COMPLETE TODO Marked answers // Basic node int deg; // The degree of a term ...

class PNode {

//JAVA COMPLETE TODO Marked answers

// Basic node

int deg; // The degree of a term

float coeff; // The coefficient of a term

PNode next;

PNode(int d, float c) { // Constructor: builds a node with given data

next = null;

deg = d;

coeff = c;

}

PNode(int d, float c, PNode n) { // Constructor: builds a node with given reference

next = n;

deg = d;

coeff = c;

}

// Basic node operations

void scale(float k) { // scales coeff by k

coeff = coeff * k;

}

void multiplyByX(int d) { // increases the degree by d

deg = deg + d;

}

boolean simplified(PNode p) {

// PRE: p is the first node of a list of PNodes.

// POST: Returns true iff the nodes are sorted (descending) according to their

// degree field

// AND all nodes have distinct deg fields (no repeats)

// AND all nodes have a non-zero coefficient field.

PNode temp = p;

while (temp != null) {

if (temp.coeff == 0 || (temp.next != null && temp.next.deg > temp.deg))

return false;

temp = temp.next;

}

return true;

}

PNode simplify(PNode p) {

// PRE: p is the first node of a list of PNodes. All nodes in the list are

// already

// Sorted descending according to their deg field

// POST: Rearranges the list so that it is simplified but equivalent to the

// original representation,

// i.e. ensures that each node has distinct deg field (by using polynomial

// arithmetic)

// AND removes any nodes with coeff field set to 0.

// The simplified representation must be mathematically equivalent (as a

// polynomial)

// to the original representation.

// Returns the first node of the now rearranged list

while (p != null && p.coeff == 0)

p = p.next;

PNode temp = p;

while (temp != null) {

if ((temp.next != null && temp.next.coeff != 0 && temp.next.deg == temp.deg)) {

temp.coeff = temp.coeff + temp.next.coeff;

temp.next = temp.next.next;

} else if ((temp.next != null && temp.next.coeff == 0))

temp.next = temp.next.next;

temp = temp.next;

}

return p;

}

PNode priorityAdd(PNode p, int d, float c) {

// PRE: the given list p list is simplified (simplified returns TRUE);

// POST: Creates a new PNode with the given data (deg = d, coeff=c) and adds it

// to the

// current list so that the result is also simplified.

// Returns the first node of the list with the new addition

PNode newNode = new PNode(d, c);

if (p != null && p.deg <= newNode.deg) {

newNode.next = p;

p = newNode;

return simplify(p);

}

PNode temp = p;

while (temp != null) {

if (temp.next != null && (temp.next.deg <= newNode.deg)) {

newNode.next = temp.next;

temp.next = newNode;

break;

} else if (temp.next == null) {

temp.next = newNode;

break;

}

temp = temp.next;

}

return simplify(p);

}

}

class PolyList {// TODO

PNode first = null;

// Class invariant -- an instance of PolyList must always satisfy the condition:

// The list is empty OR

// (The list is not empty AND

// All nodes have distinct deg fields AND

// There are no nodes having coeff equal to 0 AND

// All nodes are ordered from first to last with decreasing deg field. )

PolyList(PNode p) { // Constructor that sets its first node to the given PNode p.

first = p;

}

int getDegree() { // TODO

// Returns the largest degree in the list representation.

return 0;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

, 1· パ »).ht, .Ja, d 1-pu PV 09 105 Scanned by CamScanner

Add a comment
Know the answer?
Add Answer to:
Class PNode { //JAVA COMPLETE TODO Marked answers // Basic node int deg; // The degree of a term ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

  • Java/LinkedList Need help with a few of the TODO parts, more info below in comments in...

    Java/LinkedList Need help with a few of the TODO parts, more info below in comments in bold. Thanks, package lab4; import java.util.IdentityHashMap; public class IntNode implements Cloneable {    private int data;    private IntNode next;       public IntNode(int d, IntNode n) {        data = d;        next = n;    }       public IntNode getNext() {        return next;    }          /// Override methods from Object       @Override   ...

  • Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test...

    Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • Java: Return an array of booleans in a directed graph. Please complete the TODO section in...

    Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...

  • Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all...

    Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...

  • Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

    Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Nod...

    Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Node getHeado return head public void setHead(Node head) [ head: this. head Method that creates a Node containing 'item' @param item Value to be added this.headnew Node(item, this.head) * and adds it as the first element in the list *I public void add(int item) Method that finds the node at the given index d replaces its value...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT