0
我想做一个链接列表,它将从用户接受输入他想要使链接列表不使用内置LinkedList函数的数量。但是如果用户输入一个负数,它会给用户输入一个正数的信息。我已经完成了用户输入的代码,但是发现其他部分的负面输入和连接数字非常困难。任何人都可以给我这些代码部分。链接列表用户输入
import java.io.*;
import java.util.Scanner;
class MyList{
public MyList firstLink,lastLink;
int info,size;
MyList link;
private MyList next;
MyList(){
this.link=null;
firstLink = null;
lastLink=null;
}
public boolean isEmpty(){
return(firstLink == null);
}
public void showMyList() {
MyList currentLink = firstLink;
System.out.print("List: ");
while(currentLink != null) {
currentLink.showMyList();
currentLink = currentLink.lastLink;
}
System.out.println("");
}
}
public class MyLinkedList {
public static void main(String[] args){
MyList newMyList=new MyList();
Scanner userInput= new Scanner(System.in);
int userInputNumber;
System.out.println("Enter Total Data");
userInputNumber = userInput.nextInt();
int i=1;
while(i<=userInputNumber){
System.out.println("Enter Data "+ i +":");
i++;
newMyList.info=userInput.nextInt();
}
if(newMyList.firstLink!=null){
newMyList=newMyList.firstLink;
newMyList=newMyList.lastLink;
newMyList.firstLink=newMyList.firstLink.link;
}
}
}
'if(userInputNumber <0)'?另外我可能会检查'LinkedList'的本地实现如何工作。你没有机会迭代它, – SomeJavaGuy
你可以给我这个代码并链接数字吗?这对我非常有帮助。 – Prem
您的类MyList不是实现链接列表结构。现在你有一份清单清单... –