이 기사에서는 연결 목록을 구현하는 방법을 이해할 것입니다. java.util.LinkedList 클래스 작업은 이중 연결 목록에서 기대할 수 있는 작업을 수행합니다. 목록에 대한 색인을 생성하는 작업은 지정된 색인에 더 가까운 시작 또는 끝에서 목록을 순회합니다.
아래는 동일한 데모입니다 -
입력이 다음과 같다고 가정 -
Run the program
원하는 출력은 -
The elements of the linked list are: 100 150 200 250
알고리즘
Step 1 - START Step 2 - Create a class with the required members. Step 3 - Define an ‘insert’ function to add elements to the list. Step 4 - In the ‘main’ method, create a new instance of the class. Step 5 - Create a list, and add elements to it using the ‘insert’ method. Step 6 - Iterate over the list, and display the value present in the current node. Step 7 - Move on to the next node and perform the same operation. Step 8 - Do this until the end of the list is reached. Step 9 - Display the result Step 10 - Stop
예시 1
여기에서 모든 작업을 'main' 기능 아래에 묶습니다.
public class Demo {
Node head;
static class Node {
int data;
Node next_element;
Node(int element){
data = element;
next_element = null;
}
}
public static Demo insert(Demo input_list, int data){
Node new_node = new Node(data);
new_node.next_element = null;
if (input_list.head == null) {
input_list.head = new_node;
}
else {
Node last = input_list.head;
while (last.next_element != null) {
last = last.next_element;
}
last.next_element = new_node;
}
return input_list;
}
public static void main(String[] args){
Demo input_list = new Demo();
System.out.print("A linked list is declared: \n");
input_list = insert(input_list, 100);
input_list = insert(input_list, 150);
input_list = insert(input_list, 200);
input_list = insert(input_list, 250);
Node current_node = input_list.head;
System.out.print("The elements of the linked list are: \n");
while (current_node != null) {
System.out.print(current_node.data + " ");
current_node = current_node.next_element;
}
}
} 출력
A linked list is declared: The elements of the linked list are: 100 150 200 250
예시 2
여기에서 객체 지향 프로그래밍을 나타내는 함수로 작업을 캡슐화합니다.
public class Demo {
Node head;
static class Node {
int data;
Node next_element;
Node(int element){
data = element;
next_element = null;
}
}
public static Demo insert(Demo input_list, int data){
Node new_node = new Node(data);
new_node.next_element = null;
if (input_list.head == null) {
input_list.head = new_node;
}
else {
Node last = input_list.head;
while (last.next_element != null) {
last = last.next_element;
}
last.next_element = new_node;
}
return input_list;
}
public static void print_list(Demo input_list){
Node current_node = input_list.head;
System.out.print("The elements of the linked list are: \n");
while (current_node != null) {
System.out.print(current_node.data + " ");
current_node = current_node.next_element;
}
}
public static void main(String[] args){
Demo input_list = new Demo();
System.out.print("A linked list is declared: \n");
input_list = insert(input_list, 100);
input_list = insert(input_list, 150);
input_list = insert(input_list, 200);
input_list = insert(input_list, 250);
print_list(input_list);
}
} 출력
A linked list is declared: The elements of the linked list are: 100 150 200 250