-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestLinkedList.java
75 lines (65 loc) · 1.47 KB
/
TestLinkedList.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
class LinkedList{
Node head;
static class Node {
int data;
Node next;
public Node(int data){
this.data = data;
}
}
static LinkedList insertNode(LinkedList list, int d){
Node newNode = new Node(d);
if(list.head == null){
list.head = newNode;
}
else{
Node current = list.head;
while(current.next != null){
current = current.next;
}
current.next = newNode;
}
return list;
}
static LinkedList deleteNode(LinkedList list, int d){
Node current = list.head;
Node prevNode = null;
if(current != null && current.data == d){
list.head = current.next;
}
else{
while(current.next != null && current.data != d){
prevNode = current;
current = current.next;
}
if(current != null)
prevNode.next = current.next;
else{
System.out.println("Not found");
}
}
return list;
}
static void printElements(LinkedList list){
Node current = list.head;
while( current != null){
System.out.println(current.data);
current = current.next;
}
}
}
public class TestLinkedList{
public static void main(String[] args){
LinkedList list = new LinkedList();
list.insertNode(list,1);
list.insertNode(list,2);
list.insertNode(list,3);
list.insertNode(list,2);
list.insertNode(list,8);
list.insertNode(list,2);
list.insertNode(list,1);
list.insertNode(list,9);
list.insertNode(list,8);
list.printElements(list);
}
}