이 기사에서는 연결 목록에서 요소를 제거하는 방법을 이해할 것입니다.
java.util.LinkedList 클래스 작업은 이중 연결 목록에서 기대할 수 있는 작업을 수행합니다. 목록에 대한 색인을 생성하는 작업은 지정된 색인에 더 가까운 시작 또는 끝에서 목록을 순회합니다.
아래는 동일한 데모입니다 -
입력이 다음과 같다고 가정 -
The list is defined as: [Java, Scala, Python, JavaScript, C++]
원하는 출력은 -
The list after removing all the elements is: [Python, JavaScript, C++]
알고리즘
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Display the result Step 5 - Stop
예시 1
여기에서 모든 작업을 'main' 기능 아래에 묶습니다.
import java.util.LinkedList;
public class Demo {
public static void main(String args[]){
LinkedList<String> input_list = new LinkedList<String>();
input_list.add("Java");
input_list.add("Scala");
input_list.add("Python");
input_list.add("JavaScript");
input_list.add("C++");
System.out.println("The list is defined as: " + input_list);
input_list.remove();
input_list.remove();
System.out.println("The list after removing all the elements is: " + input_list);
}
} 출력
The list is defined as: [Java, Scala, Python, JavaScript, C++] The list after removing all the elements is: [Python, JavaScript, C++]
예시 2
여기에서 객체 지향 프로그래밍을 나타내는 함수로 작업을 캡슐화합니다.
import java.util.LinkedList;
public class Demo {
static void remove_element(LinkedList<String> input_list){
input_list.remove();
input_list.remove();
System.out.println("The list after removing all the elements is: " + input_list);
}
public static void main(String args[]){
LinkedList<String> input_list = new LinkedList<String>();
input_list.add("Java");
input_list.add("Scala");
input_list.add("Python");
input_list.add("JavaScript");
input_list.add("C++");
System.out.println("The list is defined as: " + input_list);
remove_element(input_list);
}
} 출력
The list is defined as: [Java, Scala, Python, JavaScript, C++] The list after removing all the elements is: [Python, JavaScript, C++]