-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueLL.java
More file actions
109 lines (107 loc) · 3.44 KB
/
QueueLL.java
File metadata and controls
109 lines (107 loc) · 3.44 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
100
101
102
103
104
105
106
107
108
109
import java.util.Scanner;
public class QueueLL {
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}
}
private Node head = null;
public void enqueue(int data){
Node newNode = new Node(data);
newNode.data=data;
if(head==null){
head=newNode;
System.out.println(head.data+" successfully inserted to the queue");
}else{
newNode.next=head;
head=newNode;
System.out.println(head.data+" successfully inserted to the queue");
}
}
public void dequeue(){
if(head==null){
System.out.println("Queue is empty");
}
else{
System.out.println(head.data+" deleted from queue");
head=head.next;
}
}
public void display(){
if(head==null){
System.out.println("List is empty");
}
else{
Node temp = head;
while(temp!=null){
System.out.print(temp.data+"--->");
temp=temp.next;
}
System.out.println("NULL");
}
}
public void front(){
if(head==null){
System.out.println("List is empty");
}
else{
System.out.println(head.data+" is the front element");
}
}
public void rear(){
if(head==null){
System.out.println("List is empty");
}
else{
Node temp = head;
while(temp.next != null){
temp = temp.next;
}
System.out.println(temp.data+" is the rear element");
}
}
public static void main(String[] args) {
//Generate the rest of the code
QueueLL q1 = new QueueLL();
Scanner scanner = new Scanner (System.in);
int choice;
do{
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("3. Display");
System.out.println("4. Front");
System.out.println("5. Rear");
System.out.println("6. Exit");
System.out.println("Enter your choice:- ");
choice = scanner.nextInt();
switch(choice){
case 1:
System.out.println("Enter the element to be inserted:- ");
int element = scanner.nextInt();
q1.enqueue(element);
break;
case 2:
q1.dequeue();
break;
case 3:
q1.display();
break;
case 4:
q1.front();
break;
case 5:
q1.rear();
break;
case 6:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice");
}
}while(choice!=6);
scanner.close();
}
}