2017-12-01 6 views
0

사용자가 입력 할 사용자 입력 값보다 큰 모든 요소를 ​​링크 된 목록에서 삭제하려고합니다. 현재 다음과 같은 항목이 있습니다. code.Consider 나는 목록에서 이미 12,23,34,45,56.iow와 같은 5 가지 요소를 가지고 있습니다. 출력에 20을 입력하면 12로 출력하고 싶습니다. 링크 된 list.But의 마지막 요소를 삭제할 수 있습니다. 런타임에 사용자 입력 값보다 큰 모든 요소를 ​​삭제하려고합니다. 여기 런타임에 사용자 입력 값보다 큰 연결된 목록의 모든 요소를 ​​제거하십시오.

내가 tail.It가 됐었을 노력하고있다 내가 로직을 구현하는 방법을 잘 모릅니다 그 마지막에 요소를 제거 할 수 있어요 현재 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; 
    } 
}