CODE:
import java.util.Scanner;
public class swapHeadTail{
static llistNode insert(llistNode H,int data){
llistNode tmp = new llistNode();
tmp.data=data;
tmp.next=null;
if(H==null)
return tmp;
llistNode curr = new llistNode();
curr = H;
while(curr.next != null)
curr = curr.next;
curr.next = tmp;
return H;
}
public static void main(String[] args) {
llistNode H = new llistNode();
Scanner in = new Scanner(System.in);
llistNode tmp =new llistNode();
H=null;
do{
System.out.print("Enter data:");
H=insert(H,in.nextInt());
System.out.print("Enter 1 to continue: ");
}while (in.nextInt()==1);
tmp = H;
while(tmp != null){
System.out.print(tmp.data+"\t");
tmp = tmp.next;
}
public class swapHeadTail{
static llistNode insert(llistNode H,int data){
llistNode tmp = new llistNode();
tmp.data=data;
tmp.next=null;
if(H==null)
return tmp;
llistNode curr = new llistNode();
curr = H;
while(curr.next != null)
curr = curr.next;
curr.next = tmp;
return H;
}
public static void main(String[] args) {
llistNode H = new llistNode();
Scanner in = new Scanner(System.in);
llistNode tmp =new llistNode();
H=null;
do{
System.out.print("Enter data:");
H=insert(H,in.nextInt());
System.out.print("Enter 1 to continue: ");
}while (in.nextInt()==1);
tmp = H;
while(tmp != null){
System.out.print(tmp.data+"\t");
tmp = tmp.next;
}
System.out.println();
llistNode head = new llistNode();
llistNode headNext = new llistNode();
llistNode tailPrev = new llistNode();
llistNode tail = new llistNode();
head = H;
headNext = H.next;
tailPrev = H;
while(tailPrev.next.next != null)
tailPrev=tailPrev.next;
tail = tailPrev.next;
if ((tailPrev == head)&&(headNext == tail)){
head.next = null;
tail.next = head;
}
else{
tmp = head;
tmp.next = null;
tailPrev.next = tmp;
tail.next=headNext;
}
H=tail;
tmp = H;
while(tmp != null){
System.out.print(tmp.data+"\t");
tmp = tmp.next;
}
}
}
OUTPUT:
Enter data:1
Enter 1 to continue: 1
Enter data:2
Enter 1 to continue: 0
1 2
2 1
Enter 1 to continue: 1
Enter data:2
Enter 1 to continue: 0
1 2
2 1
0 comments :
Post a Comment