Answer:
Part 1 :
----------------------
class middle_item
{
static Node head;
class Node
{
int data;
Node next;
public Node(Node next, int data)
{
this.data = data;
this.next = next;
}
}
void printMiddle(Node head)
{
int count = 0;
Node mid = head;
while (head != null)
{
if ((count % 2) == 1)
mid = mid.next;
++count;
head = head.next;
}
if (mid != null)
System.out.println("The middle element is [" + mid.data + "]\\");
}
void push(Node head_ref, int new_data)
{
Node new_node = new Node(head_ref,new_data);
head = new_node;
}
void printList(Node head)
{
while (head != null)
{
System.out.print(head.data + "-> ");
head = head.next;
}
System.out.println("null");
}
public static void main(String[] args)
{
middle_item ll = new middle_item();
for(int i = 5; i > 0; i--)
{
ll.push(head, i);
ll.printList(head);
ll.printMiddle(head);
}
}
}
******************************************************************************************
PART 2 :
----------------
import java.util.Scanner;
class ReverseNumberWhile
{
public static void main(String args[])
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
Scanner in = new Scanner(System.in);
num = in.nextInt();
while( num != 0 )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of input number is: "+reversenum);
}
}