-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathLinkedList.java
More file actions
168 lines (153 loc) · 3.58 KB
/
LinkedList.java
File metadata and controls
168 lines (153 loc) · 3.58 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.util.Scanner;
public class LinkedList
{
static Scanner in=new Scanner(System.in);
Node p=null;
//CREATING A VERTICALLY LINKED LIST
public static Node pushdown(Node p, int key)
{
Node newNode = new Node();
newNode.data = key;
//newNode.next = null;
newNode.down=p;
return newNode; //RETURNS EACH NODE CREATED
}
//INSERTION OF ALL ELEMENTS INTO A SINGLE FLATTENED LIST
void Insert(int value)
{
Node newnode = new Node();
newnode.data=value;
newnode.next=null;
newnode.next=p;
p=newnode;
}
//FLATTENING OF LL- TRAVERSAL HAPPENS IN BOTH DIRECTIONS
//GOES TO EACH HEAD NODE AND ADDS THE SUBSEQUENT NODE(ELEMENT(S)) IN THE VERTICAL LIST TO THE FLATTENED LIST
void flatten(Node root)
{
Node hor=root;
while(hor!=null)
{
Node ver=hor;
while(ver!=null)
{
Insert(ver.data);
ver=ver.down;//TRAVERSING OF VERTICAL LIST
}
hor=hor.next;//HORIZONTAL MOVEMENT-MAIN LIST TRAVERSAL
}
}
void ll_test(Node root)
{
Node hor=root,temp;
temp=hor;
while(temp!=null)
{
System.out.print(temp.data+"\t");
temp=temp.next;
}
System.out.println();
while(hor!=null)
{
Node ver=hor;
while(ver!=null)
{
System.out.print(ver.data+"\t");
ver=ver.down;
}System.out.println();
hor=hor.next;//HORIZONTAL MOVEMENT
}
}
//SORTING OF THE FLATTENED LIST
void sort_ll()
{
Node current=null,Next=null;
int temp;
current=p;
if(p==null)
{
System.out.println("EMPTY");
}
else
{
while(current!=null)
{
Next=current.next;
while(Next!=null)
{
while(current.data>Next.data)
{
temp=current.data;
current.data=Next.data;
Next.data=temp;
}
Next=Next.next;
}
current=current.next;
}
}
}
void traversal()
{
if(p==null)
{
System.out.println("LIST IS EMPTY");
}
else
{
Node temp=p;
System.out.print(temp.data);
while(temp.next!=null)
{
//System.out.print(temp.data+"-->");
temp=temp.next;
System.out.print("-->"+temp.data);
}
}
System.out.println();
}
//METHOD TO CREATE VERTICAL LIST
public Node createVerticalList(Node head, int[] arr)
{
for (int key: arr) //GOES THROUGH EACH ELEMENT
{
head = pushdown(head, key);
}
return head;//RETURNS HEAD OF THE MAIN LIST
}
//A METHOD TO INPUT ELEMENTS FOR THE LIST IN THE FORM OF AN ARRAY
void inputArray(int arr[],int size)
{
int ele;
for(int i=size-1;i>=0;i--)
{
ele=in.nextInt();
arr[i]=ele;
}
System.out.println();
}
public static void main(String[] args)
{
Node head = null;
LinkedList l= new LinkedList();
int[] arr1 = new int[4];
int[] arr2 = new int[4];
int[] arr3 = new int[3];
int[] arr4 = new int[2];
l.inputArray(arr1, 4);
l.inputArray(arr2, 4);
l.inputArray(arr3, 3);
l.inputArray(arr4, 2);
//LINKING LIST VERTICALLY
System.out.println();
head = l.createVerticalList(head, arr1);
head.next = l.createVerticalList(head.next, arr2);
head.next.next = l.createVerticalList(head.next.next, arr3);
head.next.next.next = l.createVerticalList(head.next.next.next, arr4);
l.flatten(head);
l.sort_ll();
l.ll_test(head);
System.out.println();
l.traversal();
}
}