Java,Python中没有指针,怎么实现链表,图等数据结构
2个回答
展开全部
package com.list;
public class Node<T extends Number> {
private Node next;
private T data;
public Node(){
this(null, null);
}
public Node(Node next, T data){
this.next = next;
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "Node " + ", data=" + data;
}
}
package com.list;
public class List <T extends Number> {
private Node<T> head;
private Node<T> tail;
private Node<T> current;
public void createList(T[] data){
//List<T> list = new List<T>();
for (int i = 0; i < data.length; i++){
Node<T> temp = new Node<T>(null, data[i]);
if (0 == i){
head = temp;
current = temp;
continue;
}
tail = temp;
current.setNext(temp);
current = temp;
}
//return list;
}
public List(){
head = null;
tail = null;
}
public Node<T> getHead() {
return head;
}
public void setHead(Node<T> head) {
this.head = head;
}
public Node<T> getTail() {
return tail;
}
public void setTail(Node<T> tail) {
this.tail = tail;
}
private void print(){
current = head;
while (current != null){
System.out.println(current.toString());
current = current.getNext();
}
}
public static void main(String[] args) {
Integer[] data = new Integer[5];
for (int i = 0; i< data.length; i++){
data[i] = i + 10;
}
List<Integer> temp = new List<Integer>();
temp.createList(data);
temp.print();
}
}
2015-07-19
展开全部
用类了,保存引用对象
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询