-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathdoublyll.java
164 lines (150 loc) · 3.42 KB
/
doublyll.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
import java.util.*;
public class doublyll
{
class Node
{
int info;
Node prev,next;
public Node(int item)
{
this.info=item;
}
}
static Node head=null,tail=null,p;
void InsertFirst(int val)
{
p=new Node(val);
p.next=head;
if(head==null)
head=tail=p;
else
{
head.prev=p;
head=p;
}
}
void InsertAfter(int item,int item1)
{
Node curr=head;
while(curr!=null && curr.info!=item1)
{
curr=curr.next;
}
if(curr==null)
System.out.printf("No such element or node");
else
{
p=new Node(item);
p.prev=curr;
p.next=curr.next;
if(curr==tail)
tail=p;
else
curr.next.prev=p;
curr.next=p;
}
}
void InsertBefore(int item,int item1)
{
Node curr=head;
while(curr!=null && curr.info!=item1)
curr=curr.next;
if(curr==null)
System.out.printf("No such node or element");
else
{
p=new Node(item);
p.prev=curr.prev;
p.next=curr;
if(curr==head)
head=p;
else
curr.prev.next=p;
curr.prev=p;
}
}
void TraverseRight(Node head)
{
Node curr=head;
while(curr!=null)
{
System.out.printf("%d",curr.info);
curr=curr.next;
if(curr!=null)
System.out.printf("-->");
}
}
void TraverseLeft(Node tail)
{
Node ptr=tail;
while(ptr!=null)
{
System.out.printf("%d",ptr.info);
ptr=ptr.prev;
if(ptr!=null)
System.out.printf("<--");
}
}
void Delete(int item)
{
Node curr=head;
while(curr!=null && curr.info != item)
curr = curr.next;
if(curr == null)
System.out.printf("No such element or node");
else
{
if(curr == head)
head = curr.next;
else
curr.prev.next=curr.next;
if(curr == tail)
tail = curr.prev;
else
curr.next.prev=curr.prev;
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
doublyll d=new doublyll();
int ch,po,it;
do
{
System.out.printf("\nDoubly LinkedList Operations are:\n");
System.out.printf("\n1.Insfirst\n2.InsAfter\n3.InsBefore\n4.TraverseRight\n5.TraverseLeft\n6.Delete\n7.Exit\n");
System.out.printf("\nEnter your choice:\n");
ch=sc.nextInt();
switch(ch)
{
case 1:System.out.printf("Enter the value to be inserted first:");
po=sc.nextInt();
d.InsertFirst(po);
break;
case 2:System.out.printf("Enter item after which an element is to be inserted:");
it=sc.nextInt();
System.out.printf("Value to be inserted:");
po=sc.nextInt();
d.InsertAfter(po,it);
break;
case 3:System.out.printf("Enter item before which an element is to be inserted:");
it=sc.nextInt();
System.out.printf("Value to be inserted:");
po=sc.nextInt();
d.InsertBefore(po,it);
break;
case 4:d.TraverseRight(head);
break;
case 5:d.TraverseLeft(tail);
break;
case 6:System.out.printf("Enter the value to be deleted:");
po=sc.nextInt();
d.Delete(po);
break;
case 7:break;
default:System.out.printf("Invalid choice");
}
}while(ch!=7);
sc.close();
}
}