Java实现单链表基本操作

来源:互联网 发布:c语言scanf 编辑:程序博客网 时间:2024/06/06 19:15

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 26linkedList.add(1, 21);linkedList.add(4, 24);linkedList.displayAllNode();// 此时显示内容为 :20 21 22 23 24 25 26Node 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();}}

原创粉丝点击