/*
@ Used DATA Structure is Linked List
@ author: maulik gohil
*/
package ChanceSequence;
import java.util.Scanner;
public class Main
{
private static Scanner sc;
public static void main(String args[])throws Exception
{
LinkedList ls=new LinkedList();
sc = new Scanner(System.in);
System.out.println("enter length of linkedList ->");
int length=Integer.parseInt(sc.nextLine());
for(int i=1;i<=length;i++)
{
int data=Integer.parseInt(sc.nextLine());
ls.insert(data);
}
System.out.println("insert range which u want to reverse ->");
int r1=Integer.parseInt(sc.nextLine());
int r2=Integer.parseInt(sc.nextLine());
int test=1+(r2-r1);
ls.defineStart(r1);
ls.defineEnd(r2);
if((test%2)==0)
{
ls.doEven(r1,r2);
}
else
{
ls.doOdd(r1,r2);
}
ls.display();
}
}
public Node Start;
public Node End;
public Node def;
private Node temp;
private Node head;
private Node tail;
public void insert(int data)
{
temp=new Node(data);
if(tail!=null)
{
tail.next=temp;
temp.previous=tail;
tail=temp;
}
else
{
def=temp;
head=temp;
tail=temp;
}
}
public void defineStart(int r1)
{
while(count!=r1)
{
def=def.next;
count++;
}
Start=def;
System.out.println("Start DATA>"+Start.data);
}
public void defineEnd(int r2)
{
while(count!=r2)
{
def=def.next;
count++;
}
End=def;
System.out.println("End DATA>"+End.data);
}
public void doEven(int r1,int r2)
{
int i=r1;
int j=r2;
for(i=r1;i<j;i++)
{
int store=Start.data;
Start.data=End.data;
End.data=store;
Start=Start.next;
End=End.previous;
j--;
}
}
public void doOdd(int r1,int r2)
{
int i=r1;
int j=r2;
for(i=r1;i!=j;i++)
{
int store=Start.data;
Start.data=End.data;
End.data=store;
Start=Start.next;
End=End.previous;
j--;
}
}
public void display()
{
//System.out.println("hello");
while(head!=null)
{
System.out.println(head.data);
head=head.next;
}
}
@ Used DATA Structure is Linked List
@ author: maulik gohil
*/
package ChanceSequence;
import java.util.Scanner;
public class Main
{
private static Scanner sc;
public static void main(String args[])throws Exception
{
LinkedList ls=new LinkedList();
sc = new Scanner(System.in);
System.out.println("enter length of linkedList ->");
int length=Integer.parseInt(sc.nextLine());
for(int i=1;i<=length;i++)
{
int data=Integer.parseInt(sc.nextLine());
ls.insert(data);
}
System.out.println("insert range which u want to reverse ->");
int r1=Integer.parseInt(sc.nextLine());
int r2=Integer.parseInt(sc.nextLine());
int test=1+(r2-r1);
ls.defineStart(r1);
ls.defineEnd(r2);
if((test%2)==0)
{
ls.doEven(r1,r2);
}
else
{
ls.doOdd(r1,r2);
}
ls.display();
}
}
public class LinkedList
{
public static int count=0;public Node Start;
public Node End;
public Node def;
private Node temp;
private Node head;
private Node tail;
public void insert(int data)
{
temp=new Node(data);
if(tail!=null)
{
tail.next=temp;
temp.previous=tail;
tail=temp;
}
else
{
def=temp;
head=temp;
tail=temp;
}
}
public void defineStart(int r1)
{
while(count!=r1)
{
def=def.next;
count++;
}
Start=def;
System.out.println("Start DATA>"+Start.data);
}
public void defineEnd(int r2)
{
while(count!=r2)
{
def=def.next;
count++;
}
End=def;
System.out.println("End DATA>"+End.data);
}
public void doEven(int r1,int r2)
{
int i=r1;
int j=r2;
for(i=r1;i<j;i++)
{
int store=Start.data;
Start.data=End.data;
End.data=store;
Start=Start.next;
End=End.previous;
j--;
}
}
public void doOdd(int r1,int r2)
{
int i=r1;
int j=r2;
for(i=r1;i!=j;i++)
{
int store=Start.data;
Start.data=End.data;
End.data=store;
Start=Start.next;
End=End.previous;
j--;
}
}
public void display()
{
//System.out.println("hello");
while(head!=null)
{
System.out.println(head.data);
head=head.next;
}
}
public class Node {
int data,index=0;
Node next;
Node previous;
public Node(int x)
{
this.data=x;
//this.index=index;
this.next=null;
this.previous=null;
}
}
No comments:
Post a Comment