I am working on a project for my Data Structures class that asks me to write a class to implement a linked list of ints. Use an inner class for the Node. Include the methods below. Write a tester to enable you to test all of the methods with whatever data you want in any order. I have to create three different constructors. One of the constructors is meant to be a constructor that creates a linked list of N random integers between low and high. I've written methods that do what the constructor is asking for and therefore wrote the code I would have in a method as a constructor. However, I don't think I'm right. Can someone let me know If I'm doing the right thing? If not tell me what I need to do?
import java.util.Random;
public class LinkedListOfInts {
Node head;
private class Node {
int value;
Node nextNode;
public Node(int value, Node nextNode) {
this.value = value;
this.nextNode = nextNode;
}
}
public LinkedListOfInts() {
}
public LinkedListOfInts(int N, int low, int high) {
Random random = new Random();
int[] result = new int[N];
for (int i = 0; i < N; i++)
result[i] = random.nextInt(high - low) + low;
}
public void addToFront(int x) {
head = new Node(x, head);
}
public String toString() {
String result = " ";
for (Node ptr = head; ptr != null; ptr = ptr.nextNode)
result += ptr.value + " ";
return result;
}
public static void main(String[] args) {
LinkedListOfInts list = new LinkedListOfInts();
for (int i = 0; i < 15; i++)
list.addToFront(i);
System.out.println(list);
}
}
Aucun commentaire:
Enregistrer un commentaire