/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
/**
*
* @author Tarequzzaman
*/
import java.util.Scanner;
public class Link_Sort
{
node head,current;
class node
{
int data;
node next;
node(int data)
{
this.data = data;
next = null;
}
}
void insert(int data)
{
node new_node = new node(data);
if(head==null)
{
head = new_node;
current = new_node;
}
else
{
current.next = new_node;
current = current.next;
}
}
void display()
{
node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data + " ");
tnode = tnode.next;
}
System.out.println("");
}
void sort(int count)
{
node current = head;
for(int i=0; i<count; i++)
{
node next_node= current.next;
for(int j=i; j<count-1; j++)
{
if(current.data > next_node.data)
{
int temp = current.data;
current.data = next_node.data;
next_node.data = temp;
}
next_node = next_node.next;
}
current= current.next;
}
}
public static void main(String args[])
{
int count=0;
Link_Sort s = new Link_Sort();
System.out.println("Give input enter 0 to break");
while (true)
{
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
if (i == 0)
{
break;
}
count++;
s.insert(i);
}
System.out.println("Current Output");
s.display();
System.out.println("Sorted Output");
s.sort(count);
s.display();
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
/**
*
* @author Tarequzzaman
*/
import java.util.Scanner;
public class Link_Sort
{
node head,current;
class node
{
int data;
node next;
node(int data)
{
this.data = data;
next = null;
}
}
void insert(int data)
{
node new_node = new node(data);
if(head==null)
{
head = new_node;
current = new_node;
}
else
{
current.next = new_node;
current = current.next;
}
}
void display()
{
node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data + " ");
tnode = tnode.next;
}
System.out.println("");
}
void sort(int count)
{
node current = head;
for(int i=0; i<count; i++)
{
node next_node= current.next;
for(int j=i; j<count-1; j++)
{
if(current.data > next_node.data)
{
int temp = current.data;
current.data = next_node.data;
next_node.data = temp;
}
next_node = next_node.next;
}
current= current.next;
}
}
public static void main(String args[])
{
int count=0;
Link_Sort s = new Link_Sort();
System.out.println("Give input enter 0 to break");
while (true)
{
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
if (i == 0)
{
break;
}
count++;
s.insert(i);
}
System.out.println("Current Output");
s.display();
System.out.println("Sorted Output");
s.sort(count);
s.display();
}
}
No comments:
Post a Comment