单向链表

本文详细介绍了单向链表的构造、搜索、删除、插入和反转操作,并通过实例展示了如何实现这些操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单向链表的构造:

public class LinkList {

	private Node headNode;

	public void createLinked(int[] a) {
		Node tailNode = new Node();
		headNode = tailNode;
		Node newNode;
		for (int i = 0; i < a.length; i++) {
			newNode = new Node();
			newNode.setValue(a[i]);
			tailNode.setNextNode(newNode);
			newNode.setNextNode(null);
			tailNode = newNode;
		}
	}

	public void searchLink(int value) {
		if (headNode == null) {
			return;
		}
		Node nextNode = headNode.getNextNode();
		while (nextNode != null) {
			if (nextNode.getValue() == value) {
				System.out.println("find the element");
				break;
			}
			nextNode = nextNode.getNextNode();
		}
		if (nextNode == null) {
			System.out.println("not found the element");
		}
	}

	public void delteLink(int value) {
		if (headNode == null) {
			return;
		}
		Node preNode = headNode;
		Node nextNode = headNode.getNextNode();
		while (nextNode != null) {
			if (nextNode.getValue() == value) {
				preNode.setNextNode(nextNode.getNextNode());
				System.out.println("delete the element success");
				break;
			}
			preNode = nextNode;
			nextNode = nextNode.getNextNode();
		}
		if (nextNode == null) {
			System.out.println("no found the delete element");
		}
	}

	public void insertLink(int pos, int value) {
		if (headNode == null) {
			return;
		}
		int count = 0;
		boolean isSuccess = false;
		Node currentNode = headNode;
		while (currentNode != null) {
			Node nextNode = currentNode.getNextNode();
			if (pos == count) {
				Node insertNode = new Node();
				insertNode.setValue(value);
				currentNode.setNextNode(insertNode);
				insertNode.setNextNode(nextNode);
				isSuccess = true;
				break;
			}
			count++;
			currentNode = nextNode;
		}
		if (isSuccess) {
			System.out.println("insert the element success");
		} else {
			System.out.println("insert the element failed");
		}
	}

	public void reverseLink() {
		if (headNode == null) {
			return;
		}
		Node preNode = headNode;
		Node currentNode = headNode.getNextNode();
		Node tempNode = currentNode;
		while (currentNode != null) {
			Node next = currentNode.getNextNode();
			currentNode.setNextNode(preNode);
			preNode = currentNode;
			currentNode = next;
		}
		tempNode.setNextNode(null);
		headNode.setNextNode(preNode);
	}

	public void displayLink() {
		if (headNode == null) {
			return;
		}
		Node nextNode = headNode.getNextNode();
		System.out.print("Head -> ");
		while (nextNode != null) {
			System.out.print(nextNode.getValue() + " -> ");
			nextNode = nextNode.getNextNode();
		}
		System.out.println("NULL");
	}

	public static void main(String[] args) {
		int a[] = new int[10];
		for (int i = 0; i < a.length; i++) {
			a[i] = i * 2;
		}
		LinkList list = new LinkList();
		list.createLinked(a);
		list.displayLink();
		// list.searchLink(20);
		// list.delteLink(0);
		// list.displayLink();
		// list.insertLink(0, 1);
		// list.displayLink();
		list.reverseLink();
		list.displayLink();
	}

}

class Node {
	private Node nextNode;
	private int value;

	public Node getNextNode() {
		return nextNode;
	}

	public void setNextNode(Node nextNode) {
		this.nextNode = nextNode;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值