单链表1

单链表类:

package org.zp.datastruct;

public class SLL {
	// 链表节点类
	private static class SLLNode {
		private Object data;
		private SLLNode next;

		public SLLNode() {

		}

		public SLLNode(Object data) {
			this.data = data;
		}

		public SLLNode(Object data, SLLNode next) {
			this.data = data;
			this.next = next;
		}

		public Object getData() {
			return data;
		}

		public void setData(Object data) {
			this.data = data;
		}

		public SLLNode getNext() {
			return next;
		}

		public void setNext(SLLNode next) {
			this.next = next;
		}

		public String toString() {
			return data.toString();
		}
	}

	// 根节点和节点数
	private SLLNode root;
	private int count = 0;

	public SLLNode getRoot() {
		return root;
	}

	public void setRoot(SLLNode root) {
		this.root = root;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public boolean isEmpty() {
		return root == null;
	}

	public SLLNode insertReturnNode(String data) { // 追加
		SLLNode ss = new SLLNode(data);
		SLLNode node = root; // root(根节点)
		if (this.isEmpty()) { // 链表为空时,即插入根节点,root为整个链表的根节点
			root = ss;
		} else {
			// while (root.next != null) { //
			// 不能直接用root,root为全局变量,改变root就改变了整个链表的root
			// root = root.next;
			// }
			while (node.next != null) { // 找出最后一个节点,当node.next==null时跳出此时node为链表尾节点
				node = node.next;
			}
			insertNode(data, node);
		}
		return ss;
	}

	public void insertNode(String data) { // 追加
		SLLNode ss = new SLLNode(data);
		SLLNode node = root; // root(根节点)
		if (this.isEmpty()) { // 链表为空时,即插入根节点,root为整个链表的根节点
			root = ss;
		} else {
			// while (root.next != null) { //
			// 不能直接用root,root为全局变量,改变root就改变了整个链表的root
			// root = root.next;
			// }
			while (node.next != null) { // 找出最后一个节点,当node.next==null时跳出此时node为链表尾节点
				node = node.next;
			}
			insertNode(data, node);
		}
	}

	public void insertNode(String data, SLLNode node) {
		SLLNode newNode = new SLLNode(data);
		if (node == null) {		// 第一个节点之前插入
			newNode.next = root;
			root = newNode;
		} else {		// 在node指向的节点之后插入
			newNode.next = node.next;
			node.next = newNode;
		}
		count++;
	}

	public void deleteNode(SLLNode node) { // 不能根据数据域来删除节点
		if (isEmpty()) {
			System.out.println("空链表!");
			return;
		} else {
			if (node == root) {
				root = root.next;
			} else {
				SLLNode prior = root;
				while (prior.next != node) {	// 找到node
					prior = prior.next;
				}
				prior.next = node.next; 	// 删除node	
			}
		}
	}

	public int size() {
		return count + 1;
	}

	public void print() {
		// 通过每次改变root(将下一个节点赋给root)直到链表尾,来获得链表每个元素值
		if (curr.next != null) {
                    buf += curr.data.toString() + "->";
                } else {
                    buf += curr.data.toString();
                }
		// 方法2
		// while(root != null) {
		// System.out.print(root.data + "->");
		// root = root.next;
		// // System.out.println(root.data);
		// }
		// 方法3
		// SLLNode curr = root;
		// while(curr != null) {
		// System.out.print(curr.data + "->");
		// curr = curr.next;
		// }
	}
}

测试类:

package org.zp.datastruct;

//import org.zp.datastruct.SLL.SLLNode;

public class SLLTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SLL s = new SLL();
//		SLL.SLLNode s1 = s.new SLLNode();
		s.insertNode("root");
		s.insertNode("A");
		s.insertNode("C");
		s.insertNode("A");
		s.insertNode("F");
//		SLLNode s1 = s.insertReturnNode("B");
		s.insertReturnNode("D");
//		System.out.println(s.size());
//		System.out.println(s.getRoot());
		s.print();
//		s.deleteNode(s1);    // 如何删除节点
	}
}

问题:如何在测试类中获得SLLNode(节点类)实例化对象




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值