import edu.princeton.cs.algs4.StdOut;   public class LinkedIntListR {   // helper linked list node class private class Node { private int item; private Node next;   public Node(int item, Node next) { this.item = item; this.next = next; } }   private Node first; // first node of the list   public LinkedIntListR() { first = null; }   public LinkedIntListR(int[] data) { for (int i = data.length - 1; i >= 0; i--) first = new Node(data[i], first); }     public void addFirst(int data) { first = new Node(data, first); }   public void printList() { if (first == null) { StdOut.println("[]"); } else if (first.next == null) { StdOut.println("[" + first.item + "]"); } else { StdOut.print("[" + first.item); for(Node ptr = first.next; ptr != null; ptr = ptr.next) StdOut.print(", " + ptr.item); StdOut.println("]"); } }   @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; LinkedIntListR other = (LinkedIntListR) obj; Node thisPtr = first; Node otherPtr = other.first; while(thisPtr != null && otherPtr != null) { if (thisPtr.item != otherPtr.item) return false; thisPtr = thisPtr.next; otherPtr = otherPtr.next; } if (thisPtr != null || otherPtr != null) return false; return true; }       /********************************** HOMEWORK **********************************/   /** * Computes the sum of all the ints in the list. *  * @return the sum of all the ints in the list */ public int sum() { // TODO throw new RuntimeException("Not implemented"); }     /** * Returns the index of the first occurrence of the specified int in this list, * or -1 if this list does not contain the int. * * @param i int to search for * @return the index of the first occurrence of the specified int in this list, *         or -1 if this list does not contain the int */ public int indexOf(int i) { // TODO throw new RuntimeException("Not implemented"); // HINT:The empty list is not the only base case. //If the first item in the list is the one you are looking for //you should also stop because you have found it. }   /** * Constructs a string representing the list. The ints in the list appear in a * comma separated list contained inside square brackets. *  * @return a string represeting the list. */ public String toString() { // TODO throw new RuntimeException("Not implemented"); // HINT:Have the private method just return comma separted list //and have the public method put the [ and ] around the private //method's answer. //Also, you probably need a base case for a list of size 1 as well //The empty list can be handled in the public method if you want. }     /** * Constructs a new {@code LinkedIntList} consisting of just the even number * from this list appearing in the same order as they occur in this list. *  * @return a new list that looks like this list be with all the odds removed. */ public LinkedIntListR evens() { // TODO throw new RuntimeException("Not implemented"); // HINT:Have the private method return a list via the first node. //Have the public method create an empty list and then set //first in that empty list to the list returned by the helper }   /** * Constructs a new int list where each entry in the new list is the sum of the * corresponding entries in this list and the argument list. *  * @arg otherList the other list to be added to this list * @return a new list where each entry in the new list is the sum of the *         corresponding entries in this list and the argument list * @throws IllegalArgumentException if this list and the other list have have *                                  different lengths */ public LinkedIntListR listAddition(LinkedIntListR otherList) { // TODO throw new RuntimeException("Not implemented"); // HINT:Have the private method return a list via the first node. //Have the public method create an empty list and then set //first in that empty list to the list returned by the helper  }   }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

import edu.princeton.cs.algs4.StdOut;

 

public class LinkedIntListR {

 

// helper linked list node class

private class Node {

private int item;

private Node next;

 

public Node(int item, Node next) {

this.item = item;

this.next = next;

}

}

 

private Node first; // first node of the list

 

public LinkedIntListR() {

first = null;

}

 

public LinkedIntListR(int[] data) {

for (int i = data.length - 1; i >= 0; i--)

first = new Node(data[i], first);

}

 

 

public void addFirst(int data) {

first = new Node(data, first);

}

 

public void printList() {

if (first == null) {

StdOut.println("[]");

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

StdOut.println("[" + first.item + "]");

} else {

StdOut.print("[" + first.item);

for(Node ptr = first.next; ptr != null; ptr = ptr.next)

StdOut.print(", " + ptr.item);

StdOut.println("]");

}

}

 

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

LinkedIntListR other = (LinkedIntListR) obj;

Node thisPtr = first;

Node otherPtr = other.first;

while(thisPtr != null && otherPtr != null) {

if (thisPtr.item != otherPtr.item)

return false;

thisPtr = thisPtr.next;

otherPtr = otherPtr.next;

}

if (thisPtr != null || otherPtr != null)

return false;

return true;

}

 

 

 

/********************************** HOMEWORK **********************************/

 

/**

* Computes the sum of all the ints in the list.

* @return the sum of all the ints in the list

*/

public int sum() { // TODO

throw new RuntimeException("Not implemented");

}

 

 

/**

* Returns the index of the first occurrence of the specified int in this list,

* or -1 if this list does not contain the int.

*

* @param i int to search for

* @return the index of the first occurrence of the specified int in this list,

*         or -1 if this list does not contain the int

*/

public int indexOf(int i) { // TODO

throw new RuntimeException("Not implemented");

// HINT:The empty list is not the only base case.

//If the first item in the list is the one you are looking for

//you should also stop because you have found it.

}

 

/**

* Constructs a string representing the list. The ints in the list appear in a

* comma separated list contained inside square brackets.

* @return a string represeting the list.

*/

public String toString() { // TODO

throw new RuntimeException("Not implemented");

// HINT:Have the private method just return comma separted list

//and have the public method put the [ and ] around the private

//method's answer.

//Also, you probably need a base case for a list of size 1 as well

//The empty list can be handled in the public method if you want.

}

 

 

/**

* Constructs a new {@code LinkedIntList} consisting of just the even number

* from this list appearing in the same order as they occur in this list.

* @return a new list that looks like this list be with all the odds removed.

*/

public LinkedIntListR evens() { // TODO

throw new RuntimeException("Not implemented");

// HINT:Have the private method return a list via the first node.

//Have the public method create an empty list and then set

//first in that empty list to the list returned by the helper

}

 

/**

* Constructs a new int list where each entry in the new list is the sum of the

* corresponding entries in this list and the argument list.

* @arg otherList the other list to be added to this list

* @return a new list where each entry in the new list is the sum of the

*         corresponding entries in this list and the argument list

* @throws IllegalArgumentException if this list and the other list have have

*                                  different lengths

*/

public LinkedIntListR listAddition(LinkedIntListR otherList) { // TODO

throw new RuntimeException("Not implemented");

// HINT:Have the private method return a list via the first node.

//Have the public method create an empty list and then set

//first in that empty list to the list returned by the helper 

}

 

}

thisPtr = thisPtr.next;
otherPtr = otherPtr. next;
}
if (thisPtr != null || otherPtr != null)
return false;
return true;
}
/*******
****** HOMEWORK ********
*********/
/**
* Computes the sum of all the ints in the list.
* @return the sum of all the ints in the list
*/
public int sum() { // TODO
throw new RuntimeException ("Not implemented");
}
/**
* Returns the index of the first occurrence of the specified int in this list,
* or -1 if this list does not contain the int.
*
* @param i int to search for
* @return the index of the first occurrence of the specified int in this list,
or -1 if this list does not contain the int
*/
public int index0f (int i) { // TODO
throw new RuntimeException("Not implemented");
// HINT:
//
//
}
The empty list is not the only base case.
If the first item in the list is the one you are looking for
you should also stop because you have found it.
/**
* Constructs a string representing the list. The ints in the list appear in a
* comma separated list contained inside square brackets.
* @return a string represeting the list.
*/
public String toString() { // TODO
throw new RuntimeException("Not implemented");
// HINT:
//
Have the private method just return comma separted list
and have the public method put the [ and ] around the private
method's answer.
Also, you probably need a base case for a list of size 1 as well
The empty list can be handled in the public method if you want.
}
/**
* Constructs a new {@code LinkedIntList} consisting of just the even number
* from this list appearing in the same order as they occur in this list.
* @return a new list that looks like this list be with all the odds removed.
*/
Transcribed Image Text:thisPtr = thisPtr.next; otherPtr = otherPtr. next; } if (thisPtr != null || otherPtr != null) return false; return true; } /******* ****** HOMEWORK ******** *********/ /** * Computes the sum of all the ints in the list. * @return the sum of all the ints in the list */ public int sum() { // TODO throw new RuntimeException ("Not implemented"); } /** * Returns the index of the first occurrence of the specified int in this list, * or -1 if this list does not contain the int. * * @param i int to search for * @return the index of the first occurrence of the specified int in this list, or -1 if this list does not contain the int */ public int index0f (int i) { // TODO throw new RuntimeException("Not implemented"); // HINT: // // } The empty list is not the only base case. If the first item in the list is the one you are looking for you should also stop because you have found it. /** * Constructs a string representing the list. The ints in the list appear in a * comma separated list contained inside square brackets. * @return a string represeting the list. */ public String toString() { // TODO throw new RuntimeException("Not implemented"); // HINT: // Have the private method just return comma separted list and have the public method put the [ and ] around the private method's answer. Also, you probably need a base case for a list of size 1 as well The empty list can be handled in the public method if you want. } /** * Constructs a new {@code LinkedIntList} consisting of just the even number * from this list appearing in the same order as they occur in this list. * @return a new list that looks like this list be with all the odds removed. */
public LinkedIntListR evens () { // TODO
throw new RuntimeException("Not implemented");
// HINT:
//
//
}
Have the private method return a list via the first node.
Have the public method create an empty list and then set
first in that empty list to the list returned by the helper
/**
* Constructs a new int list where each entry in the new list is the sum of the
* corresponding entries in this list and the argument list.
*
* carg otherList the other list to be added to this list
* @return a new list where each entry in the new list is the sum of the
corresponding entries in this list and the argument list
* @throws IllegalArgumentException if this list and the other list have have
*
different lengths
*/
public LinkedIntListR listAddition(LinkedIntListR otherList) { // TODO
throw new RuntimeException ("Not implemented");
// HINT:
//
//
}
Have the private method return a list via the first node.
Have the public method create an empty list and then set
first in that empty list to the list returned by the helper
Transcribed Image Text:public LinkedIntListR evens () { // TODO throw new RuntimeException("Not implemented"); // HINT: // // } Have the private method return a list via the first node. Have the public method create an empty list and then set first in that empty list to the list returned by the helper /** * Constructs a new int list where each entry in the new list is the sum of the * corresponding entries in this list and the argument list. * * carg otherList the other list to be added to this list * @return a new list where each entry in the new list is the sum of the corresponding entries in this list and the argument list * @throws IllegalArgumentException if this list and the other list have have * different lengths */ public LinkedIntListR listAddition(LinkedIntListR otherList) { // TODO throw new RuntimeException ("Not implemented"); // HINT: // // } Have the private method return a list via the first node. Have the public method create an empty list and then set first in that empty list to the list returned by the helper
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY