修改链表的Object类


class Link {
	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 boolean containsNode(Object data) {
			if (data.equals(this.data)) {
				return true;
			} else {
				if (this.next != null) {
					return this.next.containsNode(data);
				} else {
					return false;
				}
			}
		}
		public Object getNode(int index) {
			if (Link.this.foot ++ == index) {
				return this.data;
			} else {
				return this.next.getNode(index);
			}
		}
		public void setNode(int index, Object data) {
			if (Link.this.foot ++ == index) {
				this.data = data;
			} else {
				 this.next.setNode(index,data);
			}
		}
		public void removeNode(Node previous,Object data) {
			if (data.equals(this.data)) {
				previous.next = this.next;
			} else {
				 this.next.removeNode(this,data);
			}
		}
		public void toArrayNode() {
			Link.this.retArray[Link.this.foot++] = this.data;
		    if (this.next != null) {
				 this.next.toArrayNode();
			}
		}
	}
	private Node root;
	private int count = 0;
	private int foot = 0;//节点索引
	private Object [] retArray;
	public void add(Object data) {
		if (data == null) {
			return;
		}
		Node newNode = new Node(data);
		if (this.root == null) {
			this.root = newNode;
		} else {
			this.root.addNode(newNode);
		}
		this.count++;
	}
	public int size() {
		return this.count;
	}
	public boolean isEmpty() {
		return this.count == 0;
	}
	public boolean contains(Object data) {
		if ((data == null) ||
			(this.root == null)) {
			return false;
		} else {
			return this.root.containsNode(data);
		}
	}
	public Object get(int index) {
		if (index > this.count) {
			return null;
		}
		this.foot = 0;
		return this.root.getNode(index);
	}
	public void set(int index, Object data) {
		if (index > this.count) {
			return;
		}
		this.foot = 0;
		this.root.setNode(index, data);
	}
	public void remove(Object data) {
		if (this.contains(data)) {
			if (data.equals(this.root.data)) {
				this.root = this.root.next;
			} else {
				this.root.next.removeNode(this.root, data);
			}
			this.count--;
		}
	}
	public Object[] toArray() {
		if (this.root == null) {
			return null;
		}
		this.foot = 0;
		this.retArray = new Object[this.count];
		this.root.toArrayNode();
		return this.retArray;
	}
	public void clear() {
		this.root = null;
		this.count = 0;
	}
}

public class TestLink {
	public static void main(String args[]) {
		Link link = new Link();
		link.add("A");
		link.add("B");
		link.add("C");
		link.remove("A");
		Object data[] = link.toArray();
		for (int x = 0; x < data.length; x++ ) {
			String str = (String) data[x];//关键强制向下转换Object对象
			System.out.print(str + ",");
		}

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值