java双向链表的实现(部分功能)
2009-07-23 21:38
591 查看
]public class DoubleLinkedList { private Node head; private Node tail; private int size; public DoubleLinkedList() { head = new Node(); tail = head; size = 0; } private class Node { public Object data; public Node next; public Node front; Node() { this.data = null; } Node(Object data) { this.data = data; } } //在链表指定的位置插入指定的元素,后插。index从0 到 size ,为0时加在head的后面 public void add(int index, Object element) { if(index > size || index < 0) { throw new IndexOutOfBoundsException("index:" + index); } Node newNode = new Node(element); Node p = head; for(int i=0; i<index; i++) { p = p.next; } if(p.next != null){//若p不为最后一个节点 newNode.front = p; newNode.next = p.next; p.next.front = newNode; p.next = newNode; }else{ //若p为最后一个节点 p.next = newNode; newNode.front = p.front; tail = newNode; } size ++; } //在链表的最后加入指定的元素 public void add(Object element) { add(size, element); } //获取指定位置的元素 public Object get(int index) { if(index >size || index<0) { throw new IndexOutOfBoundsException("index:" + index); } Node p = head; int i = 0; while(p != tail) { p = p.next; if(i == index) { return p.data; } i++; } return null; } //清空列表 public void clear() { head = null; tail = null; size = 0; } public static void main(String[] args) { DoubleLinkedList list = new DoubleLinkedList(); for(int i=0; i<10; i++) { list.add(i); } for(int i=0; i<list.size; i++) { System.out.print(list.get(i) + " "); } } }
相关文章推荐
- JAVA实现双向链表的增删功能
- JAVA实现双向链表的增删功能的方法
- JAVA实现双向链表的增删功能
- Java自己实现双向链表LinkList
- JAVA实现双向链表
- 双向链表的Java实现,以及相关函数的实现
- java双向循环链表的实现代码
- 用java实现双向循环链表的增删改查
- 双向链表-数据结构-java实现
- 把二元查找树转变成排序的双向链表——Java实现
- Java模拟单向链表和双向链表的实现
- 双向链表 java 实现 LinkedList
- java 链表实现增删改查功能
- Java实现双向链表(两个版本)
- Java语言中链表和双向链表的实现
- Java语言中链表和双向链表的实现
- (转)java实现基本数据结构(堆,栈,链表)——推荐后面的链表扩展部分
- 【老鸟学算法】二元查找树转变成排序的双向链表——算法思想及java实现
- JAVA实现双向链表
- 单链表与双向链表的Java实现