Java 链表(链表的基本使用)

范例:链表的基本使用

class Book {
	private String name;
	private double price;

	public Book(String name, double price) {
		this.name = name;
		this.price = price;
	}

	public String getInfo() {
		return "书名:《" + this.name + "》,价格:" + this.price + "元。";

	}

	public boolean compare(Book book) {
		if (this == book) {
			return true;
		}
		if (book == null) {
			return false;
		}
		if (this.name.equals(book.name) && this.price == book.price) {
			return true;
		}
		return false;
	}
}

// 链表类
class Link {
	class Node {// 定义节点类
		private Node next;// 引用关系
		private Book data;// 保存数据

		// 构造方法
		public Node(Book data) {
			this.data = data;
		}

		// 1.添加节点
		public void addNode(Node newNode) {
			if (this.next == null) {// 当前节点的下一个节点为空
				this.next = newNode;
			} else {// 向后继续保存
				this.next.addNode(newNode);
			}
		}

		// 2.查询数据
		public boolean containsNode(Book data) {
			if (data.compare(this.data)) {// 当前节点数据为要查询的数据
				return true;// 后面不在需要查询
			} else {// 当前节点不能满足查询
				if (this.next != null) {// 判断后续是否还有节点
					return this.next.containsNode(data);
				} else {// 没后后续节点
					return false;
				}
			}
		}

		// 3.数据修改
		public void setNode(int index, Book data) {
			// 使用自定义foot内容与 index比较
			if (Link.this.foot++ == index) {
				this.data = data;// 进行内容修改
			} else {// 继续
				this.next.setNode(index, data);
			}
		}

		// 4.获取数据
		public Book getNode(int index) {
			// 使用自定义foot内容与 index比较
			// foot自增,目的为下一次查询方便
			if (Link.this.foot++ == index) {// 要查询的索引
				return this.data;// 返回当前节点数据
			} else {// 继续向后查
				return this.next.getNode(index);
			}
		}

		// 5.删除数据
		public void removeNode(Node previous, Book data) {
			if (data.compare(this.data)) {// 数据对应
				previous.next = this.next;// 空出当前节点
			} else {// 继续向后查询
				this.next.removeNode(this, data);
			}
		}

		// 6.对象数组
		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 Book[] retArray;// 返回数组

	// 1.添加数据
	public void add(Book data) {
		if (data == null) {// 数据不允许为null
			return;
		}
		Node newNode = new Node(data);// 保存数据,数据打包
		if (this.root == null) {// 当前没有根节点
			this.root = newNode;// 新节点为根节点
		} else {// 根节点存在,其它节点给Node处理
			this.root.addNode(newNode);
		}
		this.count++;// 每次保存成功后自增一次
	}

	// 2.查询数据
	public boolean contains(Book data) {
		if (data == null || this.root == null) {// 没有数据,根节点也没有数据
			return false;
		}
		return this.root.containsNode(data);
	}

	// 3.修改数据
	public void set(int index, Book data) {
		if (index > this.count) {
			return;
		}
		this.foot = 0;// 归零,从头向后开始
		this.root.setNode(index, data);// 修改过程交给Node
	}

	// 4.获取数据
	public Book get(int index) {
		if (index > this.count) {// 超出查询范围
			return null;
		}
		this.foot = 0;// 归零,从头向后开始
		return this.root.getNode(index);// 查询过程交给Node
	}

	// 5.删除数据
	public void remove(Book data) {
		if (this.contains(data)) {// 1.先判断是否存在此数据
			// 2.判断删除的数据是否根节点数据
			// 3.root是Node的对象,此处直接访问内部类的私有操作
			if (data.compare(this.root.data)) {// 根节点是否满足删除
				this.root = this.root.next;// 空出当前节点
			} else {// 不是根节点
				// 根节点的下一个节点开始判断
				this.root.next.removeNode(this.root, data);
			}
			this.count--;// 删除成功后自减
		}
	}

	// 6.链表长度
	public int length() {
		return this.count;
	}

	// 7.链表是否为空
	public boolean isEmpty() {
		return this.count == 0;
	}

	// 8.对象数组
	public Book[] toArray() {
		if (this.root == null) {
			return null;
		}
		this.foot = 0;
		this.retArray = new Book[this.count];// 开辟数组
		this.root.toArrayNode();
		return this.retArray;
	}

	// 打印方法
	public static void print(Book[] data) {
		for (Book x : data) {
			System.out.println(x.getInfo() + " ");
		}
		System.out.println();
	}

}

public class Demo03 {
	public static void main(String[] args) {
		Link all = new Link();
		all.add(new Book("Java入门", 88.8));
		all.add(new Book("Android入门", 99.6));
		all.add(new Book("Oracle入门", 89.9));
		all.remove(new Book("Java入门", 88.8));
		Book[] book = all.toArray();
		Link.print(book);
	}
}

  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
  链表类List的源代码如下: import Java.io.*; public class List {  /*用变量来实现表头*/  private Node Head=null;  private Node Tail=null;  private Node Pointer=null;  private int Length=0;  public void deleteAll()  /*清空整个链表*/  {   Head=null;   Tail=null;   Pointer=null;   Length=0;  }  public void reset()  /*链表复位,使第一个结点成为当前结点*/  {   Pointer=null;  }  public boolean isEmpty()  /*判断链表是否为空*/  {   return(Length==0);  }  public boolean isEnd()  /*判断当前结点是否为最后一个结点*/  {   if(Length==0)    throw new Java.lang.NullPointerException();   else if(Length==1)    return true;   else    return(cursor()==Tail);  }  public Object nextNode()  /*返回当前结点的下一个结点的值,并使其成为当前结点*/  {   if(Length==1)    throw new Java.util.NoSuchElementException();   else if(Length==0)    throw new Java.lang.NullPointerException();   else   {    Node temp=cursor();    Pointer=temp;    if(temp!=Tail)     return(temp.next.data);    else     throw new Java.util.NoSuchElementException();   }  }  public Object currentNode()  /*返回当前结点的值*/  {   Node temp=cursor();   return temp.data;  }     public void insert(Object d)  /*在当前结点前插入一个结点,并使其成为当前结点*/  {   Node e=new Node(d);   if(Length==0)   {    Tail=e;    Head=e;   }   else   {    Node temp=cursor();    e.next=temp;    if(Pointer==null)     Head=e;    else     Pointer.next=e;   }   Length++;  }  public int size()  /*返回链表的大小*/  {   return (Length);  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值