-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
99 lines (85 loc) · 1.42 KB
/
Queue.java
File metadata and controls
99 lines (85 loc) · 1.42 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
import java.util.*;
public class Queue {
private ListNode front;
private ListNode rear;
private int length;
private static class ListNode{
private int data;
private ListNode next;
public ListNode(int data)
{
this.data=data;
}
}
public Queue()
{
this.front=null;
this.rear=null;
this.length=0;
}
public boolean isEmpty()
{
return length==0;
}
public void Enqueue()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the data you want to add in the queue.");
int data=sc.nextInt();
ListNode temp=new ListNode(data);
if(isEmpty())
{
front=temp;
}
else {
rear.next=temp;
}
rear=temp;
length++;
}
public void display()
{
ListNode current=front;
if(isEmpty())
{
System.out.println("No element!!");
}
else {
while(current!=null)
{
System.out.println(current.data);
current=current.next;
}
}
}
public void dequeue()
{
if(isEmpty())
{
System.out.println("No element found!!");
}
int result=front.data;
front=front.next;
if(front==null)
{
rear=null;
}
length--;
System.out.println("Element deleted is " +result);
display();
}
public static void main(String args[])
{
Queue q=new Queue();
// To push data into queue
q.Enqueue();
q.Enqueue();
q.Enqueue();
//To display queue
q.display();
//To delete the data from queue
q.dequeue();
q.dequeue();
q.dequeue();
}
}