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);...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • 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 =...

  • In c, please implement the following 3 functions to the code below is_reachable(graph_t * g, int...

    In c, please implement the following 3 functions to the code below is_reachable(graph_t * g, int source, int dest) returns 0 if I can reach the destination from source, -1 otherwise ( using BFS) has_cycle(graph_t * g) returns 0 if there is a cycle in the graph, -1 otherwise (using BFS or DFS) print_path(graph_t * g, int source, int dest) prints any path from source to destination if there exists one (Choose either BFS or DFS, typically DFS is much...

  • Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(),...

    Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 {    private Node head, tail; private int size;    public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...

  • 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   ...

  • 5. Given the following definition of class Node: class Node { public int item; public Node...

    5. Given the following definition of class Node: class Node { public int item; public Node next; public Node ( int i, Node n ) { item = i; next = n; }; } and the following declarations: Node list, p, q; Assume the structure starts as below. Draw the structure created by executing the following statements. Each part is independent. list 1 2 3 4 5   [2] a) p = list; q = null; while ( p != null...

  • 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...

  • // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node();...

    // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node(); Node(int entry); int getEntry() const; void setEntry(int entry); Node *getNext(); void setNext(Node *next); }; #endif // Node.cpp #include "Node.h" Node::Node() { m_entry = 0; m_next = nullptr; } Node::Node(int entry) { m_entry = entry; } int Node::getEntry() const { return m_entry; } void Node::setEntry(int entry) { m_entry = entry; } Node *Node::getNext() { return m_next; } void Node::setNext(Node *next) { m_next = next; }...

  • Sometimes it is convenient to maintain references to both the next node and the previous node...

    Sometimes it is convenient to maintain references to both the next node and the previous node in a linked list. This is called a doubly linked list and is illustrated in Figure 13.4 of the text. File DoubleLinked contains definitions for a doubly linked list of integers. This class contains an inner class IntNode that holds information for a single node in the list (its value and references to the next and previous nodes). The DoubleLinked class also contains the...

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