[java数据结构]--java双向链表LinkedList的简单实现
2017-02-28 20:54
856 查看
前言
数据结构在java中的集合中,有很多数据结构的实现.例如:线性结构中的:线性表(LinkedList),栈(Stack),队列(Queue)
代码实现和解析
1.LinkList自定义实现
/** * 双向链表 的增删改查 * @author ccj * */ public class DoubleLinkList<E> { //头结点 private Node<E> first; //尾结点 private Node<E> last; //大小 private int size; public int getSize() { return size; } // public DoubleLinkList(){ first=null; last=null; } /** * 判断是否为空 */ public boolean isEmpty(){ if(size==0){ return true; }else{ return false; } } /** * 插入节点,在头结点后插入 */ public void insertFirst(E e){ //取链表头结点 final Node<E> f = first; //新建节点 前驱为null,后继为头结点 Node<E> node=new Node<E>(null,e,f); //将新的node作为头结点 first=node; //如果原来的头结点为null,就说明链表为空,既头结点和尾结点都为同一个, //如果存在,就把node作为旧的头指针的前驱 if (f == null) last = node; else f.prev = node; size++; first=node; } /** * 插入节点,在尾结点后插入 * 类似于头结点插入 */ void linkLast(E e) { final Node<E> l = last; final Node<E> newNode = new Node<>(l, e, null); last = newNode; if (l == null) first = newNode; else l.next = newNode; size++; } /** * 根据下标得到元素 * @param index * @return */ Node<E> getObject(int index){ //size >> 1 表示 二进制右移一位 同 size/2 //前半部分时 从前向后找,后半部分时,从后向前找,这样效率高 if(index>=size){ throw new IndexOutOfBoundsException(); } if (index < (size >> 1)) { Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } } /** * 删除节点 */ public int deleteFirst(){ Node<E> tmp=first; if(tmp==null){ throw new NoSuchElementException(); } //解除绑定 Node<E> next=tmp.next; first = next; if(next==null){ last=null; }else{ next.prev=null; } size--; return 0; } /** * 更改结点 */ public void set(int index,E object){ checkElementIndex(index); Node<E> node=getObject(index); node.item=object; } private void checkElementIndex(int index) { if (index>=size) throw new IndexOutOfBoundsException(); } public void display(){ Node current=first; while(current!=null){ current.display(); current=current.next; } } static class Node<E> { E item; //后继 Node<E> next; //前驱 Node<E> prev; /** * * @param prev * @param element * @param next */ Node(Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev; } /** * 显示 */ public void display(){ System.out.println("data is "+item); } } }
输出
--增加--- data is 3 data is 2 data is 1 --删除--- data is 2 data is 1 --改--- data is 0 data is 1 --查看--- data is 1 --大小为-- 2
相关文章推荐
- 数据结构: Java中LinkedList的简单实现
- 简单实现 linkedList 双向链表
- Data structure-4 双向链表 DoubleLinkedList--Java语言实现
- Java基础课程学习总结,使用LinkedList简单模拟队列数据结构和堆栈数据结构的实现
- (8) Java源码分析 ---- LinkedList (对应数据结构中线性表中的双向循环链表,JDK1.6)
- 数据结构(Java语言)——LinkedList简单实现
- 使用java实现双向链表数据结构
- java数据结构之实现双向链表的示例
- Java数据结构之简单链表的定义与实现方法示例
- 双向链表的C++实现 Implement of Doubly Linked List
- java 利用 LinkedList类实现 数据结构 栈.......
- 【LeetCode-面试算法经典-Java实现】【141-Linked List Cycle(单链表中有环)】
- 【LeetCode-面试算法经典-Java实现】【142-Linked List Cycle II(单链表中有环II)】
- Java数据结构-线性表之单链表LinkedList
- java 实现双向链表(数据结构)
- 简单数据结构之双向链表(C++实现)
- 数据结构(LinkedList的java实现)
- 数据结构--java实现双向链表
- java与数据结构(4)---java实现双向循环链表
- java 利用 LinkedList类实现 数据结构 栈.......