File size: 1,964 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
class Concat{
static Node first,second, last, t;
static class Node{
int data;
Node next;
Node(int d){
data = d;
next = null;
}
}
void create(int a[], int n){
first = new Node(a[0]);
last = first;
for(int i=1;i<n;i++){
t = new Node(a[i]);
last.next = t;
last =t;
}
}
void create1(int a[], int n){
second = new Node(a[0]);
last = second;
for(int i=1;i<n;i++){
t = new Node(a[i]);
last.next = t;
last =t;
}
}
void Merge(Node first, Node second){
// for the first Node
Node last;
if(first.data < second.data){
last = first;
first = first.next;
last.next = null;
}
else{
last = second;
second = second.next;
last.next = null;
}
// for connecting second node from the last.next
while(first!=null && second!=null){
if(first.data<second.data){
last.next = first;
last = first;
first = first.next;
last.next = null;
}
else{
last.next = second;
last = second;
second = second.next;
last.next = null;
}
}
// final step
if(first!=null)
last.next = first;
else
last.next = second;
}
void display(Node n){
if(n!=null){
System.out.print(n.data+" ");
display(n.next);
}
else{
return;
}
}
boolean isSorted(Node p){
int max = Integer.MIN_VALUE;
while(p!=null){
if(p.data<max){
return false;
}
max =p.data;
p = p.next;
}
return true;
}
public static void main(String[] args){
Concat l = new Concat();
int a[] = {2,1,10,15};
int a1[] = {4,7,12,14};
int n1 = a1.length;
int n = a.length;
l.create(a,n);
l.create1(a1,n1);
System.out.println(l.isSorted(l.first));
l.display(l.first);
}
}
|