Java链表的实现

     传统的对象数组一旦声明则长度不可改变,因此,操作起来较为繁杂。

链表从本质上讲可以理解为“动态的对象数组”。链表可以实现对象的增加、删除、查询等等一系列的操作,可以实现动态扩充。如下为链表的实现:

package cn.mldn.java;
// 1、定义要保存对象的类:
class Phone {// 此类提供要保存的数据
	private String brand;
	private double price;

	public Phone() {
	}

	public Phone(String brand, double price) {
		this.brand = brand;
		this.price = price;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getBrand() {
		return brand;
	}

	public double getPrice() {
		return price;
	}

	@Override
	public boolean equals(Object obj) {// 对象比较
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Phone other = (Phone) obj;
		if (brand == null) {
			if (other.brand != null)
				return false;
		} else if (!brand.equals(other.brand))
			return false;
		if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "brand = " + this.brand + ", price = " + this.price;
	}
}
//2、定义链表的操作标准:
interface ILink {// 定义链表操作标准
	public void add(Object obj);// 向链表中保存数据

	public int size();// 取得链表的长度

	public Object get(int index);// 根据索引取得对应的数据

	public boolean isEmpty();// 判断链表是否为空

	public boolean contains(Object obj);// 判断某一对象是否存在

	public void set(int index, Object obj);// 根据索引设置数据

	public void remove(Object obj);// 删除数据

	public void clean();// 清空链表

	public Object[] toArray();// 取得链表中的所有数据
}
//3、链表的具体实现:
class LinkImpl implements ILink {
	private class Node {// 私有内部类,为外部类服务实现数据的增加等一系列的操作
		private Object data;// 可以保存任意类型的数据
		private Node next;// 指向下一个节点

		public Node(Object data) {// 数据封装
			this.data = data;
		}

		public void addNode(Node newNode) {
			if (this.next == null) {
				this.next = newNode;
			} else {
				this.next.addNode(newNode);
			}
		}

		public Object getNode(int index) {
			if (LinkImpl.this.foot++ == index) {
				return this.data;
			} else {
				return this.next.getNode(index);
			}
		}

		public boolean containsNode(Object obj) {// 需要对象比较
			if (this.data.equals(obj)) {
				return true;
			} else {
				if (this.next != null) {
					return this.next.containsNode(obj);
				}
				return false;
			}
		}

		public void setNode(int index, Object obj) {
			if (LinkImpl.this.foot++ == index) {
				this.data = obj;
			} else {
				this.next.setNode(index, obj);
			}
		}

		public void removeNode(Node previous, Object obj) {// 需要对象比较
			if (this.data.equals(obj)) {
				previous.next = this.next;
			} else {
				this.next.removeNode(this, obj);
			}
		}

		public void toArrayNode() {
			LinkImpl.this.retArray[foot++] = this.data;
			if (this.next != null) {
				this.next.toArrayNode();
			}
		}
	}

	// ****************************************************
	private Node root;// 根节点
	private int count;// 统计链表中的元素个数
	private int foot;// 链表元素的下角标
	private Object[] retArray;// 用于接收返回链表中的所有数据

	@Override
	public void add(Object obj) {
		if (obj == null) {// 要保存的数据为空,则结束调用
			return;
		}
		Node newNode = new Node(obj);// 封装数据为一节点
		if (this.root == null) {
			this.root = newNode;
		} else {
			this.root.addNode(newNode);
		}
		this.count++;// 数据增加成功,count 自增
	}

	@Override
	public int size() {
		return this.count;
	}

	@Override
	public Object get(int index) {
		if (this.isEmpty()) {// 链表为空
			return null;
		}
		if (index >= this.count) {
			return null;
		}
		this.foot = 0;// 保证数据是从第一个开始遍历
		return this.root.getNode(index);
	}

	@Override
	public boolean isEmpty() {
		return this.count == 0 && this.root == null;// 根节点不存在或者链表中的元素个数为0,表示链表为空链表
	}

	@Override
	public boolean contains(Object obj) {
		if (this.isEmpty()) {
			return false;
		}
		return this.root.containsNode(obj);
	}

	@Override
	public void set(int index, Object obj) {
		if (this.isEmpty()) {
			return;
		}
		if (index >= this.count) {
			return;
		}
		this.foot = 0;
		this.root.setNode(index, obj);
	}

	@Override
	public void remove(Object obj) {
		if (!this.contains(obj)) {
			return;
		}
		if (this.root.data.equals(obj)) {
			this.root = this.root.next;
		} else {
			this.root.next.removeNode(this.root, obj);
		}
		this.count--;// 数据删除成功,count 自减
	}

	@Override
	public void clean() {
		this.count = 0;
		this.root = null;
	}

	@Override
	public Object[] toArray() {
		if (this.isEmpty()) {
			return null;
		}
		this.foot = 0;
		this.retArray = new Object[this.count];
		this.root.toArrayNode();
		return this.retArray;
	}
}
//4、编写测试数据:
public class TestDemo {
	public static void main(String[] args) {
		ILink all = new LinkImpl();// 通过向上转型实例化链表
		System.out.println(all.size());
		System.out.println(all.isEmpty());
		all.add(new Phone("大米手机", 1999.0));
		all.add(new Phone("白米手机", 999.0));
		all.add(new Phone("黑族手机", 1999.0));
		System.out.println(all.isEmpty());
		System.out.println(all.size());
		System.out.println(all.get(1));
		System.out.println(all.contains(new Phone("黑族手机", 1999.0)));
		all.set(2, new Phone("白族手机", 899.0));
		System.out.println(all.get(2));
		all.remove(new Phone("白米手机", 999.0));
		System.out.println(all.size());
		Object obj[] = all.toArray();
		for (int x = 0; x < obj.length; x++) {
			System.out.println(obj[x]);
		}
	}
}


  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值