JAVA简易链表的建立 内部类和外部类

class Link{
	class Node{
		private Object data;
		private Node next;
		
		Node(Object data){
			this.data = data;
		}
		
		//第一次调用  this = root               data  (第二个节点添加完成)
		//第二次调用  this =root->next 		  data  (第三个节点添加完成)
		//第三次调用  this =root->next->next    data  (第四个节点添加完成)
		public void addNode(Node newNode){
			if(this.next == null){
				this.next = newNode;
			} else {
				this.next.addNode(newNode);
			}
		}
		//第一次调用 this = root              data
		//第二次调用 this = root->next        data
		//第二次调用 this = root->next->next  data 
		public boolean containsNode(Object data){
			if(this.data.equals(data)){
				return true;
			} else {
				if(this.next == null)
					return false;
				else
					return this.next.containsNode(data);
			}
		}
		
		//第一次调用 this = root             foot = 0 
		//第二次调用 this = root->next       foot = 1
		//第三次调用 this = root->next->next foot = 2
		public Object getNode(int index){
			if(Link.this.foot++ == index){
				return this.data;
			}
			else{
				return this.next.getNode(index);
			}
		}
		
		//第一次调用 this = root             foot = 0 
		//第二次调用 this = root->next       foot = 1
		//第三次调用 this = root->next->next foot = 2
		public void setNode(int index, Object data){
			if(Link.this.foot ++ == index){
				this.data = data;
			}
			else{
				this.next.setNode(index, data);
			}
		}
		
		//第一次调用this = root->next  pre = root
		//第二次调用this = root->next->next pre = root->next
		//第三次调用this = root->next->next->next pre = root->next->next
		public void removeNode(Node pre, Object data){
			if(this.data.equals(data)){
				pre.next = this.next;
			} else {
				this.next.removeNode(this, data);
			}
		}
		
		public void toArrayNode(){
			Link.this.array[Link.this.foot++] = this.data;
			if(this.next == null){
				return ;
			}else {
				this.next.toArrayNode();
			}
		}
	}
	//以上为内部类
	
	private Node root;
	private int count = 0;//用于记录节点个数
	private int foot = 0;
	private Object[] array;
	//添加节点
	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(this.root == null||data == 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(contains(data)){
			if(this.root.data.equals(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.array = new Object[this.count];
		this.root.toArrayNode();
		return this.array;		
	}
}
public class LinkDemo2 {
	public static void main(String[] args) {
		Link all = new Link();
		all.add(1);
		all.add(2);
		all.add(3);
		
		System.out.println(all.size());
		Object data[] = all.toArray();
		for(int i = 0; i < data.length; i++){
			int str = Integer.parseInt(data[i].toString());
			System.out.println(str);
		}
	}

}
class Node{
	private Object data;
	private Node next;
	Node(Object data){
		this.data = data;
	}
	public Object getDate(){
		return this.data;
	}
	public Node getNext(){
		return this.next;
	}
	public void addNode(Node newnode){
		if(this.next == null){
			this.next = newnode;
		}
		else 
			this.next.addNode(newnode);
	}
	public void printNode(){
		System.out.println((String)this.data);
		if(this.next!=null)
			this.next.printNode();
		else
			return ;
	}
	public boolean searchNode(Object data){
		if(this.data.equals(data))
			return true;
		if(this.next == null)
			return false;
		else
			return this.next.searchNode(data);
	}
	public boolean deleteNode(Node node,Object data){
		if(this.data.equals(data)){
			node.next = this.next;
			return true;
		}else{
			return this.next.deleteNode(this, data);
		}
	}
}
class Link{
	private Node root;
	
	public void add(Object data){
		Node newnode = new Node(data);
		if(root == null){
			root = newnode;
		}
		else {
			root.addNode(newnode);
		}
	}
	
	public void print(){  
        if(this.root!=null){  
            this.root.printNode();  
        }  
	}  
	
	public boolean search(Object data){
		if(this.root==null)
			return false;
		else
			return this.root.searchNode(data);
	}
	
	public boolean delete(Object data){
		if(this.root==null)
			return false;
		if(!this.search(data))
			return false;
		else{
			if(this.root.getDate().equals(data)){
				this.root = this.root.getNext();
				return true;
			}
			else
				return this.root.getNext().deleteNode(this.root, data);
		}
	}
}
public class waibuclass {

	public static void main(String[] args) {
		Link link = new Link();
		link.add("1");
		link.add("2");
		link.add("3");
		link.print();
		link.delete("1");
		link.print();
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的链表是一种数据结构,它可用于存储一系列具有相同类型的对象。链表中的每个对象都被称为节点,每个节点都包含数据和指向下一个节点的引用。 在Java中,链表一般使用Node类来实现。Node类具有两个属性:data用于存储数据,next用于指向下一个节点。在创建链表时,我们需要定义一个头节点,在链表的第一个位置,并将头节点的next属性指向第一个实际的节点。 链表的优点是可以动态地添加和删除节点,不需要事先定义链表的长度。相比于数组,链表的插入和删除操作更加高效。但是链表的缺点是访问链表中的任意一个节点的时候需要从头节点开始遍历,导致访问时间较长。 一般来说,我们可以通过遍历链表的方式来查找、插入和删除节点。例如,要查找具有特定值的节点,我们可以从头节点开始遍历链表,直到找到相应的节点。要在两个节点之间插入一个新节点,我们需要将新节点的next属性指向后一个节点,并将前一个节点的next属性指向新节点。要删除一个节点,我们将前一个节点的next属性指向后一个节点,从而跳过被删除的节点。 总之,Java中的链表是一种灵活的数据结构,它可以用于存储和操作一系列对象。通过不断遍历链表中的节点,我们可以对链表进行查找、插入和删除等操作。然而,由于链表的访问时间较长,需要在实际应用中根据场景和要求选择合适的数据结构。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值