Java实现单链表基本操作

Java实现单链表的   插入头结点、删除头结点、在指定位置添加元素、在指定位置删除元素、根据数据删除元素、显示所有元素、根据位置查找元素、根据数值查找元素等。

public class Node {
	public int value;
	public Node next;

	public Node(int value) {
		this.value = value;
	}

	// 显示此节点
	public void display() {
		System.out.print(value + " ");
	}

}

public class LinkedList {

	public Node first;// 定义头结点
	public int pos = 0;// 节点的位置

	/**
	 * 构造方法
	 */
	public LinkedList() {
		this.first = null;
	}

	/**
	 * 插入头结点(头插法)
	 * 
	 * @param data
	 */
	public void addFirstNode(int data) {
		Node node = new Node(data);
		node.next = first;
		first = node;
	}

	/**
	 * 删除头结点
	 * 
	 * @return
	 */
	public Node deleteFirstNode() {

		Node tempNode = first;
		first = tempNode.next;
		return tempNode;
	}

	/**
	 * 在指定位置添加元素
	 * 
	 * @param index
	 * @param data
	 */
	public void add(int index, int data) {
		Node node = new Node(data);
		Node current = first;
		Node previous = first;
		while (pos != index) {
			previous = current;
			current = current.next;
			pos++;
		}
		node.next = current;
		previous.next = node;

		pos = 0;
	}

	/**
	 * 在指定位置删除元素
	 * 
	 * @param index
	 * @return
	 */
	public Node deleteByPos(int index) {
		Node current = first;
		Node previous = first;

		while (pos != index) {
			pos++;
			previous = current;
			current = current.next;
		}
		if (current == first) {
			first = first.next;
		} else {
			pos = 0;
			previous.next = current.next;
		}
		return current;

	}

	/**
	 * 根据数据删除元素
	 * 
	 * @param data
	 * @return
	 */
	public Node deleteByData(int data) {
		Node current = first;
		Node previous = first;
		while (current.value != data) {
			if (current.next == null) {
				return null;
			}
			previous = current;
			current = current.next;
		}
		if (current == first) {
			first = first.next;
		} else {
			previous.next = current.next;
		}

		return current;
	}

	/**
	 * 显示所有元素
	 */
	public void displayAllNode() {
		Node current = first;
		while (current != null) {
			current.display();
			current = current.next;
		}
		System.out.println();
	}

	/**
	 * 根据位置查找元素
	 * 
	 * @param index
	 * @return
	 */
	public Node findByPos(int index) {
		Node current = first;
		while (pos != index) {
			current = current.next;
			pos++;
		}
		pos = 0;
		return current;
	}

	/**
	 * 根据数值查找元素
	 * 
	 * @param data
	 * @return
	 */
	public Node findByData(int data) {
		Node current = first;
		while (current.value != data) {
			if (current.next == null) {
				return null;
			}
			current = current.next;
		}
		return current;
	}

	public static void main(String[] args) {
		LinkedList linkedList = new LinkedList();
		// 添加元素
		linkedList.addFirstNode(26);
		linkedList.addFirstNode(25);
		linkedList.addFirstNode(23);
		linkedList.addFirstNode(22);
		linkedList.addFirstNode(20);
		linkedList.displayAllNode();// 此时显示内容为 :20 22 23 25 26

		linkedList.add(1, 21);
		linkedList.add(4, 24);
		linkedList.displayAllNode();// 此时显示内容为 :20 21 22 23 24 25 26

		Node node = linkedList.deleteByPos(0);
		System.out.println("删除元素为:" + node.value);
		linkedList.displayAllNode();

		Node node2 = linkedList.deleteByData(21);
		System.out.println("删除元素为:" + node2.value);
		linkedList.displayAllNode();

		Node node3 = linkedList.deleteFirstNode();
		System.out.println("删除元素为:" + node3.value);
		linkedList.displayAllNode();

		Node node4 = linkedList.findByPos(0);
		System.out.println("查找到的元素为:" + node4.value);
		linkedList.displayAllNode();

		Node node5 = linkedList.findByData(24);
		System.out.println("查找到的元素为:" + node5.value);
		linkedList.displayAllNode();

	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值