Java,Python中没有指针,怎么实现链表,图等数据结构
关注:276 答案:2 mip版
解决时间 2021-01-17 05:42
提问者这笑,有多危险
2021-01-16 08:06
Java,Python中没有指针,怎么实现链表,图等数据结构
最佳答案
二级知识专家笑尽沧桑
2021-01-16 08:20
package com.list;
public class Node {
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 {
private Node head;
private Node tail;
private Node current;
public void createList(T[] data){
//List list = new List();
for (int i = 0; i
Node temp = new Node(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 getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public Node getTail() {
return tail;
}
public void setTail(Node 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[i] = i + 10;
}
List temp = new List();
temp.createList(data);
temp.print();
}
}
全部回答
1楼假装安慰
2021-01-16 09:20
package com.list;
public class node{
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 {
private nodehead;
private nodetail;
private nodecurrent;
public void createlist(t[] data){
//listlist = new list();
for (int i = 0; i < data.length; i++){
nodetemp = new node(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 nodegethead() {
return head;
}
public void sethead(nodehead) {
this.head = head;
}
public nodegettail() {
return tail;
}
public void settail(nodetail) {
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;
}
listtemp = new list();
temp.createlist(data);
temp.print();
}
}
我要举报
如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
→点此我要举报以上信息!←
推荐资讯