java实现链表操作
来源:互联网 发布:淘宝电脑主机靠谱吗 编辑:程序博客网 时间:2024/05/16 10:06
java实现简单链表
线性表接口的抽象
package com.fsc.mylinkedlist;/** * 线性表操作接口 * @author fsc * * @param <T> */public interface ListInterface<T> {/** * 向表尾添加元素 */public boolean add(T newEntry);/** * 向表的指定位置添加元素 */public boolean add(T newEntry, int givenPosition);/** * 移除指定位置元素 */public T remove(int givenPosition);/** * 用新元素替换指定位置元素 */public T replace(T newEntry, int givenPosition);/** * 获取指定位置元素 */public T getEntry(int givenPosition);/** * 表中是否包含指定元素 */public boolean contains(T anEntry);/** * 表长 */public int getLength();/** * 表是否为空 */public boolean isEmpty();/** * 展示表 */public void display();}
为了插入和删除代码的一致性,引入了哨兵节点(也就是下面代码中出现的firstNode)。
下标的选择和数组的规则相同,都是从0开始。为了满足下标的规则,在代码中相应作出了调整,下面是代码
package com.fsc.mylinkedlist;public class LList<T> implements ListInterface<T> {private Node firstNode;private Node lastNode;private int length;public LList() {clear();}/** * 将链表置为初始状态 */public final void clear() {// firstNode为哨兵节点firstNode = new Node(null);lastNode = null;length = 0;}/** * 向链表尾部插入数据域 */@Overridepublic boolean add(T newEntry) {return add(newEntry, length);}/** * 向链表指定位置插入数据 position为0时代表向链表头插入元素(第一个元素) * * @param newEntry * @param position * @return */public boolean add(T newEntry, int position) {if (newEntry == null) {throw new IllegalArgumentException("数据域不能为null");}if (position < 0 || position > length) {throw new IllegalArgumentException("插入的位置不合法");}Node nextNode = new Node(newEntry);//向表尾插入的特殊情况处理表尾引用if(position == length){if(length == 0){firstNode.next = nextNode;lastNode = nextNode;}else{lastNode.next = nextNode;lastNode = nextNode;}}else{// 向currentNode后插入newNodeNode currentNode = getNodeAt(position);// 插入nextNode.next = currentNode.next;currentNode.next = nextNode;}length++;return true;}/** * 删除指定位置的节点 传入0代表删除表头节点 * 链表是从0开始计数 */@Overridepublic T remove(int givenPosition) {if (givenPosition < 0 || givenPosition >= length) {throw new IllegalArgumentException("删除的位置不合法");}Node currentNode = getNodeAt(givenPosition);Node result = currentNode.next;currentNode.next = currentNode.next.next;return result.data;}@Overridepublic T replace(T newEntry, int givenPosition) {if (newEntry == null) {throw new IllegalArgumentException("数据域不能为null");}if (givenPosition < 0 || givenPosition >= length) {throw new IllegalArgumentException("替换的位置不合法");}//要替换节点之前的节点Node currentNode = getNodeAt(givenPosition);T result = currentNode.next.data;currentNode.next.data = newEntry;return result;}@Overridepublic T getEntry(int givenPosition) {if (givenPosition < 0 || givenPosition >= length) {throw new IllegalArgumentException("传入的位置不合法");}Node currentNode = getNodeAt(givenPosition);return currentNode.next.data;}@Overridepublic boolean contains(T anEntry) {if (anEntry == null) {throw new IllegalArgumentException("数据域不能为null");}boolean result = false;Node currentNode = firstNode.next;while(currentNode != null){T data = currentNode.data;if(anEntry.equals(data)){result = true;break;}currentNode = currentNode.next;}return result;}/** * 从链表的头节点开始打印链表的数据域 */@Overridepublic void display() {StringBuilder sb = new StringBuilder();Node currentNode = firstNode.next;while (currentNode != null) {sb.append(currentNode.data);currentNode = currentNode.next;if (currentNode != null) {sb.append("->");}}System.out.println(sb.toString());}@Overridepublic int getLength() {return length;}@Overridepublic boolean isEmpty() {return length == 0;}/** * 获取指定位置的节点 节点的计数从0开始,getNodeAt(0)代表获取firstNode(哨兵节点) * * @param givenPosition * @return 若要找的位置不存在节点返回null */private Node getNodeAt(int givenPosition) {// 从头节点向后找Node currentNode = firstNode;for (int i = 0; i <= length; ++i) {if (givenPosition == i) {return currentNode;}currentNode = currentNode.next;}return null;}/** * 节点类 * * @author fsc * */private class Node {private T data;private Node next;public Node(T data) {this(data, null);}public Node(T data, Node next) {this.data = data;this.next = next;}}}
0 0
- java实现链表操作
- java实现双向链表的操作
- 链表、反转链表操作,Java实现
- 链表基本操作java实现
- java实现双向链表操作
- java实现链表以及相关操作
- java实现链表及其相关操作
- JAVA实现链表的基本操作
- 链表的常用操作(java实现)
- Java实现--链表基本操作
- java实现链表基本操作
- java实现链表各种操作
- 单向链表的一些操作实现-Java实现
- java泛型实现链表基本操作
- 链表基础操作与常见题目Java实现
- 链表及其基本操作的JAVA实现。
- 数据结构之链表的增删操作的Java实现
- Java实现双向链表的基本操作
- C#的认识
- VR冷思考
- 冒泡排序和选择排序
- XX转Bitmap,XX转Drawable
- Handler消息处理机制--原理分析
- java实现链表操作
- android源码定制之--入门篇--android源码目录结构
- linux 线程栈
- 解决UIToolbar 顶部的黑色线条问题
- ibm ds5020 for aix storage manager .
- AdapterViewFlipper 和 StackView的使用
- [java][log4j]Log4j每天、每小时、每分钟定时生成日志文件
- 如何逃脱老鼠奔跑试的工作?
- struts+Hibernate小程序的坑我