-
Notifications
You must be signed in to change notification settings - Fork 3
/
Queue_LinkedList.java
68 lines (65 loc) · 1.49 KB
/
Queue_LinkedList.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
package DATA_STRUCTURE;
public class Queue_LinkedList {
class Node{
int data;
Node next;
Node(int d){
this.data = d;
}
}
Node front = null;
Node rear = null;
void insert(int d){
Node n = new Node(d);
n.next = null;
if (front == null) {
front = rear = n;
System.out.println(n.data+" is Inserted ");
}
else{
rear.next = n;
rear = n;
System.out.println(n.data+" is Inserted ");
}
}
void delete(){
if (front == null){
System.out.println("Underflow");
}
else{
System.out.println(front.data+" is deleted form Queue");
front = front.next;
}
}
void display(){
Node n = front;
if(n == null){
System.out.println("Queue is empty..!");
return;
}
while(n!=null){
System.out.print(n.data+" ");
n = n.next;
}
}
int count(){
Node n = front;
int p =0;
while(n!=null){
p++;
n = n.next;
}
return p;
}
public static void main(String[] args) {
Queue_LinkedList q = new Queue_LinkedList();
q.insert(1);
q.insert(11);
q.insert(12);
q.insert(123);
q.insert(1234);
q.delete();
q.display();
System.out.println("total number of nodes : "+q.count());
}
}