【发布时间】:2017-04-12 13:36:55
【问题描述】:
我正在尝试制作一个链表,该链表将从用户那里获取他想要制作链表的数量而不使用内置的 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的本机实现如何工作。你没有机会迭代它, -
你能给我这个代码并链接数字吗?这对我很有帮助。
-
您的 MyList 类不是链表结构的实现。现在您有了列表列表的列表...
-
这里我想制作用户给出的输入数字的链表。例如,一个用户想要制作一个包含 10 个正数的单链表。但如果他给出任何负数,它会警告用户输入正数。我无法在数字之间创建链接或制作链接列表。
-
你能给我正确的解决方案吗@VladimirParfenov