春風(fēng)博客

          春天里,百花香...

          導(dǎo)航

          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          統(tǒng)計

          公告

          MAIL: junglesong@gmail.com
          MSN: junglesong_5@hotmail.com

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          輪圈數(shù)數(shù)退出問題

          package com.sitinspring.roundtable;

          /** *//**
           * 循環(huán)鏈表節(jié)點類
           * 
          @author: sitinspring(junglesong@gmail.com)
           * @date: 2008-7-1-下午10:42:49
           * 
          @param <T>
           
          */

          class Node{
              
          protected String name;
              
          protected boolean isQuited;
              
          protected Node next;
          }


          /** *//**
           * 循環(huán)鏈表類,用于解決輪圈數(shù)數(shù)退出問題的建模
           * 
          @author: sitinspring(junglesong@gmail.com)
           * @date: 2008-7-1-下午10:42:37
           * 
          @param <T>
           
          */

          public class CircleChainList{
              
          // 頭節(jié)點
              private Node first;
              
              
          // 人個數(shù)
              private int length;
              
              
          /**
               * 輪圈退出
               * 
          @param interval 間隔或起始數(shù)
               
          */

              
          public void wheelOut(int interval){
                  
          // 總節(jié)點數(shù)
                  int n=length;
                  
                  
          // 當(dāng)前輪到的下標(biāo)
                  int currentIndex=interval;
                  
                  
          // 當(dāng)前節(jié)點
                  Node currentNode=first;
                  
                  
          // 輪圈退出直到剩下最后一個人
                  while(n>1)
                      
          // 經(jīng)過一個未推出節(jié)點即下標(biāo)減一
                      if(currentNode.isQuited==false){                  
                          currentIndex
          --;                
                      }
            
                      
                      
          // 走向下一個節(jié)點
                      currentNode=currentNode.next;
                      
                      
          // 當(dāng)下標(biāo)為0及當(dāng)前節(jié)點未退出時讓其退出
                      if(currentIndex==0 && currentNode.isQuited==false){
                          currentNode.isQuited
          =true
                          System.out.println(currentNode.name
          +"退出");
                          n
          --;    
                          currentIndex
          =interval;   
                      }

                  }

                  
                  
          // 找出最后一個幸存者
                  currentNode=first;
                  
          while(true){
                      
          if(currentNode.isQuited==false){
                          System.out.println(currentNode.name
          +"是最后的幸存者");
                          
          break;
                      }

                      
          else{
                          currentNode
          =currentNode.next;
                      }

                  }

              }

              
              
          /**
               * 顯示鏈表元素
               *
               
          */

              
          public void display(){
                  System.out.print(
          "鏈表元素有");
                  Node curr
          =first;
                  
          int n=length;
                  
                  
          while(n>0){
                      System.out.print(curr.name
          +"-("+curr.isQuited+"");
                      curr
          =curr.next;
                      n
          --;
                  }

                  System.out.println();
              }

              
              
          /**
               * 添加一個數(shù)組組成環(huán)形鏈表
               * 
          @param arr
               
          */

              
          public void addArray(String[] arr){
                  length
          =arr.length;
                  
          for(String t:arr){
                      addTail(t);
                  }

                  
                  
                  Node curr
          =first;
                  
                  
          while(curr.next!=null){
                      curr
          =curr.next;
                  }

                  
          // 將最后一個節(jié)點的指針指向頭節(jié)點形成環(huán)形鏈表
                  curr.next=first;
              }

              
              
          /**
               * 在鏈表尾部添加節(jié)點
               * 
          @param t
               
          */

              
          public void addTail(String t){
                  Node newNode
          =new Node();
                  newNode.name
          =t;
                  
                  
          if(first==null){                        
                      first
          =newNode;
                  }

                  
          else{
                      Node curr
          =first;
                      
                      
          while(curr.next!=null){
                          curr
          =curr.next;
                      }

                      
                      curr.next
          =newNode;
                  }

              }

              
              
          public static void main(String[] args){
                  CircleChainList ls
          =new CircleChainList();
                  
                  String[] arr
          ={"1","2","3","4","5","6","7","8"};
                  ls.addArray(arr);
                  ls.wheelOut(
          2);
              }

          }

          posted on 2008-07-05 09:14 sitinspring 閱讀(435) 評論(0)  編輯  收藏 所屬分類: 算法數(shù)據(jù)結(jié)構(gòu)

          sitinspring(http://www.aygfsteel.com)原創(chuàng),轉(zhuǎn)載請注明出處.
          主站蜘蛛池模板: 壤塘县| 葵青区| 南宁市| 政和县| 玛沁县| 连城县| 黑水县| 渭南市| 吴旗县| 祁连县| 介休市| 温州市| 阿城市| 冀州市| 静乐县| 元氏县| 岳阳县| 东乌珠穆沁旗| 石阡县| 南平市| 遵化市| 湘潭县| 南京市| 冕宁县| 天台县| 类乌齐县| 东平县| 承德市| 韶山市| 秦安县| 兴国县| 龙门县| 阳原县| 鄂托克旗| 海阳市| 淮滨县| 汝阳县| 即墨市| 思南县| 兴山县| 平顶山市|