그래프 (인접성 목록)를 저장하기 위해 클래스에 대한 링크 된 목록의 배열을 만들어야합니다. 자바를 사용해야합니다. 배열을 만들고 각 연결된 목록을 인스턴스화 할 수 있지만 각 요소에 첫 번째 요소를 추가 할 때 모든 연결된 목록이 배열의 인덱스에있는 것뿐만 아니라 변경됩니다. 이 각각의 새로운 연결리스트를 인스턴스화Java에서 연결된 목록의 배열
는Node [] adjList;
for(i=0;i<adjList.length;i++)
adjList[i] = new Node(0,0,null);
[Node
생성자 Node(int head, int data, Node next
로, 내 자신의 클래스)와 LinkedList
확장]
그때 내가 각 노드에 첫 번째 값을 추가로 이동합니다
for(i=0;i<adjList.length;i++)
adjList[i].setHead(i+1); // numbers 1 to end are the graph vertices
또는
for(i=0;i<adjList.length;i++)
adjList[i].add(new Node(i+1,0,null);
인쇄용 ements는 코드 을 디버깅하기 위해이 루프의 끝에서 각 연결된 목록을 인쇄하지만 각각에 대해 최종 값이됩니다.
즉. adjList.length = 2
경우, 그것은
[3,0,null] // adjList[0]
[3,0,null] // adjList[1]
[3,0,null] // adjList[2]
편집 출력 할 것이다 : 여기 노드 클래스
import java.util.LinkedList;
public class Node extends LinkedList{
private static int head;
private static int data;
private static Node next;
public Node(int h,int d,Node n) {
head = h;
data = d;
next = n;
}
public int getHead(){ // getNext() and getData() are the same
return head;
}
public void setHead(int h){ // setNext() and setData() are basically the same
head = h;
}
}
이 숙제입니까? 나는 우리가 돕기 위해'Node' 코드를 필요로한다고 생각합니다 ... –
'Node'의 정의를 게시하십시오. –
'Node' 클래스가'LinkedList'를 확장하는 이유는 무엇입니까? 거기에 is-a 관계가 없습니다. –