forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.java
161 lines (137 loc) · 4.76 KB
/
LinkedList.java
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
import java.io.*;
import java.util.*;
import java.lang.*;
public class LinkedList {
private Node Start;
class Node{
public String info;
public Node next;
public Node(String s){
info = s;
next = null;
}
}
public void Insert_position(int pos, String val){ // METHOD FOR INSERTING A NODE AT A GIVEN POSITION IN LL
int act_pos = pos-1;
if(act_pos == 0){
Node temp = new Node(val);
if(Start == null){
Start = temp;
}
else{
temp.next = Start;
Start = temp;
}
}
else{
Node temp = new Node(val);
temp.next = Start;
Node p = temp;
for(int z = 0; z < act_pos; ++z){
p = p.next;
}
Node q = new Node(val);
q.info = val;
q.next = p.next;
p.next = q;
}
}
public void Deletion_LinkedList(int pos){ // METHOD FOR DELETING A NODE AT A POSITION FROM THE LINKED LIST
int act_pos = pos - 1;
if(act_pos == 0){
Node temp = Start;
Start = temp.next;
temp.next = null;
}
else{
Node temp = Start;
for(int g = 0; g < act_pos-1 && temp != null; g++){
temp = temp.next;
}
Node p = temp.next.next;
temp.next = p;
}
}
public void Insertion_LinkedList(String val){ // METHOD FOR APPENDING VALUES TO A LINKED LIST.
Node temp1 = new Node(val);
if(Start == null){
Start = temp1;
}
else{
Node temp2 = Start;
while(temp2.next != null){
temp2 = temp2.next;
}
temp2.next = temp1;
}
}
public void Display(){ // METHOD FOR DISPLAYING THE LINKED LIST
Node now = Start;
while(now != null){
System.out.print(now.info + " ");
now = now.next;
}
System.out.println("");
}
public void Compare(LinkedList ob){ // METHOD FOR COMPARING THE LINKED LIST
Node temp1 = this.Start;
Node temp2 = ob.Start;
while(temp1 != null && temp2 != null){
if(!(temp1.info.equals(temp2.info)) == true){
System.out.println("0");
break;
}
temp1 = temp1.next;
temp2 = temp2.next;
}
if((temp1 == null) && (temp2 == null)){
System.out.println("1");
}
else if((temp1 == null) || (temp2 == null)){
System.out.println("0");
}
}
public static void main(String[] args) throws Exception { // BEGINNING OF MAIN MODULE
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(r);
String t = br.readLine();
String[] tokens = t.split(" ");
int[] a = new int[tokens.length];
for(int i = 0; i < tokens.length; i++){
a[i] = Integer.parseInt(tokens[i]);
}
LinkedList ll = new LinkedList();
String[] b = new String[a[0]];
String arr1 = br.readLine();
String[] split_arr = arr1.split(" ");
for(int j = 0; j < split_arr.length; j++){
ll.Insertion_LinkedList(split_arr[j]);
}
for(int m = 0; m < a[1]; m++){
String q = br.readLine();
String[] query_arr = q.split(" ");
String [] c = new String[query_arr.length];
for(int l = 0; l < query_arr.length; l++){
c[l] = query_arr[l];
}
if(Integer.parseInt(c[0]) == 1){
ll.Insert_position(Integer.parseInt(c[2]), c[1]);
}
else if(Integer.parseInt(c[0]) == 2){
ll.Deletion_LinkedList(Integer.parseInt(c[1]));
}
else if(Integer.parseInt(c[0]) == 3){
ll.Display();
}
else{
if(Integer.parseInt(c[0]) == 4){
LinkedList ll2 = new LinkedList();
for(int d = 2; d <= c.length-1; d++){
ll2.Insertion_LinkedList(c[d]);
}
ll.Compare(ll2);
}
}
}
}
}