• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
楠得有你
没有什么能比自己的贪婪,憎恨,沮丧等负面情绪更能伤害自己。
博客园    首页    新随笔    联系   管理    订阅  订阅

用面向对象的思维实现回环列表,,一个手拉手的小游戏

public class Count3Quit2 {
public static void main(String[] args) {
KidCircle kc = new KidCircle(500);
int countNum = 0;
Kid k = kc.first;
while(kc.count > 1) {
countNum ++;
if(countNum == 3) {
countNum = 0;
kc.delete(k);
}
k = k.right;
}

System.out.println(kc.first.id);
}
}

class Kid {
int id;
Kid left;
Kid right;
}

class KidCircle {
int count = 0;
Kid first, last;

KidCircle(int n) {
for(int i=0; i<n; i++) {
add();
}
}

void add() {
Kid k = new Kid();
k.id = count;
if(count <= 0) {
first = k;
last = k;
k.left = k;
k.right = k;
} else {
last.right = k;
k.left = last;
k.right = first;
first.left = k;
last = k;
}
count ++;
}

void delete(Kid k) {
if(count <= 0) {
return;
} else if (count == 1) {
first = last = null;
} else {
k.left.right = k.right;
k.right.left = k.left;

if(k == first) {
first = k.right;
} else if( k == last) {
last = k.left;
}
}
count --;
}
}

天行健,君子以自强不息。
posted @ 2017-10-27 14:41  楠得有你  阅读(145)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3