python中如何实现指针_Java,Python中没有指针,怎么实现链表,图等数据结构

Java,Python中没有指针,怎么实现链表,图等数据结构

关注:276  答案:2  mip版

解决时间 2021-01-17 05:42

e6cb1a03ad541b3098697807b7bf1798.png

提问者这笑,有多危险

2021-01-16 08:06

Java,Python中没有指针,怎么实现链表,图等数据结构

最佳答案

e6cb1a03ad541b3098697807b7bf1798.png

二级知识专家笑尽沧桑

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();

}

}

全部回答

e6cb1a03ad541b3098697807b7bf1798.png

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();

}

}

我要举报

如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

→点此我要举报以上信息!←

推荐资讯

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表是一种线性数据结构,可以用来存储一系列元素。它由一系列节点组成,每个节点包含两部分:一个值和一个指向下一个节点的指针Python可以使用类来实现链表数据结构。具体实现方法如下: 1. 定义一个类表示节点,节点包含两个属性:value和next。 ```python class Node: def __init__(self, value): self.value = value self.next = None ``` 2. 定义一个链表类,链表包含一个头节点。 ```python class LinkedList: def __init__(self): self.head = None ``` 3. 实现链表的常用方法:添加节点、删除节点、查找节点等。 ```python class LinkedList: def __init__(self): self.head = None # 添加节点 def add(self, value): new_node = Node(value) if not self.head: self.head = new_node else: current_node = self.head while current_node.next: current_node = current_node.next current_node.next = new_node # 删除节点 def remove(self, value): current_node = self.head previous_node = None while current_node and current_node.value != value: previous_node = current_node current_node = current_node.next if current_node: if previous_node: previous_node.next = current_node.next else: self.head = current_node.next # 查找节点 def find(self, value): current_node = self.head while current_node and current_node.value != value: current_node = current_node.next return current_node ``` 4. 使用链表。 ```python # 创建链表 linked_list = LinkedList() # 添加节点 linked_list.add(1) linked_list.add(2) linked_list.add(3) # 删除节点 linked_list.remove(2) # 查找节点 node = linked_list.find(3) if node: print(node.value) else: print("Node not found.") ``` 链表是一种常用的数据结构,在Python可以使用类来实现链表的添加、删除和查找节点的操作都可以通过遍历链表来完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值