-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdll.java
More file actions
130 lines (128 loc) · 2.92 KB
/
dll.java
File metadata and controls
130 lines (128 loc) · 2.92 KB
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
import java.io.*;
import java.util.*;
class dll {
private Node head;
private Node tail;
private int length;
class Node{
Node next;
Node prev;
int value;
Node(int value){
this.value=value;
}
}
public dll(int value){
Node newnode =new Node(value);
head=newnode;
tail=newnode;
length=1;
}
public void printList(){
Node temp=head;
while(temp!=null){
System.out.println(temp.value);
temp=temp.next;
}
}
public void gethead(){
System.out.println("Head value:"+head.value);
}
public void gettail(){
System.out.println("Tail value:"+tail.value);
}
public void getlen(){
System.out.println("Length of linked list "+length);
}
public void append(int value){
Node newnode=new Node(value);
if(length==0){
head=newnode;
tail=newnode;
}
else{
tail.next=newnode;
newnode.prev=tail;
tail=newnode;
}
length++;
}
public Node removelast(){
if(length==0)
return null;
Node temp=tail;
if(length==1){
head=null;
tail=null;
}
else{
tail=tail.prev;
tail.next=null;
temp.prev=null;
}
length--;
return temp;
}
public void prepend(int value){
Node newnode=new Node(value);
if(length==0){
head=newnode;
tail=newnode;
}
else{
newnode.next=head;
head.prev=newnode;
head=newnode;
}
length++;
}
public Node removefirst(){
if(length==0)
return null;
Node temp=head;
if(length==1){
head=null;
tail=null;
}
else{
head=head.next;
head.prev=null;
temp.next=null;
}
length--;
return temp;
}
public Node get(int index){
if(index<0||index>=length)
return null;
Node temp=head;
if(index<length\2){
for(int i=0;i<index;i++)
temp=temp.next;
}
else{
temp=tail;
for(int i=length-1;i>index;i--)
temp=temp.prev;
}
return temp;
}
public static void main(String args[]){
dll mydll=new dll(7);
mydll.append(8);
mydll.printList();
mydll.gethead();
mydll.getlen();
System.out.println("Removing list");
System.out.println(mydll.removelast().value);
System.out.println(mydll.removelast().value);
System.out.println(mydll.removelast());
mydll.append(8);
mydll.append(9);
mydll.prepend(4);
System.out.println("New list");
mydll.printList();
System.out.println("First item "+mydll.removefirst().value);
System.out.println("Getting item at 1 "+mydll.get(1).value);
}
}