2017-12-01 3 views
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の誰かがこれで私を助けている間提供している入力値よりも大きいすべての要素を削除するための?

答えて

1

私はこれが役立つと思う:

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; 
    } 
} 
関連する問題