Java实现链表

1、Node.java(链表结点类实现)

package test2;

public class Node {
    // 为了避免外部对象直接访问Node的成员
    // 建议尽量降低访问权限
    private int value;  // 节点的值
    private Node next;  // 节点的 next对象

    // 为Node类型提供必要的构造器和get、set方法
    public Node(int value){
        this.value = value;
        next = null;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public void setNext(Node next) {
       this.next = next;
    }

    public int getValue() {
        return this.value;
    }

    public Node getNext() {
        return this.next;
    }
}

2、List.java(链表类的实现)

package test2;
public class List {
    // 单链表或者循环链表
    private Node head; // 链表首节点,其index为0
    private Node last; // 链表尾结点
    public List() {
        head = last = null;
    }

    // 将指定节点添加到此列表的结尾
    public boolean insert(Node node) {
        return insert(length(), node);
    }

    // 根据value生成Node对象,添加至指定位置
    public boolean insert(int index, int value) {
        return insert(index, new Node(value));
    }

    // 将Node对象添加至指定位置
    // 如果 index<0 或者 index>length() ,则返回false
    public boolean insert(int index, Node node) {
        // **补充代码
		if(index<0||index>length())
			return false;
		
		int countOfNode =0;
		Node pointer = head;
	
		if(index==0&&this.isEmpty()){
			head=node;
			last=node;
			return true;
		}
		if(index==0){
			pointer = head;
			head = node;
			node.setNext(pointer);
			return true;
		}
	
		Node quote = head;
		while(countOfNode!=index-1){
			++countOfNode;
			pointer=pointer.getNext();
		}
		if(index!=length()){
			quote = pointer.getNext();
			pointer.setNext(node);
			node.setNext(quote) ;
		}else{
			pointer.setNext(node);
			last = node;
		}
		return true;
    }

    // 将指定元素插入此列表的开头
    public boolean insertToHead(Node node) {
        return insert(0, node);
    }

    // 返回此列表中指定位置处的元素
    public Node get(int index) {
        // **补充代码
		if(index<0||index>=this.length()){
			System.out.println("输入元素非法!");
			return null;
		}
		int countOfNode =0;
		Node pointer = head;
		while(countOfNode!=index){
			pointer=pointer.getNext();
			++countOfNode;
		}
		return pointer;
    }

    // 将此列表中指定位置的元素value值更新为指定值
    public void setData(int index, int value) {
        // **补充代码
        Node quote = this.get(index);
        quote.setValue(value);
    }

    // 返回此列表的元素个数
    // 因为length()只在类体内部调用,可以设置为private权限
    private int length() {
        // **补充代码
		int countOfNode=0;
		Node quote = this.head;
		if(head==null)
			return 0;
		while(quote!=last){
			quote = quote.getNext();
			++countOfNode;
		}
		return ++countOfNode;
    }

    // 如果此 循环链表不包含元素,则返回 true
    public boolean isEmpty() {
        // **补充代码
		return  this.head==null;
    }

    // 返回此列表的第一个元素
    public Node getHead() {
        return head;
    }

    // 返回此列表的最后一个元素
    public Node getLast() {
        return last;
    }

    // 遍历输出每个节点的序号和value
    public void printList() {
        // **补充代码
		Node quote = head;
		for(int i=0;i<this.length();i++){
			System.out.println(i+" : "+quote.getValue()+";");
			if(quote.getNext()==null){
					break;
			}
			quote = quote.getNext();
		}
    }

    // 移除此列表中指定位置处的元素,并返回被删除元素对象
    public Node remove(int index) {
        // **补充代码
        Node quote = this.get(index);
		if(index==0){
			this.head=quote.getNext();
		}
		else if(index==length()-1){
			Node previousquote = this.get(index-1);
			last = previousquote;
		}else if(index>=this.length()){
			return null;
		}
		else{
			Node previousquote = this.get(index-1);
			previousquote.setNext(quote.getNext());
		}
        return quote; 
    }

    /* 本方法选做 */
    // 将参数list中的所有元素按照原有顺序添加至此列表的结尾
    boolean addAll(List list) {
        // **补充代码
		Node quote = list.head;
		for(int i =0;i<=list.length();i++){
			this.insert(this.length(),new Node(quote.getValue()));
			quote = quote.getNext();
		}	
		return true;
    }
}

3、main.java测试主类

package test2;
public class ListTest {
    public static void main(String[] args) {
       
        System.out.println("--------insert test---------");
        List list = new List();
        list.insertToHead(new Node(1));
        list.insert(0, new Node(2));
        list.insert(0,new Node(3));
        list.insert(new Node(4));
        list.insert(2, 5);
        list.insertToHead(new Node(9));
        list.insert(0,new Node(6));
        list.insert(new Node(7));
        list.insert(new Node(8));
        list.printList();

        System.out.println("--------remove test---------");
        list.remove(3);
        list.remove(0);
        list.remove(6);
        list.printList();
    }
}

本文为笔者的java实习作业,欢迎大家讨论指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东北大马猴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值