用JAVA写链表


public class Link {
	public static void main(String args[]){
		LinkList all = new LinkList();
		all.add("A");
		all.add("B");
		all.add("C");
		all.remove("A");
		Object [] data = all.toArray();
		for(int i = 0; i < all.size(); i++){
			String str = (String) data[i];
			System.out.println(str + "、 ");
		}
	}

}
class LinkList{
	class Node{
		private Object dataObject;//节点数据
		private Node nextNode;//节点的下一个节点
		
		public Node(Object dataObject){
			this.dataObject = dataObject;
		}
		
		public void addNode(Node node){
			if(this.nextNode == null){//判断this的下一个节点是否为空,即该节点是否是最后一个节点
				this.nextNode = node;
			}else{
				this.nextNode.addNode(node);
			}			
		}
		
		public Boolean containNode(Object dataObject){//判断是否含有该节点数据
			if(dataObject.equals(this.dataObject)){
				return true;
			}else{
				if(this.nextNode == null){
					return false;
				}else{
					return this.nextNode.containNode(dataObject);
				}
			}
		}
		
		public Object getNode(int index){
			if(LinkList.this.foot++ == index){
				return this.dataObject;
			}else{
				return this.nextNode.getNode(index);
			}
		}
		
		public void setNode(int index, Object dataObject){
			if(LinkList.this.foot++ == index){
				this.dataObject = dataObject;
			}else{
				this.nextNode.setNode(index, dataObject);
			}
		}
		
		public void removeNode(Node previous, Object dataObject){
			if(dataObject.equals(this.dataObject)){
				previous.nextNode = this.nextNode;
			}else{
				this.nextNode.removeNode(this, dataObject);
			}
		}
		public void toArrayNode(){
			LinkList.this.retArray[LinkList.this.foot++] = this.dataObject;
			if(this.nextNode != null){
				this.nextNode.toArrayNode();
			}
		}
		
		
	}
	
	private Node rootNode;//根目录
	private int foot = 0;//索引
	private int count = 0;//保存元素的数据
	private Object []retArray;//返回的节点数据数组
	
	public void add(Object newdata){
		if(newdata == null){
			return;
		}
		Node newNode = new Node(newdata);
		if(this.rootNode == null){
			this.rootNode = newNode;
		}else{
			this.rootNode.addNode(newNode);
		}
		this.count ++;
	}
	public Boolean contain(Object dataObject){
		if(dataObject == null || this.rootNode == null){
			return false;
		}else{
			return this.rootNode.containNode(dataObject);
		}
	}
	public Object get(int index){
		if(index > this.count){
			return null;
		}
		this.foot = 0;
		return this.rootNode.getNode(index);
	}
	public void set(int index, Object dataObject){
		if(index > this.count){
			return;
		}else{
			this.foot = 0;
			this.rootNode.setNode(index, dataObject);
		}
	}
	public void remove(Object dataObject){
		if(contain(dataObject)){
			if(dataObject.equals(this.rootNode.dataObject)){
				this.rootNode = this.rootNode.nextNode;
			}else{
				this.rootNode.nextNode.removeNode(this.rootNode, dataObject);
			}
			this.count --;
		}
	}
	public Object[] toArray(){
		if(this.rootNode == null){
			return null;
		}else{
			this.foot = 0;
			this.retArray = new Object[this.count];
			this.rootNode.toArrayNode();
			return this.retArray;
		}
	}
	public int size(){
		return this.count;
	}
	public Boolean isEmpty(){
		if(this.count == 0){
			return true;
		}else{
			return false;
		}
	}
	public void clear(){
		this.rootNode = null;
		this.count = 0;	}
}

Java中,手链表(也称为单链表)是一种基础数据结构,它由一系列节点组成,每个节点包含数据和指向下一个节点的引用。创建和操作链表的便利之处在于它的动态性和灵活性,可以在运行时添加或删除元素,而不需要像数组那样预先分配固定大小。 下面是一个简单的Java链表节点和链表类的例子: ```java // 链表节点定义 class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } // 链表类定义 public class LinkedList { private Node head; // 头节点 // 插入节点方法 public void insert(int value) { Node newNode = new Node(value); if (head == null) { head = newNode; } else { Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } } // 查找节点方法 public boolean contains(int value) { Node current = head; while (current != null) { if (current.data == value) { return true; } current = current.next; } return false; } // 删除节点方法 public void remove(int value) { if (head == null) { return; } if (head.data == value) { head = head.next; return; } Node current = head; while (current.next != null && current.next.data != value) { current = current.next; } if (current.next != null) { current.next = current.next.next; } } // 显示链表元素(仅做演示,实际应用中可能有更优雅的方法) public void printList() { Node current = head; while (current != null) { System.out.print(current.data + " -> "); current = current.next; } System.out.println("null"); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值