Question 1: Joseph's ring problem: Java class library only has a cyclic list and no circular linked list.
// Node class
Public class node {
Public int data;
Public node next;
Public node (int I ){
Data = I;
}
}
// Circular linked list
Public class cirlist {
Private node tail;
Private node cur;
Public node head;
Public int size;
// Create a one-way cyclic linked list
Public cirlist (int n ){
Tail = new node (N );
Cur = tail;
For (INT I = n-1; I> 1; I --){
Node node = new node (I );
Node. Next = cur;
Cur = node;
}
Head = new node (1 );
Head. Next = cur;
Tail. Next = head;
Size = N;
}
// Place the element to be deleted in the first position
Public void setheader (INT s ){
Node temp;
For (INT I = 1; I <s; I ++ ){
Temp = head;
Head = head. Next;
Tail = temp;
}
}
// Delete the element at the first position
Public void Delete (){
Head = head. Next;
Tail. Next = head;
Size --;
}}
// Subject class
Import java. Io .*;
Public class Joseph PHUs {
Public static void main (string ARGs []) {
Joseph PHUs JSF = new Joseph PHUs ();
Try {
System. Out. println ("Enter the number of students :");
Int n = integer. valueof (JSF. getstring (). intvalue ();
System. Out. println ("Enter the step :");
Int S = integer. valueof (JSF. getstring (). intvalue ();
Cirlist list = new cirlist (N );
While (list. size! = 1 ){
List. setheader (s );
List. Delete ();
}
System. Out. println (list. Head. data );
} Catch (ioexception e ){
System. Out. println ("Enter data ");
}
}
Public String getstring () throws ioexception {
Inputstreamreader ISR = new inputstreamreader (system. In );
Bufferedreader BR = new bufferedreader (ISR );
String S = Br. Readline ();
Return S;
}}
Question 2: Enter 123456 on the keyboard --> 654321
Import java. Io .*;
Public class porblem2 {
Public static void main (string ARGs []) throws ioexception {
System. Out. println ("Enter the data to be converted :");
Inputstreamreader in = new inputstreamreader (system. In );
Bufferedreader bu = new bufferedreader (in );
String S = Bu. Readline ();
Stringbuffer sb = new stringbuffer (s );
System. Out. println (sb. Reverse ());
}
}