单向环形链表
思路
创建孩子节点
class Child {
private int no;
private Child next;
public Child ( int no) {
this . no = no;
}
public int getNo ( ) {
return no;
}
public void setNo ( int no) {
this . no = no;
}
public Child getNext ( ) {
return next;
}
public void setNext ( Child next) {
this . next = next;
}
}
创建环形链表类,让first节点为空
class CircleLinkedList {
private Child first = null;
}
添加节点到链表中
nums为要添加节点的个数,进行判断 定义一个辅助变量curChild 刚开始环形链表中没有一个节点,对于第一个添加的节点比较特殊,先让节点first指向它,然后执行 first.setNext(first),就是让这一个节点指向自己,形成一个只有一个节点的环形链表,并让辅助变量curChild指向它 对接下来添加的节点,只需让辅助变量curChild指向它,再让该节点指向第一个节点first,就可完成节点添加的操作,最后让辅助变量curChild指向新添加进来的节点就可
public void add ( int nums) {
if ( nums < 1 ) {
System. out. println ( "nums的值不正确!!!" ) ;
return ;
}
Child curChild = null;
for ( int i = 1 ; i <= nums; i++ ) {
Child child = new Child ( i) ;
if ( i == 1 ) {
first = child;
first. setNext ( first) ;
curChild = first;
} else {
curChild. setNext ( child) ;
child. setNext ( first) ;
curChild = child;
}
}
}
遍历链表
定义辅助变量curChild指向first 输出curChild节点信息,curChild = curChild.getNext();接着遍历下一个节点 直到curChild.getNext() == first,这时已经遍历完链表,结束遍历
public void show ( ) {
if ( first == null) {
System. out. println ( "没有任何孩子节点!!!" ) ;
return ;
}
Child curChild = first;
while ( true ) {
System. out. println ( "小孩节点:" + curChild. getNo ( ) ) ;
if ( curChild. getNext ( ) == first) {
break ;
}
curChild = curChild. getNext ( ) ;
}
}
节点出圈 1.startNo:表示从第几个节点开始 countNum:表示每次数多少次 nums:总共有多少个节点 2. 先对参数进行判断 3. 定义辅助变量helper指向链表最后一个节点(用循环完成操作) 4. 接着需要让first节点指向第一个开始计数的节点,helper节点跟在后面,移动多少个取决于(startNo - 1) 5. first移动到开始计数的节点后,让first移动到要出圈的那个节点,移动的次数取决于(countNum - 1),helper跟在first后面 6. 将该节点输出,然后让first指向该出圈节点的下一个节点,让节点helper指向first节点,即可完成节点的出圈操作 7. 最后helper == first时,链表中只剩最后一个节点,直接输出
public void get ( int startNo, int countNum, int nums) {
if ( startNo < 1 || startNo > nums || countNum < 1 ) {
System. out. println ( "输入的参数不正确!!!" ) ;
return ;
}
Child helper = first;
while ( true ) {
if ( helper. getNext ( ) == first) {
break ;
}
helper = helper. getNext ( ) ;
}
for ( int i = 0 ; i < startNo - 1 ; i++ ) {
first = first. getNext ( ) ;
helper = helper. getNext ( ) ;
}
while ( true ) {
if ( helper == first) {
break ;
}
for ( int i = 1 ; i <= countNum - 1 ; i++ ) {
first = first. getNext ( ) ;
helper = helper. getNext ( ) ;
}
System. out. println ( "孩子节点:" + first. getNo ( ) ) ;
first = first. getNext ( ) ;
helper. setNext ( first) ;
}
System. out. println ( "最后的孩子节点:" + first. getNo ( ) ) ;
}
完整代码
package LinkedList;
public class CircleSingleLinkedList {
public static void main ( String[ ] args) {
CircleLinkedList list = new CircleLinkedList ( ) ;
list. add ( 5 ) ;
list. show ( ) ;
System. out. println ( "出圈顺序为:" ) ;
list. get ( 1 , 2 , 5 ) ;
}
}
class CircleLinkedList {
private Child first = null;
public void add ( int nums) {
if ( nums < 1 ) {
System. out. println ( "nums的值不正确!!!" ) ;
return ;
}
Child curChild = null;
for ( int i = 1 ; i <= nums; i++ ) {
Child child = new Child ( i) ;
if ( i == 1 ) {
first = child;
first. setNext ( first) ;
curChild = first;
} else {
curChild. setNext ( child) ;
child. setNext ( first) ;
curChild = child;
}
}
}
public void show ( ) {
if ( first == null) {
System. out. println ( "没有任何孩子节点!!!" ) ;
return ;
}
Child curChild = first;
while ( true ) {
System. out. println ( "小孩节点:" + curChild. getNo ( ) ) ;
if ( curChild. getNext ( ) == first) {
break ;
}
curChild = curChild. getNext ( ) ;
}
}
public void get ( int startNo, int countNum, int nums) {
if ( startNo < 1 || startNo > nums || countNum < 1 ) {
System. out. println ( "输入的参数不正确!!!" ) ;
return ;
}
Child helper = first;
while ( true ) {
if ( helper. getNext ( ) == first) {
break ;
}
helper = helper. getNext ( ) ;
}
for ( int i = 0 ; i < startNo - 1 ; i++ ) {
first = first. getNext ( ) ;
helper = helper. getNext ( ) ;
}
while ( true ) {
if ( helper == first) {
break ;
}
for ( int i = 1 ; i <= countNum - 1 ; i++ ) {
first = first. getNext ( ) ;
helper = helper. getNext ( ) ;
}
System. out. println ( "孩子节点:" + first. getNo ( ) ) ;
first = first. getNext ( ) ;
helper. setNext ( first) ;
}
System. out. println ( "最后的孩子节点:" + first. getNo ( ) ) ;
}
}
class Child {
private int no;
private Child next;
public Child ( int no) {
this . no = no;
}
public int getNo ( ) {
return no;
}
public void setNo ( int no) {
this . no = no;
}
public Child getNext ( ) {
return next;
}
public void setNext ( Child next) {
this . next = next;
}
}