-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcLinkedlist.java
177 lines (138 loc) · 3.42 KB
/
ExcLinkedlist.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
class Node {
private String data;
private Node next;
public Node(String data) {
this.data = data;
}
public void setData(String data) {
this.data = data;
}
public void setNext(Node node) {
this.next = node;
}
public String getData() {
return this.data;
}
public Node getNext() {
return this.next;
}
}
class LinkedList {
private Node head;
private Node tail;
public Node getHead() {
return this.head;
}
public Node getTail() {
return this.tail;
}
public void addAtEnd(String data) {
Node node = new Node(data);
if (this.head == null) {
this.head = this.tail = node;
} else {
this.tail.setNext(node);
this.tail = node;
}
}
public void addAtBeginning(String data) {
Node node = new Node(data);
if (this.head == null) {
this.head = this.tail = node;
}
else {
node.setNext(this.head);
this.head = node;
}
}
public void display() {
Node temp = this.head;
while (temp != null) {
System.out.println(temp.getData());
temp = temp.getNext();
}
}
public Node find(String data) {
Node temp = this.head;
while (temp != null) {
if (temp.getData().equals(data))
return temp;
temp = temp.getNext();
}
return null;
}
public void insert(String data, String dataBefore) {
Node node = new Node(data);
if (this.head == null)
this.head = this.tail = node;
else {
Node nodeBefore = this.find(dataBefore);
if (nodeBefore != null) {
node.setNext(nodeBefore.getNext());
nodeBefore.setNext(node);
if (nodeBefore == this.tail)
this.tail = node;
} else
System.out.println("Node not found");
}
}
public void delete(String data) {
if (this.head == null)
System.out.println("List is empty");
else {
Node node = this.find(data);
if (node == null)
System.out.println("Node not found");
if (node == this.head) {
this.head = this.head.getNext();
node.setNext(null);
if (node == this.tail)
tail = null;
}
else {
Node nodeBefore = null;
Node temp = this.head;
while (temp != null) {
if (temp.getNext() == node) {
nodeBefore = temp;
break;
}
temp = temp.getNext();
}
nodeBefore.setNext(node.getNext());
if (node == this.tail)
this.tail = nodeBefore;
node.setNext(null);
}
}
}
}
public class LinkedListExercise {
public static void main(String args[]) {
LinkedList linkedList = new LinkedList();
linkedList.addAtEnd("AB");
linkedList.addAtEnd("BC");
linkedList.addAtEnd("CD");
linkedList.addAtEnd("DE");
linkedList.addAtEnd("EF");
String elementToBeFound = "CD";
int position = findPosition(elementToBeFound, linkedList.getHead());
if (position != 0)
System.out.println("The position of the element is " + position);
else
System.out.println("The element is not found!");
}
public static int findPosition(String element, Node head) {
int position = 1;
Node current = head;
while (current != null) {
if (current.getData().equals(element)) {
return position;
}
current = current.getNext();
position++;
}
// If element is not found
return 0;
}
}