/
QUL.java
81 lines (64 loc) · 1.06 KB
/
QUL.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
package queueUsingList;
//Reversing a queue using recursion
import java.io.*;
import java.util.*;
class LinkedList
{
int data;
LinkedList next;
LinkedList prev;
}
public class QUL
{
public static LinkedList reverseQueue(LinkedList head)
{
LinkedList current = head;
if(current == null)
{
return null;
}
LinkedList next = current.next;
if(next == null)
{
head = current;
return head;
}
head = reverseQueue(next);
next.next = current;
current.prev = next;
next.prev = null;
current.next = null;
return head;
}
public static void displayQueue(LinkedList head)
{
while(head != null)
{
System.out.println(head.data);
head = head.next;
}
}
public static void main(String [] args)
{
LinkedList p= null,q=null, head = null;
for(int i =1 ; i<=10; i++)
{
q = new LinkedList();
q.data = i;
q.next = null;
if(head == null)
{
head = q;
p=q;
}
else
{
p.next = q;
p=q;
}
}
displayQueue(head);
head = reverseQueue(head);
displayQueue(head);
}
}