-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedList.java
165 lines (155 loc) · 3.38 KB
/
DoublyLinkedList.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.util.*;
public class DoublyLinkedList
{
private Node head,tail;
//DoublyLinkedList Structure
private static class Node{
int data;
Node next,prev;
Node(int data){
this.data=data;
next=null;
prev=null;
}
}
//Add data at first doubly linked list.
public void addFirst(int data){
Node newNode=new Node(data);
if(head==null){
head=newNode;
tail=newNode;
}
else{
head.prev=newNode;
newNode.next=head;
head=newNode;
}
System.out.println(data+" is added at first successfully.");
}
//Add data at last doubly linked list.
public void addLast(int data){
Node newNode=new Node(data);
if(tail==null){
head=newNode;
tail=newNode;
}
else{
tail.next=newNode;
newNode.prev=tail;
tail=newNode;
}
System.out.println(data+" is added at last successfully.");
}
//Delete data from using index.
public void delete(int index){
if(head==null){
System.out.println("Doubly Linked list is empty.");
}
else{
Node q=head,pre=head;
int i;
for(i=0;q!=null&&i<index;i++){
q=q.next;
}
if(q==null)
System.out.println("Not available.");
else{
System.out.println(q.data+" deleted successfully.");
if(q==head)
head=head.next;
if(q==tail)
tail=tail.prev;
if(q.next!=null)
q.next.prev=q.prev;
if(q.prev!=null)
q.prev.next=q.next;
}
}
}
//Display From the First.
public void displayFirst(){
if(head==null){
System.out.println("Doubly Linked list is empty.");
}
else{
Node q=head;
System.out.println("Doubly LinkedList from first : ");
while(q!=null){
System.out.print(q.data+" ");
q=q.next;
}
System.out.println();
}
}
//Display From the Last.
public void displayLast(){
if(tail==null){
System.out.println("Doubly Linked list is empty.");
}
else{
Node q=tail;
System.out.println("Doubly LinkedList from last: ");
while(q!=null){
System.out.print(q.data+" ");
q=q.prev;
}
System.out.println();
}
}
//Reverse the DLL
public void reverse(){
if(head==null){
System.out.println("Doubly Linked list is empty.");
}
else{
Node curr=head,temp;
while(curr!=null){
temp=curr.next;
curr.next=curr.prev;
curr.prev=temp;
curr=temp;
}
temp=head;
head=tail;
tail=temp;
System.out.println("DLL is reversed.");
}
}
public static void main(String arg[])
{
DoublyLinkedList dll=new DoublyLinkedList();
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("1. Add data at first");
System.out.println("2. Add data at last");
System.out.println("3. Delete from first");
System.out.println("4. Reverse the DLL");
System.out.println("5. Display LL from first");
System.out.println("6. Display LL from last");
System.out.println("7. Exit");
System.out.print("\nEnter your choice : ");
int choice=sc.nextInt();
switch(choice){
case 1: System.out.print("\nData : ");
dll.addFirst(sc.nextInt());
break;
case 2: System.out.print("\nData : ");
dll.addLast(sc.nextInt());
break;
case 3: System.out.print("\nIndex : ");
dll.delete(sc.nextInt());
break;
case 4: dll.reverse();
break;
case 5: dll.displayFirst();
break;
case 6: dll.displayLast();
break;
case 7: System.exit(0);
break;
default: System.out.println("You enter wrong choice.");
}
System.out.println();
}
}
}