We have written the following Node class:
class Node {
// instance variables
private int value;
private Node next;
// constructor
public Node(int value) {
this.value = value;
}
// get the 'next' variable
public Node getNext() {
return this.next;
}
// get the 'value' variable
public int getValue() {
return this.value;
}
// set the 'next' variable
public void setNext(Node next) {
this.next = next;
}
}
TASK: Create a class called List that has the following properties:
EXAMPLE: If we run the following code:
Node node = new Node(1); node.setNext(new Node(2)); node.getNext().setNext(new Node(3)); List list = new List(node); System.out.println(list.sum());
We should print 1 + 2 + 3 = 6.
Sample Input:
1 2 3
Sample Output:
6
Explanation::
Code in JAVA::
List.java Code:
public class List {
private Node head;
public List(Node newNode) {
this.head = newNode;
}
public int sum() {
/**
* An integer variable
named total is declared and initialized to 0
* We will store all the node values
into it as summation and at the end return it
* */
int total = 0;
/**
* We iterate through nodes using
while loop until current head is not null
* */
while(this.head!=null) {
total = total +
this.head.getValue();
this.head =
this.head.getNext();
}
return total;
}
}
Test.java Code:
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method
stub
Node node = new Node(1);
node.setNext(new Node(2));
node.getNext().setNext(new
Node(3));
List list = new List(node);
System.out.println(list.sum());
}
}
NOTE : Node.java code is present in question itself
:)
OUTPUT:
6
Please provide the feedback!!
Thank You!!
We have written the following Node class: class Node { // instance variables private int value;...
Need help completing my instance method for deleteFront() as specified below To the class IntegerLinkedList, add an instance method deleteFront such that … Method deleteFront has no input. Method deleteFront returns … an empty Optional instance if the list is empty an Optional instance whose value is the integer that was deleted from the front of the list, otherwise Hints https://docs.oracle.com/javase/10/docs/api/java/util/Optional.html import java.util.Optional; public class IntegerLinkedList { private IntegerNode head ; private int numberOfItems ; public int getNumberOfItems() { return...
C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...
public class myLinkedList<E extends Comparable<E>>{ private Node<E> head = null; private Node<E> tail = null; public myLinkedList() { head = null; } public void concatenateList (myLinkedList<E> M) {//attach another linkedList refered by M to the end of this linkedList } public int searchElement (E targetElement){//test if a target element can be found on the list, return the occurrences of the target element ...
We have written the following Class1 class: class Class1 implements SecretInterface { // instance variable private int x = 0; // constructor public Class1(int x) { this.x = x; } // instance methods public double myMethod1(double x, double y) { return x - y; } public void myMethod2(int x) { System.out.println(x); } public String myMethod3(String x) { return "hi " + x; } } We have also written the following Class2 class: class Class2 implements SecretInterface { // instance variable...
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);...
Create a complete LinkedList class which implements all of the methods listed below using dynamic memory allocation. You will need a Node class to represent each node in the list. The list should store String values. Include a main method which tests all the methods of your list. Also answer these questions: What is the time complexity (using big-O notation) of these operations in your linked list? add(String val) add(int index, String val) get(int index) remove(String val) Submit three files:...
public class LinkedList { // The LinkedList Node class private class Node{ int data; Node next; Node(int gdata) { this.data = gdata; this.next = null; } } // The LinkedList fields Node head; // Constructor LinkedList(int gdata) { this.head = new Node(gdata); }...
How do i write a destructor for this class? #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node{ public: // Constructor // Preconditions: None // Postconditions: None Node(); // Destructor // Preconditions: None // Postconditions: Frees dynamically allocated memory ~Node(); // Overloaded Constructor // Preconditions: None // Postconditions: Initializes member variable with given argument Node(bool value); // ReplaceValue - Replaces m_value with opposite (if true then false or if false then true) // Preconditions: None // Postconditions: m_value...
Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int. There is also an attribute of the class, points, which does not have a corresponding instance variable. Also note the constructor and methods of the class and what they do. TournamentAdmin class code: public class RankAdmin { /** * Constructor for objects of class...
// 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; }...