-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_implementation_using_LinkedList.java
83 lines (83 loc) · 1.82 KB
/
queue_implementation_using_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.Scanner;
class Node
{
int data;
Node next=null;
Node(int val)
{
this.data=val;
this.next=null;
}
}
class Queue
{
static Node front=null,rear=null;
public static boolean isEmpty()
{
if(front==null&&rear==null)
return true;
else
return false;
}
public static void enqueue(int element)
{
Node newNode=new Node(element);
if(front==null)
{
front=newNode;
rear=newNode;
}
else
{
rear.next=newNode;
rear=newNode;
}
}
public static int peek()
{
if(isEmpty())
{
System.out.print("Queue is empty");
return -1;
}
else
return front.data;
}
public static void dequeue()
{
if(isEmpty())
{
System.out.print("Queue is empty");
return;
}
else
{
Node tempNode=front;
front=front.next;
if(front==null)
rear=null;
System.out.print(tempNode.data+" ");
}
}
}
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of Elements: ");
int N=sc.nextInt();
System.out.println("Enter the Elements: ");
Queue queue=new Queue();
for(int index=1;index<=N;index++)
{
queue.enqueue(sc.nextInt());
}
System.out.println("Front Element: "+queue.peek());
System.out.println("Inserted Elements in the queue are: ");
while(!queue.isEmpty())
{
queue.dequeue();
}
}
}