今天我们来写链表
首先定义一个结点类
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;}
}