【发布时间】:2021-09-15 01:56:42
【问题描述】:
这是一个给定的活动,我们应该在其中创建一个插入节点的方法,并使用扫描仪进行输入。到目前为止,我可以从列表中输入 3 个对象,但是当我尝试添加另一个对象时出现了问题:
它是这样的:1,2,3,当我尝试添加另一个时,它会转到这个 1,2,4 但我想要的是这个 1,2,3,4。
非常感谢您提前提供的帮助。
这里是主要方法:
import java.util.Scanner;
public class MySinglyLinkedCircularListMain {
public static void main(String[] args) throws ListOverflowException {
Scanner keyboard = new Scanner(System.in);
MySinglyLinkedCircularList<Node> singlyLinkedCircularList = new MySinglyLinkedCircularList<>();
while (true) {
System.out.println("+---------------------------------------------------+");
System.out.println("| Select the Number to be executed: |\n" +
"| 1) Insert an element |\n" +
"| 2) Delete an element from the list |\n" +
"| 3) Get an element from the list |\n" +
"| 4) Search an element in the list |\n" +
"| 5) Number of elements in the list |\n" +
"| 6) Show the elements in the list |");
System.out.print("+---------------------------------------------------+ \n");
System.out.print("Input your choice: ");
int intInput = keyboard.nextInt();
if (intInput == 1) {
singlyLinkedCircularList.insert(new Node(singlyLinkedCircularList));
} else if (intInput == 2) {
singlyLinkedCircularList.delete(new Node(singlyLinkedCircularList));
} else if (intInput == 3) {
singlyLinkedCircularList.getElement(new Node(singlyLinkedCircularList));
} else if (intInput == 4) {
singlyLinkedCircularList.search(new Node(singlyLinkedCircularList));
} else if (intInput == 5){
System.out.println("The current capacity of the single circular linked list is "
+ singlyLinkedCircularList.getSize());
} else if (intInput == 6) {
singlyLinkedCircularList.showAllElements();
System.out.println();
}
}
}
}
这里是节点类
public class Node<T> {
T data;
Node<T> next;
public Node(T data) {
this.data = data;
next = null;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public void setNext(Node<T> node) {
next = node;
}
public Node<T> getNext() {
return next;
}
}
这是插入方法的类
import java.util.NoSuchElementException;
import java.util.Scanner;
public class MySinglyLinkedCircularList<E> implements MyList<E> {
Scanner keyboard = new Scanner(System.in);
int size;
Node<E> startNode;
Node<E> endNode;
public MySinglyLinkedCircularList() {
size = 0;
startNode = endNode = null;
}
public int getSize() {
return size;
}
public void insert(E data) throws ListOverflowException {
System.out.print("Input the element you want: ");
data = (E) keyboard.next();
Node<E> newNode = new Node(data);
if (startNode == null) {
startNode = endNode = newNode;
startNode.next = startNode;
size++;
System.out.println("Element " + startNode.getData() + " has been stored in position "
+ getSize() + " and is now referenced itself");
} else {
Node<E> addNode = endNode;
for (int i = 0; i < getSize(); i++) {
addNode = addNode.getNext();
}
addNode.next = newNode;
newNode.next = startNode;
size++;
}
}
这里是显示元素方法
public void showAllElements() {
Node<E> showNode = startNode;
int i = 0;
System.out.print("Here are the current elements: ");
while (i<getSize()) {
System.out.print(showNode.getData() + " ");
showNode = showNode.getNext();
i++;
}
System.out.print(showNode.getData());
}
界面如下
public interface MyList<E> {
public int getSize();
public void insert(E data) throws ListOverflowException;
public E getElement(E data) throws NoSuchElementException;
public boolean delete(E data); // returns false if the data is not deleted in the list
public boolean search(E data);
public void showAllElements();
}
【问题讨论】:
标签: java java.util.scanner circular-list