day13--链表

学习链表的一些基本操作,初始化链表,插入与删除节点。
代码:

package com;

public class LinkedList {

    /**
     * 节点
     */
    class Node {
        int data;//数据
        Node next;//指向下一个节点


        public Node(int paraValue) {
            data = paraValue;
            next = null;
        }

    }

    Node header;//头结点的数据域不存放数据
    /***
     * 构造空链表
     */
    public LinkedList() {
        header =new Node(0);
    }

    public String toString() {
        String resultString = "";

        if(header.next == null) {
            return "empty";
        }
        Node tempNode = header.next;
        while(tempNode != null) {
            resultString += tempNode.data + ", ";
            tempNode = tempNode.next;
        }
        return resultString;
    }
    //清空链表
    public void reset() {
        header.next = null;
    }

    /**
     * 查找给定值的位置,若有多个返回第一个
     * @param paraValue 需要查找的值
     * @return 位置,返回值为-1代表没有找到
     */
    public int locate(int paraValue) {
        int tempPosition = -1;

        Node tempNode = header.next;
        int tempCurrentPosition = 0;
        while (tempNode != null) {
            if(tempNode.data == paraValue) {
                tempPosition = tempCurrentPosition;
                break;
            }

            tempNode = tempNode.next;
            tempPosition++;
        }
        return tempPosition;
    }

    /**
     * 插入
     * @param paraPosition 插入位置
     * @param paraValue 插入值
     * @return 成功返回true ,失败返回false
     */
    public boolean insert(int paraPosition,int paraValue) {
        Node tempNode =  header;
        Node tempNewNode;

        for(int i = 0; i < paraPosition; i++) {
            if(tempNode.next == null) {
                System.out.println("The position " + paraPosition + " is illegal.");
                return false;
            }
            tempNode = tempNode.next;
        }
        tempNewNode = new Node(paraValue);

        tempNewNode.next = tempNode.next;
        tempNode.next = tempNewNode;

        return true;

    }

    /**
     * 删除
     * @param paraPosition 需要删除的位置
     * @return 成功返回true ,失败返回false
     */
    public boolean delete(int paraPosition) {
        if(header.next == null) {
            System.out.println("Cannot delete element from an empty list.");
            return false;
        }
        Node tempNode = header;

        for(int i = 0; i < paraPosition; i++) {
            if(tempNode.next.next == null) {
                System.out.println("The position " + paraPosition + " is illegal.");
                return false;
            }
            tempNode = tempNode.next;
        }
        tempNode.next = tempNode.next.next;
        return true;

    }

    public static void main(String[] args) {
        LinkedList tempFirstList = new LinkedList();
        System.out.println("Initialized, the list is: " + tempFirstList.toString());

        for (int i = 0; i < 5; i++) {
            tempFirstList.insert(0, i);
        }
        System.out.println("Inserted, the list is: " + tempFirstList.toString());

        tempFirstList.insert(6, 9);

        tempFirstList.delete(4);

        tempFirstList.delete(2);
        System.out.println("Deleted, the list is: " + tempFirstList.toString());

        tempFirstList.delete(0);
        System.out.println("Deleted, the list is: " + tempFirstList.toString());

        for (int i = 0; i < 5; i++) {
            tempFirstList.delete(0);
            System.out.println("Looped delete, the list is: " + tempFirstList.toString());
        }
    }


}

运行结果:

Initialized, the list is: empty
Inserted, the list is: 4, 3, 2, 1, 0, 
The position 6 is illegal.
Deleted, the list is: 4, 3, 1, 
Deleted, the list is: 3, 1, 
Looped delete, the list is: 1, 
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值