-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPr_LinkedList.java
More file actions
140 lines (113 loc) · 3.3 KB
/
Pr_LinkedList.java
File metadata and controls
140 lines (113 loc) · 3.3 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
class Node{
private Integer data;
private Node next;
public Node() {
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Integer getData() {
return data;
}
public void setData(Integer data) {
this.data = data;
}
}
import java.util.StringJoiner;
public class LinkedList {
private Node firstNode;
private Node lastNode;
private int size;
public LinkedList() {
}
public String toString() {
if (firstNode == null) {
return "[ No Elements ]";
}
StringJoiner stringJoiner = new StringJoiner(" ", "[", "]");
Node nextNode = firstNode;
do {
stringJoiner.add(String.valueOf(nextNode.getData()));
nextNode = nextNode.getNext();
} while (nextNode != null);
return stringJoiner.toString();
}
public void add(Integer data) {
Node tempNodeNode = new Node();
tempNodeNode.setData(data);
tempNodeNode.setNext(null);
Node lastNode = this.lastNode;
this.lastNode = tempNodeNode;
if (lastNode == null) {
firstNode = tempNodeNode;
} else {
lastNode.setNext(tempNodeNode);
}
this.size++;
}
public Integer get(int index) {
if (index > this.size || index < 0 || this.size == 0)
return null;
else {
int count = 0;
Node tempNode = firstNode;
while (count != index) {
tempNode = tempNode.getNext();
count++;
}
if (tempNode != null) {
return tempNode.getData();}
else return null;
}
}
public boolean delete(int index) {
if (index > this.size || index < 0 || this.size == 0) {
return false;
}
else {
int count = 0;
Node tempNode = firstNode;
if (index == 0) {
this.firstNode = firstNode.getNext();
}
else {
while (count != index -1) {
tempNode = tempNode.getNext();
count++;
}
if (tempNode == lastNode ) {
tempNode.setNext(null);
lastNode = tempNode;
}
else {
tempNode.setNext(tempNode.getNext().getNext());
}
}
this.size--;
return true;
}
}
public int size() {
if (this.size != 0) {
return this.size;
}
return 0;
}
}
public class Main {
public static void main(String[] args) {
LinkedList artLinkedList = new LinkedList();
for (int i = 0; i < 10; i++) {
artLinkedList.add(i);
}
System.out.println("Linked list size is: " + artLinkedList.size());
System.out.println("Fourth element before deletion: " + artLinkedList.get(4));
artLinkedList.delete(4);
System.out.println("Fourth element after deletion: " + artLinkedList.get(4));
System.out.println("Linked list size is: " + artLinkedList.size());
System.out.println("Linked list is: " + artLinkedList.toString());
}
}