0
ユーザーが入力するユーザー入力値より大きいすべての要素をリンクリストから削除しようとしています。現在、次のものを持っていますcode.Consider私はリストにすでに12,23,34,45,56のような5つの要素を持っています。出力に20を入力すると、出力は12になります。リンクされたlist.Butの最後の要素を削除できます。実行時にユーザー入力値より大きいすべての要素を削除したいここで実行時にユーザー入力値より大きいリンクリスト内のすべての要素を削除します
は私がロジックを実装する方法を知らないtail.Itがfine.But働いているされている最後の要素を削除することができています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 (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);
}
}
です私はruntime.Canの誰かがこれで私を助けている間提供している入力値よりも大きいすべての要素を削除するための?