Java —— 链表的创建


第一次用java敲链表吐舌头


// 每一个链表实际上是由多个结点组成的
class Node { // 定义一个节点
	private String data; // 要保存的数据
	private Node next; // 要保存的下一个节点
	// 每一个Node类对象都必须保存有相应的数据
	public Node(String data) {
		this.data = data;
	}
	public void setNext(Node next) {
		this.next = next;
	}
	public Node getNext() {
		return this.next;
	}
	public String getData() {
		return this.data;
	}
	public void addNode(Node newNode) {
		if(this.next == null) {
			this.next = newNode;
		}
		else {
			this.next.addNode(newNode);
		}
	} 
	public void printNode() {
		System.out.println(this.data) ;
		if(this.next != null) {
			this.next.printNode();
		}
	}
}
class Link {
	private Node root ; // 根节点
	private Node last;
	public void add(String data) { //增加数据
		Node newNode = new Node(data);

		if(this.root == null) {
			this.root = newNode;
			this.last = newNode;
		}
		else {
			this.last.setNext(newNode);
			this.last = newNode;
		}

/* 

	和上面的操作结果是一样的。也就是说得到的链表的逻辑关系是相同的。
	下面的操作的就是在Node类中定义一个添加节点的方法,然后把新创建
	的节点传过去然后通过递归找到最后一个节点,把newNode添加上去,
	如果这样
	需要比较的次数就是[(n-1)*n]/2
	时间复杂度就是
	O(n^2).
	效率十分低.
	所以就直接在私有属性中定义一个尾结点指针last,每次指向链表的最后一个结点,
	直接添加就可以了。
	O(1)

	数据结构学的他这样弄挺别扭的。
*/

//		if(this.root == null) {
//			this.root = newNode;
//		}
//		else {
//			this.root.addNode(newNode);
//		}

	}
	public void print() { // 输出数据
		if(this.root != null) {
			this.root.printNode();
		}
	}
}
public class Demo {
	public static void main(String []args) {
		Link link = new Link() ;
		link.add("Hello");
		link.add("World");
		link.add("Csdn");
		link.add("WW");
		link.print();
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值