java tail -n_java 循环链表 head tail

设有n个人依围成一圈,从第1个人开始报数,数到第m个人出列,然后从出列的下一个人开始报数,数到第m个人又出列,…,如此反复到所有的人全部出列为止。设n个人的编号分别为1,2,…,n,打印出出列的顺序.

思路:用JAVA实现循环链表来解决。

/*

* @author    LuoRUI

* @version 1.00 2008/10/25

*/

class  node            //节点类

{

int  no ;          //序列

node next;         //下一个节点

public node(int no) {

this(no, null);

}

public node(int no,node next)   //构造方法

{

this.no=no;

this.next=next;

}

public int getItem(){

return no;

}

public void setNext(node next){

this.next = next;

}

public node getNext(){

return next;

}

}

public class linkedlist

{

private node head,tail;      // 头指针,尾指针

int size;

public linkedlist()          //构造方法,建一个空链表

{  size=0;

head=tail=null;

}

public void addhead(int i)

{

head=new node(i,head);

if (tail == null)

tail = head;

size++;

}

public void addtail(int i)

{

tail.next = new node(i);

tail = tail.next;

tail.next=head;

size++;

}

public static void main(String[] args)

{

int n=Integer.parseInt(args[0]);  //n为节点个数,由string数组args输入

int m=Integer.parseInt(args[1]);  //m为报几个数

linkedlist LL=new linkedlist();

LL.addhead(1);

for (int i=2; i

{

LL.addtail(i);

}

node temp =LL.head;

while (LL.size!=0)

{

for (int i=0;i

temp=temp.next;

System.out.print(temp.getNext().getItem()+"->");

temp.setNext(temp.getNext().getNext());

temp=temp.next;

LL.size--;

}

}

}

结果输出:

D:/code>java linkedlist 8 3

3->6->1->5->2->8->4->7->

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值