栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我可以使用java.util.LinkedList构造循环/循环链接列表吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我可以使用java.util.LinkedList构造循环/循环链接列表吗?

class ListNode {    public ListNode next;    public Object data;    public ListNode(Object data, ListNode next) {        this.next = next;        this.data = data;    }}class CircularlinkedList {    private ListNode head = null;    private int numberOfElements = 0;    private ListNode actualElement = null;    private int index = 0;    public boolean isEmpty() {        return (numberOfElements == 0);    }    public int getNumberOfElements() {        return numberOfElements;    }    public void insertFirst(Object data) {        if (!(isEmpty())) { index++;        }        ListNode listNode = new ListNode(data, head);        head = listNode;        numberOfElements++;    }    public void insertAfterActual(Object data) {        ListNode listNode = new ListNode(data, actualElement.next);        actualElement.next = listNode;        numberOfElements++;    }    public boolean deleteFirst() {        if (isEmpty()) return false;        if (index > 0) index--;        head = head.next;        numberOfElements--;        return true;    }    public boolean deleteActualElement() {        if (index > 0) { numberOfElements--; index--; ListNode listNode = head; while (listNode.next.equals(actualElement) == false)     listNode = listNode.next; listNode.next = actualElement.next; actualElement = listNode; return true;        }        else { actualElement = head.next; index = 0; return deleteFirst();        }    }    public boolean goTonextElement() {        if (isEmpty()) return false;        index = (index + 1) % numberOfElements;        if (index == 0) actualElement = head;        else actualElement = actualElement.next;        return true;    }    public Object getActualElementData() {        return actualElement.data;    }    public void setActualElementData(Object data) {        actualElement.data = data;    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/453742.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号