Skip to content

Commit 1cebf79

Browse files
Add files via upload
1 parent 5ab2b58 commit 1cebf79

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package com.company;
2+
3+
public class DoublyLinkedList {
4+
static class Node{
5+
int data;
6+
Node prev;
7+
Node next;
8+
}
9+
Node head;
10+
public void insertAtFirst(int n){
11+
Node node=new Node();
12+
node.data=n;
13+
node.prev=null;
14+
node.next=null;
15+
if(head==null){
16+
head=node;
17+
}else{
18+
node.next=head;
19+
head.prev=node;
20+
head=node;
21+
}
22+
}
23+
public void show(){
24+
if(head==null){
25+
return;
26+
}
27+
Node temp=head;
28+
while(temp!=null){
29+
System.out.println(temp.data);
30+
temp=temp.next;
31+
}
32+
}
33+
public void reversePrint(){
34+
if(head==null){
35+
return;
36+
}
37+
Node temp=head;
38+
while(temp.next!=null){
39+
temp=temp.next;
40+
}
41+
while(temp!=null){
42+
System.out.println(temp.data);
43+
temp=temp.prev;
44+
}
45+
}
46+
public void insertAtPosition(int position,int data){
47+
if(position==0){
48+
insertAtFirst(data);
49+
}else{
50+
Node temp=head;
51+
Node node=new Node();
52+
node.data=data;
53+
node.prev=null;
54+
node.next=null;
55+
for(int i=0;i<position-1;i++){
56+
temp=temp.next;
57+
if(temp==null) {
58+
return;
59+
}
60+
}
61+
node.next = temp.next;
62+
node.prev = temp;
63+
temp.next = node;
64+
if (temp.next != null) {
65+
temp.next.prev = node;
66+
}
67+
}
68+
}
69+
public void deleteFirst(){
70+
if(head==null || head.next==null){
71+
head=null;
72+
}else{
73+
head.next.prev=null;
74+
head=head.next;
75+
}
76+
}
77+
public void delete(int data){
78+
if(head.data==data){
79+
deleteFirst();
80+
return;
81+
}
82+
Node temp=head;
83+
while(temp!=null){
84+
if(temp.data==data){
85+
break;
86+
}
87+
temp=temp.next;
88+
}
89+
if(temp==null) {
90+
System.out.println("Element doesn't exist");
91+
return;
92+
}else{
93+
if(temp.next!=null)
94+
temp.next.prev=temp.prev;
95+
temp.prev.next=temp.next;
96+
}
97+
}
98+
public void deletelast(){
99+
if(head==null || head.next==null)
100+
head=null;
101+
Node temp=head;
102+
while(temp.next!=null){
103+
temp=temp.next;
104+
}
105+
temp.prev.next=null;
106+
}
107+
public void deleteAtPosition(int position){
108+
if(position==0){
109+
deleteFirst();
110+
}else{
111+
Node temp=head;
112+
for(int i=0;i<position;i++){
113+
temp=temp.next;
114+
}
115+
if(temp==null){
116+
return;
117+
}else{
118+
if(temp.next!=null)
119+
temp.next.prev=temp.prev;
120+
temp.prev.next=temp.next;
121+
}
122+
}
123+
}
124+
public int size(){
125+
if(head==null)
126+
return 0;
127+
Node temp=head;
128+
int count=0;
129+
while(temp!=null){
130+
count++;
131+
temp=temp.next;
132+
}
133+
return count;
134+
}
135+
public void printAllNodes(){
136+
if(head==null){
137+
return;
138+
}
139+
Node temp=head;
140+
while(temp!=null){
141+
System.out.println(temp.data);
142+
temp=temp.next;
143+
}
144+
}
145+
public void roatateClockwiseByK(int k){
146+
if(head==null||head.next==null)
147+
return;
148+
149+
int size=size();
150+
k=k%size;
151+
152+
Node temp=head,start=head;
153+
for(int i=0;i<k;i++){
154+
temp=temp.next;
155+
}
156+
head=temp;
157+
temp.prev.next=null;
158+
temp.prev=null;
159+
for(int i=0;i<k-1;i++){
160+
temp=temp.next;
161+
}
162+
temp.next=start;
163+
start.prev=temp;
164+
}
165+
}

0 commit comments

Comments
 (0)