Java链表的实现

一.链表类基本框架

我们要自己实现自己的链表。里面实际上就是内部类Node,它含有数据域以及next引用,另外就是一些方法了。

//一个大类
public class LinkedList {

     //内部节点类
	class Node {
		int data;
		Node next;
    //构造方法
		public Node(int paraValue) {

		}
	}

	Node header;
    
    //链表构造
	public LinkedList() {

	}// Of 
    
    //销毁链表的方法
	public void reset() {

	}

    //查出索引
	public int locate(int paraValue) {

	}// Of lacate

    //插入元素
	public boolean insert(int paraPosition, int paraValue) {
	
	}// Of insert

    //删除元素
	public boolean delete(int paraPosition) {

	}


}

二.细节化代码

当然里面的方法很多,加就是了。不同的人对链表使用情况不同,这里我就只举几个列子。索引位置从零开始,我面向程序员设计,这与用户矛盾的,这一点大家注意。

废话少说直接上代码:

package datastructure.list;

public class LinkedList {
	/**
	 * 
	 ****************************
	 * TODO 定义一个结点类型,数据域,指针域。
	 * 
	 * @author Chen Fan
	 * @version 1.0 time 2021年12月21日
	 ****************************
	 */
	class Node {
		int data;
		Node next;

		/**
		 * ******************** 这里是一个含参构造方法
		 * 
		 * @param paraValue
		 *********************
		 */
		public Node(int paraValue) {
			data = paraValue;
			next = null;
		}// Of construct
	}// Of class Node

	/**
	 * 下面进行构建链表,先创建一个头结点
	 */
	Node header;

	public LinkedList() {
		header = new Node(0);
	}// Of the first constructor

	/**
	 *************************************
	 * 重新 toString 函数,输出我们想得到的格式
	 ************************************* 
	 */
	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 while

		return resultString;
	}// Of toString

	/**
	 **************************************
	 * 重置链表,只需要将头结点置为空就好了, 后面断掉的部分java回收机制直接回收, 比C语言方便多了。
	 **************************************
	 */
	public void reset() {
		header.next = null;
	}// Of reset

	/**
	 * 
	 *********************
	 * TODO 找到了目标元素所在的位置,返回第一个出现的位置
	 * 
	 * @param paraValue
	 * @return -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++;// 位置+1继续找
		}

		return tempPosition;
	}// Of lacate

	/**
	 * 
	 *********************
	 * TODO 插入结点,按位置插入和头插法
	 * 
	 * @param paraPosition
	 * @param paraValue
	 * @return
	 *********************
	 */
	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

		/**
		 * 头插法
		 */
		tempNewNode = new Node(paraValue);// 加入的新节点

		tempNewNode.next = tempNode.next;// 进行链接
		tempNode.next = tempNewNode;// 是tempNode.next指向头结点

		return true;
	}// Of insert

	public boolean delete(int paraPosition) {
		// 判断表是否为空
		if (header.next == null) {
			System.out.println("Can not delete from an empty list");
			return false;
		} // Of if

		Node tempNode = header;
		/**
		 * 找出删除位置的前一个位置,并检查输出的位置是否合法
		 */
		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next.next == null) {
				System.out.println("The position " + paraPosition + " is illegal");
				return false;
			} // Of if

			tempNode = tempNode.next;// 标志位后移
		} // Of for i

		tempNode.next = tempNode.next.next;// 中间的结点断掉被回收
		return true;
	}

	/**
	 * 
	 *********************
	 * TODO 测试程序
	 * 
	 * @param args
	 *********************
	 */
	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
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值