Java 删除linkedlist中运行时大于用户输入值的所有元素 我试图删除链接列表中的所有元素,这些元素大于用户输入的用户输入值。目前我有以下代码。考虑我已经在列表中有5个元素,如12、23、34、45、56。现在,如果我在输出中输入20,我希望输出为12。我能够删除链表的最后一个元素。但是我想要。在运行时删除大于用户输入值的所有元素
这里是LinkedList.javaJava 删除linkedlist中运行时大于用户输入值的所有元素 我试图删除链接列表中的所有元素,这些元素大于用户输入的用户输入值。目前我有以下代码。考虑我已经在列表中有5个元素,如12、23、34、45、56。现在,如果我在输出中输入20,我希望输出为12。我能够删除链表的最后一个元素。但是我想要。在运行时删除大于用户输入值的所有元素,java,linked-list,Java,Linked List,这里是LinkedList.java import java.io.*; class node { int data; node prev,next; public node(int x) { data=x; next=null; } } class SLL { node start=null; public int removeLast() { if (i
import java.io.*;
class node
{
int data;
node prev,next;
public node(int x)
{
data=x;
next=null;
}
}
class SLL
{
node start=null;
public int removeLast()
{
if (isEmpty())
{
System.out.println("empty");
return 0;
}
else
{
node current=start;
while (current.next.next!=null)
current=current.next;
int x=current.next.data;
current.next=null;
return x;
}
}
public int removeAllBasedOnInputValue(int val){
//I dont know how to implement code here//
}
public void display()
{
if (isEmpty())
System.out.println("The list is empty");
else
{
node current=start;
while (current!=null)
{
System.out.print(current.data+" ");
current=current.next;
}
}
}
public class Sl
{
public static void main(String[] args) throws IOException
{
InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader r=new BufferedReader(obj);
int ch;
SLL s=new SLL();
do
{
System.out.println("1.Remove");
System.out.println("2.Display");
System.out.println("3.Exit");
System.out.println("Enter your choice:");
ch=Integer.parseInt(r.readLine());
switch (ch)
{
case 1:
System.out.println("1.Remove tail");
System.out.println("2.Remove all elements based on specific value");
System.out.println("Enter choice:");
int al1=Integer.parseInt(r.readLine());
switch (al1)
{
case 1:
System.out.println("deleted: "+s.removeLast());
break;
case 2:
//System.out.println("deleted:"+s.removeAllBasedOnInputValue();
break;
}
break;
case 2:
s.display();
break;
case 3:
break;
}
}while(ch!=3);
}
}
目前,我能够在最后删除元素,即尾部。它工作正常。但我不知道如何实现逻辑来删除运行时提供的大于输入值的所有元素。有人能帮我吗?我认为这将有助于:
public int removeAllBasedOnInputValue(int val){
if (isEmpty())
{
System.out.println("empty");
return 0;
}else{
int counter=0;
node current=start;
//here we will go to the last node
while(current.next != null){
if(current.data > val){
/* Here, we need to verify 3 things:
* 1 - If it is the start;
* 2 - If it is the end; and
* 3 - If it is the body.
*/
/*1st verification -
If the start is bigger than your value,
then you just make your next node as "start",
and make its previous as NULL.*/
if(current == start)
{
start = current.next;
current.next.prev = null;
}/*2nd verification -
If it is the last element,
then you just make the next node of your previous be NULL.*/
else if(current.next == null)
{
current.prev.next = null;
}/*3rd verification -
You will make the next of the previous as your current next;
and the previous of the next as your current previous.
In that way you will lose all the ways of reaching the current
(which is greater than the value)*/
else
{
current.prev.next = current.next;
current.next.prev = current.prev;
}
counter++;
}
current.next = current.next;
}
return counter;
}
}