Java实现循环单链表……

来源:互联网 发布:凡科怎么绑定域名 编辑:程序博客网 时间:2024/05/24 04:31

相比于C和C++,Java中没有了“结构”,因此就要使用类来描述“结构”,同样的,在实现链表时,就可以使用类来定义节点,然后进行相应的操作,以下是我实现循环单链表的代码,循环单链表和单链表的区别在于最后一个节点的判断,整体不难……

class CNode{    private Object data; //定义数据域    private CNode nextCNode; //定义下一个节点    private CNode head=null;//定义头节点    public Object getData() {        return data;    }    public void setData(Object data) {        this.data = data;    }    public CNode getNextCNode() {        return nextCNode;    }    public void setNextCNode(CNode next) {        this.nextCNode = next;    }    public void initCycleList(Object data){ //循环链表的初始化        head=new CNode();        head.setData(data);        head.setNextCNode(head);    }    public void insertCycleListTail(Object data){        CNode inCNode=new CNode();        inCNode.setData(data);        if (head==head.getNextCNode()) { //如果原链表只有一个节点,直接插入            head.setNextCNode(inCNode);            inCNode.setNextCNode(head);        }else {  //原链表不止一个节点            CNode temp=head; //创建临时节点            while (head!=temp.getNextCNode()) { //遍历循环链表,找到最后一个节点                temp=temp.getNextCNode();            }            temp.setNextCNode(inCNode); //插入节点            inCNode.setNextCNode(head);         }    }    //求循环链表的长度    public int cycleListSize(){        CNode temp=head;        int size=0;        while (temp.getNextCNode()!=head) {            size++;            temp=temp.getNextCNode();        }        return size;    }    //判断循环链表中是否存在某个元素    public Boolean isContain(Object data){        CNode temp=head;        while (temp.getNextCNode()!=head) {            if (temp.getData().equals(data)) {                return true;            }            temp=temp.getNextCNode();        }        return false;    }    //获取循环链表中第i个位置的元素    public CNode getCNode(int i){        if (i<0||i>cycleListSize()) {            System.out.println("输入有误");            return null;        }else {            int count=0;            CNode temp=head;            CNode retCNode=new CNode();            while (head!=temp.getNextCNode()) {                if (count==i) {                    retCNode.setData(temp.getData());                    break;                }                temp=temp.getNextCNode();                count++;            }            return retCNode;        }    }    //打印循环链表    public void printCycleList(){        CNode temp=head;        while (head!=temp.getNextCNode()) {            System.out.print(temp.getData()+" ");            temp=temp.getNextCNode();        }        System.out.println();    }}

以下是测试代码:

public class CycleList {    public static void main(String[] args){        CNode testCNode=new CNode();        testCNode.initCycleList(100);        testCNode.insertCycleListTail(0);        testCNode.insertCycleListTail(1);        testCNode.insertCycleListTail(2);        testCNode.insertCycleListTail(3);        testCNode.printCycleList();        System.out.println(testCNode.isContain(3));        System.out.println(testCNode.getCNode(2).getData());        System.out.println(testCNode.cycleListSize());    }}

以上仅仅是我个人对循环单链表的理解,有任何疏漏之处欢迎在评论区指明,谢谢!

标记为原创的博文均为本人辛苦码字所得,谢绝抄袭,转载请注明出处,新浪微博私信艾特:http://weibo.com/nieganghust

0 0
原创粉丝点击