定义结点元素:
class Node{
public int id ; //结点编号
public int val; //结点数据
public Node Next; //指向下一节点
//构造器
public Node(int val,int id) {
this.val = val;
this.id = id;
}
@Override
public String toString() {
return "Node{"+
" id=" + id +
" val=" + val +
'}';
}
}
尾插法:
//添加结点(尾插)---形参表示要添加的结点
public void add_tail(Node tmp){
//辅助指针
Node node = head;
//遍历链表 找到尾结点
while(node.Next != null){
node = node.Next;
}
node.Next = tmp; //尾结点指向新添加的结点
length ++; //添加成功,链表长度加 1
}
尾删法:
//删除结点(尾删)
public void del_tail(){
Node delete = head.Next; //保存删除结点位置
Node node = head; //保存删除结点的前一个位置
//判断链表是否为空,若为空直接退出
if(delete == null){
System.out.println("链表为空,无法删除");
return;
}
//遍历链表 找到尾结点(即删除的结点)
while(delete.Next != null){
delete = delete.Next;
node = node.Next;
}
System.out.println("删除结点数据为: "+ delete); //输出删除结点的元素(会调用该结点的tostring 方法)
node.Next = null; //将删除结点的指向赋值为空
length --; //删除成功,链表长度减 1
}
显示链表数据:
public void show(){
//判断链表是否为空
if(head.Next == null){
System.out.println("链表为空");
return;
}
Node node = head.Next;
while(node != null){
System.out.println("结点数据为: "+ node);
node = node.Next;
}
}
完整程序:
class SingleList{
public int length = 0 ; //链表长度
//初始化头结点
private Node head = new Node(0,0);
//添加结点(尾插)
public void add_tail(Node tmp){
//辅助指针
Node node = head;
//遍历链表 找到尾结点
while(node.Next != null){
node = node.Next;
}
node.Next = tmp;
length ++;
}
//删除结点(尾删)
public void del_tail(){
Node delete = head.Next; //保存删除结点位置
Node node = head; //保存删除结点的前一个位置
if(delete == null){
System.out.println("链表为空,无法删除");
return;
}
//遍历链表 找到尾结点(即删除的结点)
while(delete.Next != null){
delete = delete.Next;
node = node.Next;
}
System.out.println("删除结点数据为: "+ delete);
node.Next = null;
length --;
}
public void show(){
//判断链表是否为空
if(head.Next == null){
System.out.println("链表为空");
return;
}
Node node = head.Next;
while(node != null){
System.out.println("结点数据为: "+ node);
node = node.Next;
}
}
}
public class 单链表 {
public static void main(String[] args) {
Node node1 = new Node(5,1);
Node node2 = new Node(6,2);
Node node3 = new Node(7,3);
SingleList singleList = new SingleList();
singleList.add_tail(node1);
singleList.add_tail(node2);
singleList.add_tail(node3);
singleList.show();
System.out.println("链表长度为: "+ singleList.length);
singleList.del_tail();
singleList.del_tail();
singleList.show();
System.out.println("链表长度为: "+ singleList.length);
}
}
class SingleList{
public int length = 0 ; //链表长度
//初始化头结点
private Node head = new Node(0,0);
//添加结点(尾插)
public void add_tail(Node tmp){
//辅助指针
Node node = head;
//遍历链表 找到尾结点
while(node.Next != null){
node = node.Next;
}
node.Next = tmp;
length ++;
}
//删除结点(尾删)
public void del_tail(){
Node delete = head.Next; //保存删除结点位置
Node node = head; //保存删除结点的前一个位置
if(delete == null){
System.out.println("链表为空,无法删除");
return;
}
//遍历链表 找到尾结点(即删除的结点)
while(delete.Next != null){
delete = delete.Next;
node = node.Next;
}
System.out.println("删除结点数据为: "+ delete);
node.Next = null;
length --;
}
public void show(){
//判断链表是否为空
if(head.Next == null){
System.out.println("链表为空");
return;
}
Node node = head.Next;
while(node != null){
System.out.println("结点数据为: "+ node);
node = node.Next;
}
}
}
class Node{
public int id ; //结点编号
public int val; //保存结点数据
public Node Next; //指向下一节点
public Node(int val,int id) {
this.val = val;
this.id = id;
}
@Override
public String toString() {
return "Node{"+
" id=" + id +
" val=" + val +
'}';
}
}
输出结果:
说明:此文章为学习笔记,如有侵权请联系删除。