JAVA数据结构之链表

今天我们来写链表
首先定义一个结点类
 

public class Node{
private Node next;
private int data;
public Node(int value){
this.data=value;}
public void display(){
System.out.println(data+" ");}
}

接着便是编写链表

public class lianbiao{
private Node first;//头结点
public void lianbiao(){
first=null;}//构造函数,初始化链表
public void insert(int value){
Node node=new Node(value);
node.next=first;//设置结点的next为null
first=node;//新结点赋值给first
}//插入结点

删除第一个结点

public Node deletefirst(){
Node tmp=first;
first=tmp.next;
return tmp;}

 

显示所有结点

public void display(){
Node node=first;
while(node!=null){
node.display();
node=node.next;结点往后移}

删除结点

public void delete(int value){
Node current=first;//保存当前结点
Node previous=first;//保存上一个结点
while(current.data!=value){
if(current.next==null){
return null;}
不等于null,结点往后移
previous=current;
current=current.next;
if(current.data==first){
first=first.next;}等于头结点时,头结点往后移
esle{previous.next=current.next;
return next;}
}

查找结点

public Node find(int value){
Node node=first;
if(node.data!=value){
if(node.next==null){
return null;
}node=node.next;
return node;}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值