-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyDequeue.java
More file actions
125 lines (100 loc) · 2.85 KB
/
DoublyDequeue.java
File metadata and controls
125 lines (100 loc) · 2.85 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// 4.Write program to implement a DEQUE using Doubly Linked List.
import java.io.*;
class Dlink {
public int data;
public Dlink prev;
public Dlink next;
public Dlink(int d) {
data = d;
next = null;
}
public void displayLink() {
System.out.println(data);
}
}
class DLL {
private Dlink first;
private Dlink last;
public DLL() {
first = null;
last = null;
}
public boolean isEmpty() {
return (first == null);
}
// display all elements in the forward direction
public void displayForward() {
Dlink current = first;
// if list is Empty
if (isEmpty()) {
System.out.println("THE LIST IS EMPTY");
}
while (current != null) {
current.displayLink();
current = current.next;
}
}
// Insert at last position
public void enque(int d) {
Dlink nl = new Dlink(d);
if (isEmpty()) {
first = nl;
last = nl;
} else {
nl.prev = last;
last.next = nl;
last = nl;
}
}
// Deque first
public int deque() {
int temp = first.data;
if (isEmpty()) {
System.out.println("The doublylinkedlist is Empty");
} else if (first.next == null) {
first = null;
last = null;
} else {
first = first.next;
first.next.prev = null;
}
return temp;
}
}
class DoublyDequeue {
public static void main(String args[]) throws IOException {
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
DLL obj;
obj = new DLL();
int x, value, value1, z;
System.out.println("Doubly Linked List");
do {
System.out.println("1.DISPLAY");
System.out.println("2.ENQUE");
System.out.println("3.DEQUE");
x = Integer.parseInt(in.readLine());
switch (x) {
case 1: {
System.out.println("Display all elements");
obj.displayForward();
break;
}
case 2: {
System.out.println("Enter the element to be inserted");
value = Integer.parseInt(in.readLine());
obj.enque(value);
break;
}
case 3: {
value = obj.deque();
System.out.println(value + " is deleted");
break;
}
default:
System.out.println("Invalid input");
break;
}
} while (x == 1 || x == 2 || x == 3);
}
}