java中双向链表的增、删、查操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | import java.util.NoSuchElementException; public class DoublyLinkedListImpl<E> { private Node head; // sentinel before first item private Node tail; // sentinel after last item private int size; // number of elements on list public DoublyLinkedListImpl() { size = 0 ; } /** * this class keeps track of each element information * * @author java2novice * */ private class Node { E element; Node next; Node prev; public Node(E element, Node next, Node prev) { this .element = element; this .next = next; this .prev = prev; } } /** * returns the size of the linked list * * @return */ public int size() { return size; } /** * return whether the list is empty or not * * @return */ public boolean isEmpty() { return size == 0 ; } /** * adds element at the starting of the linked list * * @param element */ public void addFirst(E element) { Node tmp = new Node(element, head, null ); if (head != null ) { head.prev = tmp; } // 跟原来的头节点交换位置 head = tmp; if (tail == null ) { tail = tmp; } // 首次添加节点的时候,头节点就是尾节点 size++; System.out.println( "adding: " + element); } /** * adds element at the end of the linked list * * @param element */ public void addLast(E element) { Node tmp = new Node(element, null , tail); if (tail != null ) { tail.next = tmp; } // 跟原来的尾节点交换位置 tail = tmp; if (head == null ) { head = tmp; } // 首次添加节点的时候,头节点就是尾节点 size++; System.out.println( "adding: " + element); } /** * get element at the specified location of the linked list * * @param loc */ public Node getElement( int loc) { Node tmp = head; int index = 0 ; while (loc != index) { tmp = tmp.next; index++; } return tmp; } /** * add element at the specified location of the linked list * * @param element * @param loc */ public void insertAfterElement(E element, int loc) { if (loc == size) { addLast(element); } if (loc == 0 ) { addFirst(element); } Node preEle = getElement(loc); Node nextEle = preEle.next; Node tmp = new Node(element, null , preEle); // preEle=tmp.prev;//这一行没必要,因为创建类的时候已经指定preEle preEle.next = tmp; nextEle.prev = tmp; tmp.next = nextEle; size++; System.out.println( "inserting: " + element); } /** * this method walks forward through the linked list */ public void iterateForward() { System.out.println( "iterating forward.." ); Node tmp = head; while (tmp != null ) { System.out.println(tmp.element); tmp = tmp.next; } } /** * this method walks backward through the linked list */ public void iterateBackward() { System.out.println( "iterating backword.." ); Node tmp = tail; while (tmp != null ) { System.out.println(tmp.element); tmp = tmp.prev; } } /** * this method removes element from the start of the linked list * * @return */ public E removeFirst() { if (size == 0 ) throw new NoSuchElementException(); Node tmp = head; head = head.next; head.prev = null ; size--; System.out.println( "deleted: " + tmp.element); return tmp.element; } /** * this method removes element from the end of the linked list * * @return */ public E removeLast() { if (size == 0 ) throw new NoSuchElementException(); Node tmp = tail; tail = tail.prev; tail.next = null ; size--; System.out.println( "deleted: " + tmp.element); return tmp.element; } /** * removes element from the specified location of the linked list * * @return */ public E removeByIndex( int loc) { Node tmp = null ; if (loc >= size || loc < 0 ) { throw new NoSuchElementException(); } else if (loc == 0 ) { removeFirst(); } else if (loc == size - 1 ) { removeLast(); } else { tmp = getElement(loc); Node preEle = tmp.prev; Node nextEle = tmp.next; preEle.next = nextEle; nextEle.prev = preEle; size--; System.out.println( "deleted: " + tmp.element); return tmp.element; } return null ; } public static void main(String a[]) { DoublyLinkedListImpl<Integer> dll = new DoublyLinkedListImpl<Integer>(); dll.addFirst( 10 ); dll.addFirst( 34 ); dll.addLast( 56 ); dll.addLast( 364 ); dll.insertAfterElement( 88 , 2 ); dll.insertAfterElement( 99 , 3 ); dll.iterateForward(); dll.removeByIndex( 4 ); dll.iterateBackward(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?