-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdll.java
158 lines (153 loc) · 3 KB
/
dll.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
public class dll //dll stands for doubly linked list
{
node head;
node tail;
public static class node
{
int data;
node next;
node prev;
node(int data)
{
this.data=data;
next=null;
prev=null;
}
}
public static dll insertAtFront(dll list,int data)
{
node new_node=new node(data);
if(list.head==null)
{
list.head=new_node;
list.tail=new_node;
return list;
}
node temp=list.head;
new_node.next=temp;
temp.prev=new_node;
list.head=new_node;
return list;
}
public static dll insertAtPost(dll list,int data,int pos)
{
if(pos==1)
return(insertAtFront(list,data));
node temp=list.head;pos--;
node new_node=new node(data);
int count=0;
while(pos!=count)
{
count++;
if(temp.next==null)
break;
temp=temp.next;
}
if(temp.next==null)
return(insert(list,data));
else if(pos>count)
{
System.out.println("position too big bruh");
return list;
}
new_node.next=temp.next;
temp.next.prev=new_node;
temp.next=new_node;
new_node.prev=temp;
return list;
}
public static dll insert(dll list,int data)
{
node temp=list.head;
node new_node=new node(data);
if(temp==null)
{
list.head=new_node;
list.tail=new_node;
return list;
}
else
{
list.tail.next=new_node;
new_node.prev=list.tail;
list.tail=new_node;
return(list);
}
}
public static void display(dll list)
{
node temp=list.head;
while(temp!=null)
{
System.out.print(temp.data+"\t");
temp=temp.next;
}
node temp1=list.tail;System.out.println();
while(temp1!=null)
{
System.out.print(temp1.data+"\t");
temp1=temp1.prev;
}
}
public static dll removeAtFirst(dll list)
{
list.head.next.prev=null;
list.head=list.head.next;
return list;
}
public static dll removeAtLast(dll list)
{
node temp=list.head;
while(temp.next.next!=null)
temp=temp.next;
list.tail=list.tail.prev;
temp.next=null;
return list;
}
public static dll removeAtPost(dll list,int pos)
{
node temp=list.head;pos--;
int count=1;
if(pos==0)
return(removeAtFirst(list));
while(temp.next!=null)
{
if(count==pos)
break;
count++;
temp=temp.next;
}
if(count<pos)
{
System.out.println("Position greater than no. of elelemts");
return(list);
}
else if(temp.next.next==null)
return(removeAtLast(list));
else
{
node cpy=temp.next;
temp.next.next.prev=temp;
temp.next=temp.next.next;
cpy=null;
return list;
}
}
public static void main(String args[])
{
dll list=new dll();
list=insertAtFront(list,0);
list=insert(list,1);
list=insert(list,2);
//list=removeAtFirst(list);
list=insert(list,3);
list=insert(list,4);
list=removeAtLast(list);
list=insert(list,5);
list=insert(list,6);
list=insertAtPost(list,7,2);
//list=removeAtLast(list);
// list=removeAtPost(list,1);
display(list);
}
}