所以我必须创建一个使用
的LinkedListstruct infoType
{
int ID;
int X;
double Y;
};
所以我有了这个,我设置了这个LinkedList来获取元素,但我不知道如何使用ID对列表进行排序,使用X搜索。
我认为我的教授想要的是它将需要一个元素Y,并且该元素被赋予一个ID和X.Y将是一个用RNG生成的随机数。它必须能够添加我通过AddFirst创建的元素,删除,通过removeFirst添加。然后打印他的列表,它将使用getList方法添加。
import java.util.ListIterator;
import java.util.NoSuchElementException;
public class MyLinkedList {
private Node first;
private int ID;
private int X;
private double Y;
private int Size;
public MyLinkedList()
{
first = null;
ID = 0;
X = 0;
Y = 0;
}
public Object getFirst()
{
if (first == null)
throw new NoSuchElementException();
return first.data;
}
public Object removeFirst()
{
if (first == null)
throw new NoSuchElementException();
Object element = first.data;
first = first.next;
return element;
}
public void addFirst(Object element)
{
Node newNode = new Node();
newNode.data = element;
newNode.next = first;
first = newNode;
}
public ListIterator listIterator()
{
return new LinkedListIterator();
}
class Node
{
public Object data;
public Node next;
}
class LinkedListIterator implements ListIterator
{
private Node position;
private Node previous;
public LinkedListIterator()
{
position = null;
previous = null;
}
public Object next()
{
if (!hasNext())
throw new NoSuchElementException();
previous = position;
if (position == null)
position = first;
else
position = position.next;
return position.data;
}
public boolean hasNext()
{
if (position == null)
return first != null;
else
return position.next != null;
}
public void add (Object element)
{
if (position == null)
{
addFirst(element);
position = first;
}
else
{
Node newNode = new Node();
newNode.data = element;
newNode.next = position.next;
position.next = newNode;
position = newNode;
}
previous = position;
}
public void remove ()
{
if (previous == position)
throw new IllegalStateException();
if (position == first)
{
removeFirst();
}
else
{
previous.next = position.next;
}
position = previous;
}
public void set (Object element)
{
if (position == null)
throw new NoSuchElementException();
position.data = element;
}
@Override
public boolean hasPrevious() {
// TODO Auto-generated method stub
return false;
}
@Override
public int nextIndex() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object previous() {
// TODO Auto-generated method stub
return null;
}
@Override
public int previousIndex() {
// TODO Auto-generated method stub
return 0;
}
}
public int size() {
// TODO Auto-generated method stub
return Size;
}
}
这是MyLinkedListTester
public class MyLinkedListTester {
public static final void main(String[] args) {
MyLinkedList list = new MyLinkedList();
System.out.println("Number of items in the list: " + list.size());
int a = 2;
int b = 5;
double c = 7;
list.addFirst(a,b,c);
System.out.println("Number of items in the list: " + list.size());
System.out.println(list);
}
}
任何帮助都会受到赞赏,因为此时我在圈子里奔跑,直到明天我才能看到我的教授,我想今天取得更多进展。谢谢