-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList2.java
More file actions
79 lines (76 loc) · 1.47 KB
/
SinglyLinkedList2.java
File metadata and controls
79 lines (76 loc) · 1.47 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
public class SinglyLinkedList2 {
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}
}
Node head=null;
Node tail=null;
public void addNode(int data) {
Node n=new Node(data);
if(head==null) {
head=n;
tail=n;
}
tail.next=n;
tail=n;
}
public void max() {
Node current=head;
if(head==null) {
System.out.println("List is Empty");
return;
}
else {
int max=head.data;
while(current !=null) {
if(max<current.data)
max=current.data;
current=current.next;
}
System.out.println("the maximum Element in the List is "+max);
}
}
public void min() {
Node current=head;
if(head==null) {
System.out.println("List is Empty");
return;
}
else {
int min=head.data;
while(current !=null) {
if(min>current.data)
min=current.data;
current=current.next;
}
System.out.println("the minimum Element in the List is "+min);
}
}
public void display() {
Node current=head;
if(head==null) {
System.out.println("List is Empty");
return;
}
while(current !=null) {
System.out.print(current.data+",");
current=current.next;
}
System.out.println();
}
public static void main(String[] args) {
SinglyLinkedList2 s1=new SinglyLinkedList2();
s1.addNode(54);
s1.addNode(76);
s1.addNode(33);
s1.addNode(89);
s1.addNode(55);
s1.display();
s1.max();
s1.min();
}
}