(三)循环链表以及循环链表应用

摘自:http://blog.csdn.net/fangfully/article/details/12293177

单向循环链表

单向循环链表是单链表的另一种形式,其结构特点是链表中最后一个结点的指针不再是结束标记,而是指向整个链表的第一个结点,从而使单链表形成一个环。和单链表相比,循环单链表的长处是从链尾到链头比较方便。当要处理的数据元素序列具有环型结构特点时,适合于采用循环单链表。

与单链表相同,循环单链表也有带头结点结构和不带头结点结构两种,带头结点的循环单链表实现插入和删除操作时,算法实现较为方便。
带头结点的循环单链表结构如下:


带头结点的循环单链表的操作实现方法和带头结点的单链表的操作实现方法类同,差别仅在于:

(1)在构造函数中,要加一条head.next = head 语句,把初始时的带头结点的循环单链表设计成图2-11 (a)所示的状态。

(2)在index(i)成员函数中,把循环结束判断条件current != null改为current != head。(见上篇博客)

[java]  view plain copy
  1. public interface List {  
  2.     // 获得线性表长度  
  3.     public int size();  
  4.   
  5.     // 判断线性表是否为空  
  6.     public boolean isEmpty();  
  7.   
  8.     // 插入元素  
  9.     public void add(int index, Object obj) throws Exception;  
  10.   
  11.     // 删除元素  
  12.     public void delete(int index) throws Exception;  
  13.   
  14.     // 获取指定位置的元素  
  15.     public Object get(int index) throws Exception;  
  16. }  
[java]  view plain copy
  1. <pre name="code" class="java">//节点类  
  2. public class Node {  
  3.     Object element; // 数据域  
  4.     Node next; // 指针域  
  5.     //头结点的构造方法  
  6.     public Node(Node nextval){  
  7.         this.next=nextval;  
  8.     }  
  9.     //非头结点的构造方法  
  10.     public Node(Object obj,Node nextval){  
  11.         this.element=obj;  
  12.         this.next=nextval;  
  13.     }  
  14.     public Object getElement() {  
  15.         return element;  
  16.     }  
  17.     public void setElement(Object element) {  
  18.         this.element = element;  
  19.     }  
  20.     public Node getNext() {  
  21.         return next;  
  22.     }  
  23.     public void setNext(Node next) {  
  24.         this.next = next;  
  25.     }  
  26.     public String toString()  
  27.     {  
  28.        return this.element.toString();    
  29.     }  
  30. }</pre>  
[java]  view plain copy
  1. public class CycleLinkedList implements List {  
  2.     Node head; // 头指针  
  3.     Node current;// 当前结点对象  
  4.     int size;// 结点个数  
  5.     public CycleLinkedList(){  
  6.         //初始化头结点,让头指针指向头结点。并且让当前结点对象等于头结点。  
  7.         this.head = current = new Node(null);  
  8.         this.size =0;//单向链表,初始长度为零。  
  9.         this.head.next =this.head;  
  10.     }  
  11.     //定位函数,实现当前操作对象的前一个结点,也就是让当前结点对象定位到要操作结点的前一个结点。  
  12.     public void index(int index) throws Exception{  
  13.         if(index<-1||index>this.size-1){  
  14.             throw new Exception("参数错误!");  
  15.         }  
  16.         if(index==-1){  
  17.             return;  
  18.         }  
  19.         this.current=this.head.next;  
  20.         int j=0;  
  21.         while(this.current!=this.head&&j<index){  
  22.             this.current=this.current.next;  
  23.             j++;  
  24.         }  
  25.     }  
  26.     @Override  
  27.     public int size() {  
  28.         // TODO Auto-generated method stub  
  29.         return this.size;  
  30.     }  
  31.   
  32.     @Override  
  33.     public boolean isEmpty() {  
  34.         // TODO Auto-generated method stub  
  35.         return this.size == 0;  
  36.     }  
  37.   
  38.     @Override  
  39.     public void add(int index, Object obj) throws Exception {  
  40.         // TODO Auto-generated method stub  
  41.         if (index < 0 || index > this.size) {  
  42.             throw new Exception("参数错误!");  
  43.         }  
  44.         this.index(index - 1);  
  45.         this.current.setNext(new Node(obj, this.current.next));  
  46.         this.size++;  
  47.     }  
  48.   
  49.     @Override  
  50.     public void delete(int index) throws Exception {  
  51.         // TODO Auto-generated method stub  
  52.         // 判断链表是否为空  
  53.         if (isEmpty()) {  
  54.             throw new Exception("链表为空,无法删除!");  
  55.         }  
  56.         if (index < 0 || index > size) {  
  57.             throw new Exception("参数错误!");  
  58.         }  
  59.         this.index(index-1);  
  60.         this.current.setNext(this.current.next.next);  
  61.         this.size--;  
  62.     }  
  63.   
  64.     @Override  
  65.     public Object get(int index) throws Exception {  
  66.         // TODO Auto-generated method stub  
  67.         if(index <-1 || index >size-1)  
  68.         {  
  69.             throw new Exception("参数非法!");  
  70.         }  
  71.         this.index(index);  
  72.           
  73.         return this.current.getElement();  
  74.     }  
  75.   
  76. }  

双向循环链表

双向链表是每个结点除后继指针外还有一个前驱指针。和单链表类同,双向链表也有带头结点结构和不带头结点结构两种,带头结点的双向链表更为常用;另外,双向链表也可以有循环和非循环两种结构,循环结构的双向链表更为常用。
在双向链表中,每个结点包括三个域,分别是element域、next域和prior域,其中element域为数据元素域,next域为指向后继结点的对象引用,prior域为指向前驱结点的对象引用。下图为双向链表结点的图示结构。

如下图是带头结点的循环双向链表的图示结构。循环双向链表的next和prior各自构成自己的循环单链表。

在双向链表中,有如下关系:设对象引用p表示双向链表中的第i个结点,则p.next表示第i+1个结点,p.next.prior仍表示第i个结点,即p.next.prior== p;同样地,p.prior表示第i-1个结点,p.prior.next仍表示第i个结点,即p.prior.next ==p。下图是双向链表上述关系的图示。 

循环双向链表的插入过程如下图所示。图中的指针p表示要插入结点的位置,s表示要插入的结点,①、②、③、④表示实现插入过程的步骤。

循环双向链表的删除过程如下图所示。图中的指针p表示要插入结点的位置,①、②表示实现删除过程的步骤。

[java]  view plain copy
  1. public interface List {  
  2.     // 获得线性表长度  
  3.     public int size();  
  4.   
  5.     // 判断线性表是否为空  
  6.     public boolean isEmpty();  
  7.   
  8.     // 插入元素  
  9.     public void add(int index, Object obj) throws Exception;  
  10.   
  11.     // 删除元素  
  12.     public void delete(int index) throws Exception;  
  13.   
  14.     // 获取指定位置的元素  
  15.     public Object get(int index) throws Exception;  
  16. }  
[java]  view plain copy
  1. public class Node {  
  2.     Object element; // 数据域  
  3.     Node next; // 后继指针域  
  4.     Node prior; // 前驱指针域  
  5.   
  6.     Node(Node nextval){  
  7.         this.next=nextval;  
  8.     }  
  9.     Node(Object obj,Node nextval){  
  10.         this.next=nextval;  
  11.         this.element=obj;  
  12.     }  
  13.     // 获得当前结点的后继结点  
  14.     public Node getNext() {  
  15.         return this.next;  
  16.     }  
  17.   
  18.     // 获得当前结点的前驱结点  
  19.     public Node getPrior() {  
  20.         return this.prior;  
  21.     }  
  22.   
  23.     // 获得当前的数据域的值  
  24.     public Object getElement() {  
  25.         return this.element;  
  26.     }  
  27.   
  28.     // 设置当前结点的后继指针域  
  29.     public void setNext(Node nextval) {  
  30.         this.next = nextval;  
  31.     }  
  32.   
  33.     // 设置当前结点的前驱指针域  
  34.     public void setPrior(Node priorval) {  
  35.         this.prior = priorval;  
  36.     }  
  37.   
  38.     // 设置当前结点的数据域  
  39.     public void setElement(Object obj) {  
  40.         this.element = obj;  
  41.     }  
  42.   
  43.     public String toString() {  
  44.         return this.element.toString();  
  45.     }  
  46. }  
[java]  view plain copy
  1. public class DoubleCycleLinkedList implements List {  
  2.     Node head; // 头指针  
  3.     Node current;// 当前结点对象  
  4.     int size;// 结点个数  
  5.     public DoubleCycleLinkedList(){  
  6.         //初始化头结点,让头指针指向头结点。并且让当前结点对象等于头结点。  
  7.         this.head = current = new Node(null);  
  8.         this.size =0;  
  9.         this.head.next =this.head;  
  10.         this.head.prior=this.head;  
  11.     }  
  12.     //定位函数,实现当前操作对象的前一个结点,也就是让当前结点对象定位到要操作结点的前一个结点。  
  13.     public void index(int index) throws Exception{  
  14.         if(index<-1||index>this.size-1){  
  15.             throw new Exception("参数错误!");  
  16.         }  
  17.         if(index==-1){  
  18.             return;  
  19.         }  
  20.         this.current=this.head.next;  
  21.         int j=0;  
  22.         while(this.current!=this.head&&j<index){  
  23.             this.current=this.current.next;  
  24.             j++;  
  25.         }  
  26.     }  
  27.     @Override  
  28.     public int size() {  
  29.         // TODO Auto-generated method stub  
  30.         return this.size;  
  31.     }  
  32.   
  33.     @Override  
  34.     public boolean isEmpty() {  
  35.         // TODO Auto-generated method stub  
  36.         return this.size == 0;  
  37.     }  
  38.   
  39.     @Override  
  40.     public void add(int index, Object obj) throws Exception {  
  41.         // TODO Auto-generated method stub  
  42.         if (index < 0 || index > this.size) {  
  43.             throw new Exception("参数错误!");  
  44.         }  
  45.         this.index(index - 1);  
  46.         this.current.setNext(new Node(obj, this.current.next));  
  47.         this.current.next.setPrior(this.current);  
  48.         this.current.next.next.setPrior(this.current.next);  
  49.         this.size++;  
  50.     }  
  51.   
  52.     @Override  
  53.     public void delete(int index) throws Exception {  
  54.         // TODO Auto-generated method stub  
  55.         // 判断链表是否为空  
  56.         if (isEmpty()) {  
  57.             throw new Exception("链表为空,无法删除!");  
  58.         }  
  59.         if (index < 0 || index > size) {  
  60.             throw new Exception("参数错误!");  
  61.         }  
  62.         this.index(index-1);  
  63.         this.current.setNext(this.current.next.next);  
  64.         this.current.next.setPrior(current);  
  65.         this.size--;  
  66.     }  
  67.   
  68.     @Override  
  69.     public Object get(int index) throws Exception {  
  70.         // TODO Auto-generated method stub  
  71.         if(index <-1 || index >size-1)  
  72.         {  
  73.             throw new Exception("参数非法!");  
  74.         }  
  75.         this.index(index);  
  76.           
  77.         return this.current.getElement();  
  78.     }  
  79. }  
循环链表应用
游戏规则:N个人围成一个圈,从第一个人开始传花,当数到M时,该人退出游戏,直到剩下最后一个人。
[java]  view plain copy
  1. //游戏类  
  2. public class Game {  
  3.   
  4.     //单向循环链表  
  5.     CycleLinkedList list = new CycleLinkedList();  
  6.     //总人数  
  7.     int num;  
  8.     //数到几退出  
  9.     int key;  
  10.       
  11.     //游戏初始化方法  
  12.     public Game(int num,int key)  
  13.     {  
  14.        this.num = num;  
  15.        this.key = key;  
  16.     }  
  17.       
  18.     public void play() throws Exception  
  19.     {  
  20.        for(int i=0;i<num;i++)  
  21.        {  
  22.            list.add(i, i);    
  23.        }  
  24.          
  25.        System.out.println("\n-------游戏开始之前---------\n");  
  26.        for(int i=0;i<list.size();i++)  
  27.        {  
  28.            System.out.print(list.get(i)+" ");  
  29.        }  
  30.        System.out.println("\n-------游戏开始---------\n");  
  31.        int iCount=num; //开始等于总人数num  
  32.        int j=0//累加器,计算是否能被key整除。  
  33.          
  34.        Node node = list.head;  
  35.        while(iCount!=1)  
  36.        {  
  37.           if(node.getElement()!=null&& Integer.parseInt(node.getElement().toString())!=-1)  
  38.           {  
  39.             j++;    
  40.             if(j%key==0)  
  41.             {  
  42.                 node.setElement(-1);  
  43.                 iCount--;  
  44.                 System.out.println();  
  45.                 for(int i=0;i<list.size();i++)  
  46.                 {  
  47.                    System.out.print(list.get(i)+" ");  
  48.                 }  
  49.             }  
  50.           }   
  51.           node = node.next;  
  52.        }  
  53.        System.out.println("\n-------游戏结束---------\n");  
  54.        for(int i=0;i<list.size();i++)  
  55.        {  
  56.            System.out.print(list.get(i)+" ");  
  57.        }  
  58.     }  
  59.       
  60. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值