Showing posts with label LINKEDLIST. Show all posts
Showing posts with label LINKEDLIST. Show all posts

JAVA program to swap head and tail of a linked list

Posted On // Leave a Comment

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;
        }
        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   
[Read more]

Program to input a linked list and output the sorted linked list

Posted On // Leave a Comment
import java.util.*;

class ll{
static class node{
int data;
node next;
}
public static node insert(node h,int x){
node current =new node();
node temp = new node();
temp.data=x;
if(h==null)
{
temp.next=null;
h=temp;
}
else {
current = h;
while(current.next!=null)
current = current.next;
temp.next=null;
current.next=temp;
}
return h;
}
public static void display(node h){
node current =new node();
current = h;
while (current!=null)
{
if(current.next!=null)
System.out.print(current.data + "->");
else
System.out.print(current.data);
current=current.next;
}
}
public static node sort(node h){
node current =new node();
node temp = new node();
node navigate = new node();
current=h;
if(h==null)
return null;
else
{
while (current!=null){
navigate=current.next;
while(navigate!=null){
if(navigate.data<current.data)
{
temp.data=current.data;
current.data=navigate.data;
navigate.data=temp.data;
}
navigate=navigate.next;
}
current=current.next;
}
}
return h;
}
public static void main(String args[])
{
int k=0;
System.out.println("Program to input a linked list and output the sorted linked list.Print the output at each step");
node h = new node();
h=null;
while(k==0){
System.out.println("Add an element:(Enter 1 to add an element or 0 to exit)");
Scanner in = new Scanner(System.in);
int u =in.nextInt();
if(u==1)
{
System.out.println("Enter the data to insert:");
int data = in.nextInt();
h = insert(h,data);
display(h);
h=sort(h);
System.out.println("\n");
display(h);
System.out.println("\n");
}
else
k=1;
}
}
}
[Read more]