双链表java实现

主要注意结构体的创建和双链表添加删除操作中结构图应用的改变。具体改变细节请看代码注释,代码不当之处希望各位加以提点,谢谢

/**
 * 
 * <p>
 * 功能描述:循环双链表
 * </p>
 * 
 * @author 钟良健
 * @company
 * @version V1.0
 */
public class util<T> {
	private Node<T> head;
	private int size;

	@SuppressWarnings("hiding")
	public class Node<T> {
		private Node<T> next;
		private Node<T> privious;
		private T value;

		public Node(Node<T> privious, Node<T> next, T value) {
			this.privious = privious;
			this.next = next;
			this.value = value;
		}
	}

	public util() {
		head = new Node<T>(null, null, null);
		head.privious = head.next = head;
		size = 0;
	}

	/**
	 * 
	 * 方法描述: 1、首先判断指定位置的节点是链表的前半段还是后半段 2、若为前半段则通过头节点的顺序查找 3、若为后半段则通过头节点的逆向查找
	 * 注意:1、默认头节点位置为0 2、注意for循环大小
	 * 
	 * @param index
	 * @return 指定位置的节点
	 * @author 钟良健
	 */
	public Node<T> getNodeByIndex(int index) {
		if (index == 0) {
			System.out.println(head);
			return head;
		}
		if (index < 0 || index > size) {
			throw new IndexOutOfBoundsException("超出查找范围");
		}
		Node<T> node = null;
		if (index < size / 2) {
			for (int i = 0; i < index; i++) {
				node = head.next;
			}
			System.out.println(node);
			return node;
		}
		for (int j = 0; j < size - index + 1; j++) {
			node = head.privious;
		}
		System.out.println(node);
		return node;
	}

	/**
	 * 
	 * 方法描述: 插入节点到指定位置节点处 1、首先根据插入节点的位置查找出原本位置的节点
	 * 2、new出一个新节点,新节点的前驱节点为原本位置节点的前驱节点,新节点的后驱节点为原本位置节点
	 * 3、原本位置节点的前驱节点的后驱节点指向新节点,原本位置的节点的前驱节点为新节点
	 * 
	 * @param index
	 *            指定位置
	 * @param t
	 *            节点值
	 * @author 钟良健
	 */
	public void insertNodeByIndex(int index, T t) {
		Node<T> oldPositionNode = getNodeByIndex(index);
		Node<T> newPositionNode = new Node<T>(oldPositionNode.privious,
				oldPositionNode, t);
		oldPositionNode.privious.next = newPositionNode;
		oldPositionNode.privious = newPositionNode;
		size++;
	}

	/**
	 * 
	 * 方法描述:
	 * 
	 * @param index
	 * @author 钟良健
	 */
	public void deleteNodeByIndex(int index) {
		Node<T> deleteNode = getNodeByIndex(index);
		deleteNode.privious.next = deleteNode.next;
		deleteNode.next.privious = deleteNode.privious;
		deleteNode = null;
		size--;
	}

	public void displaylist() {
		Node<T> node = head;
		for (int i = 0; i < size; i++) {
			node = node.next;
			System.out.println(node.value);
		}
	}

	public static void main(String[] args) {
		util<String> list = new util<String>();
		list.insertNodeByIndex(0, "1");
		list.insertNodeByIndex(0, "2");
		list.insertNodeByIndex(0, "3");
		list.insertNodeByIndex(0, "4");
		list.displaylist();
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以通过定义一个双链表类来实现双向链表。下面是一个示例的Java代码来实现一个双链表: ```java package DoubleLinkedListTwo; //定义一个节点类 class ListNode { int val; ListNode prev; ListNode next; public ListNode(int val) { this.val = val; } } //定义双链表类 public class DoubleLinkedList { private ListNode head; //头节点 //在链表末尾添加一个节点 public void addLast(int val) { ListNode newNode = new ListNode(val); if (head == null) { head = newNode; } else { ListNode cur = head; while (cur.next != null) { cur = cur.next; } cur.next = newNode; newNode.prev = cur; } } //打印双链表 public void display() { ListNode cur = head; while (cur != null) { System.out.print(cur.val + " "); cur = cur.next; } System.out.println(); } //清空链表 public void clear() { head = null; } } //测试双链表 public class TestDoubleLinkedList { public static void main(String[] args) { //创建一个双链表 DoubleLinkedList d = new DoubleLinkedList(); //在链表末尾添加元素 d.addLast(1); d.addLast(1); d.addLast(1); d.addLast(1); //打印链表 d.display(); //清空链表 d.clear(); d.display(); } } ``` 以上代码示例演示了如何使用Java代码实现一个双向链表,并在双链表中添加元素,并打印链表。你可以根据自己的需求进一步扩展这个双链表类,并在测试类中添加其他操作,如在指定位置插入元素、删除元素等。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Java 实现 双向链表](https://blog.csdn.net/m0_52066789/article/details/122342307)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值