日撸Java三百行 day13(链表)

1. 链表的基础操作

1.1 节点的定义

class Node {
	/**
	* The data.
	*/
	int data;

	/**
	* The reference to the next node.
	*/
	Node next;

    /**
	*********************
	* The constructor
	*
	* @param paraValue
	*            The data.
	*********************
	*/
	public Node(int paraValue) {
		data = paraValue;
		next = null;
	}// Of the constructor
}// Of class Node

这里的在链表类中嵌套了一个Node类,有点对标c语言中的结构体的意思。(类的实质是一种引用数据类型,类似于基本的数据类型,但它的本质是数据类型,不是数据,只有被实例化为对象后才能被操作)

1.2 链表的重置

/**
*********************
* Reset to empty. Free the space through garbage collection.
*********************
*/
public void reset() {
	header.next = null;
}// Of reset

Java中的链表重置直接把头节点后的指针指向空就可,表示之前后面链接的部分不再引用,空间系统会自动将其回收 ,不需要free。

1.3 链表的查找、插入、删除

1.3.1 给值查位

public int locate(int paraValue) {
	int tempPosition = -1;

	Node tempNode = header.next;
	int tempCurrentPosition = 0;
	while (tempNode != null) {
		if (tempNode.data == paraValue) {
			tempPosition = tempCurrentPosition;
			break;
		} // Of if

		tempNode = tempNode.next;
		tempCurrentPosition++;
	} // Of while

	return tempPosition;
}// Of locate

给定一个值,从头到尾遍历链表,并且设置一个当前位置的tempPosition,初值设为0,若找到返回此时的tempPosition,否则返回0。

1.3.2 链表的插入

public boolean insert(int paraPosition, int paraValue) {
	Node tempNode = header;
	Node tempNewNode;

	for (int i = 0; i < paraPosition; i++) {
		if (tempNode.next == null) {
			System.out.println("The position " + paraPosition + " is illegal.");
			return false;
		} // Of if

		tempNode = tempNode.next;
	} // Of for i

	// Construct a new node
	tempNewNode = new Node(paraValue);

	// Now link them.
	tempNewNode.next = tempNode.next;
	tempNode.next = tempNewNode;

	return true;
}// Of insert

插入首先需要判断插入的位置是否合法,若合法再创建一个新的节点,去存储新插入的节点, 然后再链接。链接时一定记得注意next的赋值顺序!

 1.3.3 链表的删除

public boolean delete(int paraPosition) {
	if (header.next == null) {
		System.out.println("Cannot delete element from an empty list.");
		return false;
	} // Of if

	Node tempNode = header;

	for (int i = 0; i < paraPosition; i++) {
		if (tempNode.next == null) {
			System.out.println("The position " + paraPosition + " is illeage.");
			return false;
		} // Of if

		tempNode = tempNode.next;
	} // Of for i

	tempNode.next = tempNode.next.next;

	return true;
}// Of delete

删除节点比起插入需要进行多一步的判断,首先需要判断链表中是否还有元素可以删除, 其次需要遍历链表,判断该元素是否在链表中

2.链表的简单测试

package datastructure;

/**
 * Linked list.
 * 
 * @author Yunhua Hu yunhuahu0528@163.com.
 */

public class LinkedList {

	/**
	 * An inner class
	 */
	class Node {
		/**
		 * The data.
		 */
		int data;

		/**
		 * The reference to the next node.
		 */
		Node next;

		/**
		 *********************
		 * The constructor
		 *
		 * @param paraValue
		 *            The data.
		 *********************
		 */
		public Node(int paraValue) {
			data = paraValue;
			next = null;
		}// Of the constructor
	}// Of class Node

	/**
	 * The header node. The data is never used.
	 */
	Node header;

	/**
	 *********************
	 * Construct an empty linked list.
	 *********************
	 */
	public LinkedList() {
		header = new Node(0);
		// header.next = null; //Redundant
	}// Of the first constructor

	/**
	 *********************
	 * Overrides the method claimed in Object, the superclass of any class.
	 *********************
	 */
	public String toString() {
		String resultString = "";

		if (header.next == null) {
			return "empty";
		} // Of if

		Node tempNode = header.next;
		while (tempNode != null) {
			resultString += tempNode.data + ", ";
			tempNode = tempNode.next;
		} // Of the while

		return resultString;
	}// Of toString

	/**
	 *********************
	 * Reset to empty. Free the space through garbage collection.
	 *********************
	 */
	public void reset() {
		header.next = null;
	}// Of reset

	/**
	 *********************
	 * Locate the given value. If it appears in multiple position, simply return the
	 * first one.
	 *
	 * @param paraValue
	 *            The given value
	 * @return The position. -1 for not found.
	 *********************
	 */
	public int locate(int paraValue) {
		int tempPosition = -1;

		Node tempNode = header.next;
		int tempCurrentPosition = 0;
		while (tempNode != null) {
			if (tempNode.data == paraValue) {
				tempPosition = tempCurrentPosition;
				break;
			} // Of if

			tempNode = tempNode.next;
			tempCurrentPosition++;
		} // Of while

		return tempPosition;
	}// Of locate

	/**
	 *********************
	 * Insert a value to a position. If the list is already full, do nothing.
	 *
	 * @param paraPosition
	 *            The given position.
	 * @param paraValue
	 *            The given value.
	 * @return Success or not.
	 *********************
	 */
	public boolean insert(int paraPosition, int paraValue) {
		Node tempNode = header;
		Node tempNewNode;

		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next == null) {
				System.out.println("The position " + paraPosition + " is illegal.");
				return false;
			} // Of if

			tempNode = tempNode.next;
		} // Of for i

		// Construct a new node
		tempNewNode = new Node(paraValue);

		// Now link them.
		tempNewNode.next = tempNode.next;
		tempNode.next = tempNewNode;

		return true;
	}// Of insert

	/**
	 *********************
	 * Delete a value at a position.
	 *
	 * @param paraPosition
	 *            The given position.
	 * @return Success or not.
	 *********************
	 */
	public boolean delete(int paraPosition) {
		if (header.next == null) {
			System.out.println("Cannot delete element from an empty list.");
			return false;
		} // Of if

		Node tempNode = header;

		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next == null) {
				System.out.println("The position " + paraPosition + " is illeage.");
				return false;
			} // Of if

			tempNode = tempNode.next;
		} // Of for i

		tempNode.next = tempNode.next.next;

		return true;
	}// Of delete

	/**
	 *********************
	 * The entrance of the program.
	 *
	 * @param args
	 *            Not used now.
	 *********************
	 */
	public static void main(String args[]) {
		LinkedList tempFirstList = new LinkedList();
		System.out.println("Initialized, the list is: " + tempFirstList.toString());

		for (int i = 0; i < 5; i++) {
			tempFirstList.insert(0, i);
		} // Of for i

		System.out.println("Inserted, the list is: " + tempFirstList.toString());

		tempFirstList.insert(6, 9);

		tempFirstList.delete(4);

		tempFirstList.delete(2);
		System.out.println("Deleted, the list is: " + tempFirstList.toString());

		tempFirstList.delete(0);
		System.out.println("Deleted, the list is: " + tempFirstList.toString());

		for (int i = 0; i < 5; i++) {
			tempFirstList.delete(0);
			System.out.println("Looped delete, the list is: " + tempFirstList.toString());
		} // Of for i
	}// Of main
}// Of class LinkedList

输出: 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值