java 实现单链表

c语言中链表需要自己实现,而java提供好了LinkedList供调用,闲来无事上手用JAVA写一个,菜鸟一枚,不当不规范之处希望各位大佬指正^_^

 

先创建一个节点的模板类

class Node{/* 创建链表子节点模板*/
    public String name;//节点数据示例
    public String sex;//性别
    public int age;//年龄
    public int score;//总分
    public Node next = null;//下个节点指针
    public Node(String name,String sex,int age,int score){
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.score = score;
    }
}

创建链表操作方法类,提供链表操作方法

/*链表访问控制类*/
class PacketNode{
    public Node head = null;//头节点
    public Node last = null;//尾节点
/*构造方法存储链表头节点*/
    public PacketNode(Node head)

/*添加节点到表尾*/
    public boolean AddEnd(Node node)

/*插入节点到指定位置*/
    public  boolean Add(Node node,int place)

/*删除指定位置节点*/
    public boolean Delete(int place)

/*遍历节点,返回长度*/
    public int Length()

/*输出所有数据*/
    public void PrintAll()
}

具体实现步骤和调用方法:

/*******************************************************************
* Java实现链表数据结构
* 用法示例:----------
* PacketNode node = new PacketNode(new Node("List_head"));
 *
 * //创建头节点-创建一条链表,第一个数据为"List_head"
* node.AddEnd(new Node("666"));//到表尾插入字符串"666"
* node.Add(new Node("inset"),5);//在第5个(0-5)元素位置插入数据"inset"
* node.Delete(2);//删除第2个(0-2)元素
* *******************************************************************/
/**/
class Node{/* 创建链表子节点模板*/
    public String name;//节点数据示例
    public String sex;//性别
    public int age;//年龄
    public int score;//总分
    public Node next = null;//下个节点指针
    public Node(String name,String sex,int age,int score){
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.score = score;
    }
}
/*链表访问控制类*/
class PacketNode{
    public Node head = null;//头节点
    public Node last = null;//尾节点
    /*构造方法存储链表头节点*/
    public PacketNode(Node head){
        this.head = head;
    }
    /*添加节点到表尾*/
    public boolean AddEnd(Node node){
        Node temp = this.head;
        while (temp.next != null){//遍历至尾
            temp = temp.next;//下一个节点
        }
        temp.next = node;//连接尾节点
        last = node;//尾节点更新
        return true;
    }
    /*插入节点到指定位置*/
    public  boolean Add(Node node,int place){
        Node temp = this.head;
        int count = 0;
        if (place<0) {
            System.out.println("\n插入失败,必须是正整数..^_^..");
            return false;
        }else if (place == 0){//如果插入的是第一个节点位置
            node.next = this.head;//node的next指向第一个节点
            this.head = node;//首节点给node
            return true;
        }
        while (temp.next != null){//遍历至尾
            if (count == place-1){//找到插入位置前一节点
                node.next = temp.next;//把后一节点指针给要插入的node
                temp.next = node;//连接node到前一节点
                return true;
            }
            temp = temp.next;//下一个节点
            count++;
        }
        if (count == place-1){//插入的是尾节点
            temp.next = node;
            last = node;//尾节点更新
            return true;
        }
        System.out.println("\n插入失败,超范围啦..^_^..");
        return false;
    }
    /*删除指定位置节点*/
    public boolean Delete(int place){
        Node temp = this.head;//
        int count = 0;
        int length = Length()-1;//长度按1---计数
        if (length < place || place < 0){
            System.out.println("\n该删除位置超过表的范围!!\n");
            return false;
        }else if (place == 0){//如果删除的是第一个节点位置
            this.head = temp.next;
            temp.next = null;
            return true;
        }else {
            while (temp.next != null) {
                if (count == place-1){//找到删除位置前一节点
                    temp.next = temp.next.next;//后后节点指针给前节点的next
                    return true;
                }
                temp = temp.next;//下一个节点
                count++;
            }
            last = temp;//尾节点缓存
        }
        return false;
    }
    /*遍历节点,返回长度*/
    public int Length() {
        int length = 1;
        Node temp = this.head;//交换头结点指针
        while (temp.next != null){
             temp = temp.next;//下一个节点
            length++;
        }
        return length;
    }
    /*输出所有数据*/
    public void PrintAll(){
        Node temp = this.head;//new Node();
        System.out.println("姓名 | 性别 | 年龄 | 总分");
        while (temp.next != null){
            System.out.println(temp.name+"  "+temp.sex+"  "+temp.age+"  "+temp.score+"  ");
            temp = temp.next;//下一个节点
        }
        System.out.println(temp.name+"  "+temp.sex+"  "+temp.age+"  "+temp.score+"  ");
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值