java双向链表实现迭代_Java实现一个双向链表的倒置功能

双向链表类代码:public class DoubleLinkedList {

transient Node first; //头节点

transient Node last; //尾节点

transient int size = 0; //节点个数

public DoubleLinkedList() {

}

/**

* 判断是否为空

*

* @return true 为空, false 不为空

*/

public boolean isEmpty() {

return 0 == size;

}

/**

* 获取表元素个数

*

* @return 表链元素个数

*/

public int size() {

return size;

}

/**

* 反序排列列表中的值

*/

public void reverse() {

if (isEmpty()) {

return;

}

// 表的头节点与尾节点互换位置

Node temp = first;

first = last;

last = temp;

// 从表头按序遍历 更每个新节点的前后节点互换

Node n = first;

while (n != null) {

temp = n.next;

n.next = n.per;

n.per = temp;

n = n.next;

}

}

/**

* 根据索引获取元素

*

* @param index 元素

* @return

*/

public E get(int index) {

checkIndex(index);

return getNode(index).item;

}

/**

* 将元素追加到列表的末尾

*

* @param item 元素

*/

public void add(E item) {

Node l = last;

Node newNode = new Node(l, item, null);

last = newNode;

if (l == null) {

first = newNode;

} else {

l.next = newNode;

}

size++;

}

/**

* 根据索引获取节点

*

* @param index 索引

* @return 节点

*/

private Node getNode(int index) {

if (index < (size >> 2)) { //如果索引在列表前面 正序遍历

Node node = first;

for (int i = 0; i < index; i++) {

node = node.next;

}

return node;

} else { //否则倒叙遍历

Node node = last;

for (int i = size - 1; i > index; i--) {

node = node.per;

}

return node;

}

}

/**

* 检查索引值

*

* @param index 索引

*/

private void checkIndex(int index) {

if (index < 0 || index >= size) {

throw new IndexOutOfBoundsException();

}

}

/**

* 节点元素

*

* @param

*/

private class Node {

E item;//元素的值

Node per;// 前一个结点

Node next;// 后一个结点

public Node(Node per, E item, Node next) {

this.item = item;

this.per = per;

this.next = next;

}

public String toString() {

return item + "";

}

}

/**

* 自定义个一个列表迭代器

*

* @return

*/

public Iterator iterator() {

return new Itr();

}

private class Itr implements Iterator {

int cursor = 0;

int lastRet = -1;

public boolean hasNext() {

return cursor != size();

}

public E next() {

try {

E next = get(cursor);

lastRet = cursor++;

return next;

} catch (IndexOutOfBoundsException e) {

throw new NoSuchElementException();

}

}

public void remove() {

if (-1 == lastRet) {

throw new IllegalStateException();

}

throw new RuntimeException("remove is denied");

}

}

}

下面在创建一个测试用例:public class DoubleLinkedListTest {

/**

* 链表打印

*

* @param list

*/

public static void printDoubleLinkedList(DoubleLinkedList list) {

StringBuffer sb = new StringBuffer();

Iterator it = list.iterator();

while (it.hasNext()) {

sb.append(it.next());

if(it.hasNext()) {

sb.append("->");

}

}

System.out.println(sb.toString());

}

public static void main(String[] args) {

DoubleLinkedList list = new DoubleLinkedList();

list.add(1);

list.add(2);

list.add(3);

System.out.println("链表倒置前:");

printDoubleLinkedList(list);

list.reverse();

System.out.println("链表倒置后:");

printDoubleLinkedList(list);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值