-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleLinkedList.java
More file actions
71 lines (58 loc) · 1.12 KB
/
Copy pathSimpleLinkedList.java
File metadata and controls
71 lines (58 loc) · 1.12 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
public class SimpleLinkedList {
class Node{
String data;
Node next;
Node(String data){
this.data = data;
}
}
Node first;
public void insertFirst(String data){
Node temp = new Node(data);
temp.next = first;
first = temp;
}
public void insertLast(String data){
Node temp = new Node(data);
if (first == null){
first = temp;
return;
}
Node x;
for(x = first; x.next != null; x = x.next);
x.next = temp;
}
public void removeFirst(){
if (first == null){
System.out.println("The list is empty");
return;
}
first = first.next;
}
public void removeLast(){
if (first == null){
System.out.println("The list is empty!");
return;
}
if (first.next == null){
first = null;
return;
}
Node x;
for(x = first; x.next.next != null; x = x.next);
x.next = null;
}
public void printList(){
for (Node x = first; x != null; x = x.next)
System.out.println(x.data);
}
public void searchList(String item){
for (Node x = first; x != null; x = x.next){
if (x.data.equals(item)){
System.out.println("true");
return;
}
}
System.out.println("false");
}
}