에서 그들을 호출하는 이유, 나는 일반적으로 객체 지향 프로그래밍의 기본 개념에 꽤 확고한 이해를했다, 그러나 최근에 나는 나 자신이 뒤 떨어지는 것으로 나타났습니다 이해하기 쉽지 않은 개념 중 일부는문제 이해 생성자와 내가 대학에서 소프트웨어 공학을 공부하고 다른 클래스
한 가지 중요한 문제는 클래스 생성자 주위에 머리를 쓸 수 없다는 것입니다. 곧 내가 이것을 꽃 봉오리에 집어 넣지 않으면 내가 아는 어떤 것이 나의 몰락 일 것이다.
나는 그것을 설명하기 위해 내 교사를 요청했지만, 그냥 제가 일반적으로 수행 "아아 HAH"순간이 발생하지 않았다 그들이 그것을 설명하는 방식이어야합니다.
것은 당신이 날, 프로그램 작업 (연결리스트의 사용 및 조작을 보여주는) 아래의 예를 볼 수 있도록하는 데 도움 :
Main 클래스 :
package root;
public class Node<E> {
private E nodeValue;
private Node<E> next;
public static void main (String[] args) {
try {
// Example 1: Create an empty list and print it.
SinglyLinkedList<Integer> list1 = new SinglyLinkedList<Integer>();
System.out.println("Example 1: Create an empty list.");
System.out.println(list1.printList());
// ----------------------------------------------------------
// Example 2: Create a list of 1 integer (1) using InsertNodeToTail.
System.out.println("\nExample 2: Create a list of 1 integer using InsertNodeToTail.");
SinglyLinkedList<Integer> list2 = new SinglyLinkedList<Integer>();
System.out.println("Before: " + list2.printList());
list2.insertNodeToTail(1);
System.out.println("After: " + list2.printList());
// ----------------------------------------------------------
// Example 3: Create a list of 1 integer (1) using InsertNodeToHead.
System.out.println("\nExample 3: Create a list of 1 integer using InsertNodeToHead.");
SinglyLinkedList list3 = new SinglyLinkedList();
System.out.println("Before: " + list3.printList());
list3.insertNodeToHead(1);
System.out.println("After: " + list3.printList());
// ----------------------------------------------------------
// Example 4: Create a list of 5 integers (1, 3, 5, 7, and 9)
// using InsertNodeToTail. Output: 1->3->5->7->9
System.out.println("\nExample 4: Create list 1->3->5->7->9 using InsertNodeToTail.");
// Create an array of 5 integers
int[] array4 = { 1, 3, 5, 7, 9 };
// Create the head node
SinglyLinkedList<Integer> list4 = new SinglyLinkedList<Integer>();
System.out.println("Before: " + list4.printList());
// Insert nodes
for (int i = 0; i < array4.length; i++)
list4.insertNodeToTail(array4[i]);
System.out.println("After: " + list4.printList());
// ----------------------------------------------------------
// Example 5: Create a list of 5 integers (1, 3, 5, 7, and 9)
// using InsertNodeToHead. Output: 1->3->5->7->9
System.out.println("\nExample 5: Create list 1->3->5->7->9 using InsertNodeToHead.");
// Create an array of 5 integers
int[] array5 = { 1, 3, 5, 7, 9 };
// Create the head node
SinglyLinkedList<Integer> list5 = new SinglyLinkedList<Integer>();
System.out.println("Before: " + list5.printList());
// Insert nodes
for (int i = array5.length - 1; i >= 0; i--)
list5.insertNodeToHead(array5[i]);
System.out.println("After: " + list5.printList());
// ----------------------------------------------------------
// Example 6: Insert new node before a current node
System.out.println("\nExample 6: Insert node 0 before node 1.");
// Use list2, insert node 0 before node 1
System.out.println("Before: " + list2.printList());
list2.insertNodeBefore(0, 1);
System.out.println("After: " + list2.printList());
// ----------------------------------------------------------
// Example 7: Insert new node before a current node
System.out.println("\nExample 7: Insert node 4 before node 5.");
// Use list4, insert node 4 before node 5
System.out.println("Before: " + list4.printList());
list4.insertNodeBefore(4, 5);
System.out.println("After: " + list4.printList());
// ----------------------------------------------------------
// Example 8: Insert new node after a current node
System.out.println("\nExample 8: Insert node 2 after node 1.");
// Use list2, insert node 2 after node 1
System.out.println("Before: " + list2.printList());
list2.insertNodeAfter(2, 1);
System.out.println("After: " + list2.printList());
// ----------------------------------------------------------
// Example 9: Insert new node after a current node
System.out.println("\nExample 9: Insert node 10 after node 9.");
// Use list4, insert node 10 after node 9
System.out.println("Before: " + list4.printList());
list4.insertNodeAfter(10, 9);
System.out.println("After: " + list4.printList());
// ----------------------------------------------------------
// Example 10: Remove node if node value is given
System.out.println("\nExample 10: Remove node 10.");
// Use list4, remove node 10
System.out.println("Before: " + list4.printList());
list4.remove(10);
System.out.println("After: " + list4.printList());
// ----------------------------------------------------------
// Example 11: Remove node that is not in the list
System.out.println("\nExample 11: Remove node 100.");
// Use list4, remove node 100
System.out.println("Before: " + list4.printList());
list4.remove(100);
System.out.println("After: " + list4.printList());
} catch (Exception e) {
e.printStackTrace();
}
}
public Node() {
}
public Node(E nVal) {
nodeValue = nVal;
}
public Node(E nVal, Node<E> nextNode) {
nodeValue = nVal;
next = nextNode;
}
public E getNodeValue() {
return nodeValue;
}
public void setNodeValue (E nVal) {
nodeValue = nVal;
}
public Node<E> getNext() {
return next;
}
public void setNext (Node<E> n) {
next = n;
}
}
서브 클래스 :
package root;
import java.io.*;
public class SinglyLinkedList<E> {
private Node<E> head;
// Create an empty list
public SinglyLinkedList() {
head = null;
}
// Access to the entire linked list (read only)
public Node<E> getHead() {
return head;
}
// Insert a node with node value = nVal as the last node
public void insertNodeToTail(E nVal) {
Node<E> lastNode = new Node<E>();
lastNode.setNodeValue(nVal);
if (head == null) {
head = lastNode;
return;
}
Node<E> curr = head;
while (curr.getNext() != null) {
curr = curr.getNext();
}
curr.setNext(lastNode);
}
// Insert a node with node value = nval as the first node
public void insertNodeToHead(E nVal) {
Node<E> newHead = new Node<E>();
newHead.setNodeValue(nVal);
newHead.setNext(head);
head = newHead;
}
// Insert new node nVal to the list before current node curVal
public void insertNodeBefore(E nVal, E curVal) {
Node<E> newNode = new Node<E>(nVal);
Node<E> curr = head;
Node<E> prev = null;
if (head.getNodeValue() == curVal) {
newNode.setNext(head);
head = newNode;
return;
}
// scan until locate node or come to end of list
while (curr != null) {
// have a match
if (curr.getNodeValue() == curVal) {
// insert node
newNode.setNext(curr);
prev.setNext(newNode);
break;
} else {
// advanced curr and prev
prev = curr;
curr = curr.getNext();
}
}
}
// Insert new node nVal to the list after current node curVal
public void insertNodeAfter(E nVal, E curVal) {
Node<E> newNode = new Node<E>();
newNode.setNodeValue(nVal);
Node<E> curr = head.getNext();
Node<E> prev = head;
//scan until locate a node or come to the end of the list
while (prev != null) {
//have a match
if (prev.getNodeValue().equals(curVal)) {
//insert node
newNode.setNext(curr);
prev.setNext(newNode);
break;
} else {
//advance curr and prev
prev = curr;
curr = curr.getNext();
}
}
}
// Remove the node containing item nVal
public void remove(E nVal) throws IOException {
if (head == null) {
throw new IOException("List empty!");
} else {
Node<E> curr = head;
Node<E> prev = null;
// becomes true if we locate target
boolean foundItem = false;
// scan until locate nodeVal or come to end of list
while (curr != null && !foundItem) {
// have a match
if (curr.getNodeValue() == nVal) {
// if current node is the first node
// remove first node by moving head to next node
if (prev == null) {
head = head.getNext();
} else { // erase intermediate node
prev.setNext(curr.getNext());
}
foundItem = true;
} else {
// advanced curr and prev
prev = curr;
curr = curr.getNext();
}
}
}
}
public String printList() {
String outputList = "";
Node<E> temp = head;
if (temp == null) {
return "List empty!";
}
do {
// Print head node value
outputList += temp.getNodeValue().toString();
// Move to next node
temp = temp.getNext();
// if next node is not empty, print ->
// else print end of line then break the loop
if (temp != null) {
outputList += "->";
} else {
break;
}
} while (true);
// the loop terminates itself when it reaches to
// end of the list
return outputList;
}
}
는 사람이 설명 할 수 무엇 t의 생성자의 목적 그는 일반 Node<E>
(Main) 수업입니까? 그들은 어떤 상황에 처해야합니까? 새 노드를 만들려면 및 데이터가없는 경우
노드 클래스는 generic으로 정의되었으므로 Person/Student와 같이 String, Integer, User 정의 클래스를 사용할 수 있습니다. 이는 링크 된 목록에 요소를 추가하는 Main 클래스에서주의 깊게 보면 해당 특정 유형의 링크 된 목록을 만드는 데 더 사용됩니다. (기본적으로 해당 링크 된 목록에서 수행중인 모든 작업은 remove()/insert()/print()와 같으며이 값은 생성자에서 할당됩니다. –
용도는 insertNodeAfter (E val, E curVal) 방법. 첫 번째 줄에서 빈 생성자를 사용하여 새 노드를 만든 다음 노드 값을 설정합니다. 이것은 다른 생성자 중 하나를 사용하는 예제입니다. – Terje
여기 StackOverflow에서 게시 한 것과 같은 길고 비 기술적 인 소개는 권장되지 않습니다. 문제를 고집하고 명확한 질문을하고 필요한 배경 (코드, 결과, 예)을 제공하십시오. –