forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
85 lines (85 loc) · 2.27 KB
/
Copy pathTest.java
File metadata and controls
85 lines (85 loc) · 2.27 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
class Node<T>
{
T val;
Node prev,next;
Node(T val)
{
this.val=val;
}
}
class DoubleLinkList<T>
{
private Node<T> head,tail;
public int addFront(Node<T> n)
{
if(head == null && tail == null)
head = tail = n;
else
{
tail.next = n;
tail = n;
}
return 0;
}
public int addBack(Node<T> n)
{
if(tail == null && tail == null)
head = tail = n;
else
{
head.prev = n;
head = n;
}
return 0;
}
public int removeFront()
{
/**if(tail == null)
throw new Exception(){};
else**/ if(head == tail)
head = tail = null;
else
{
tail = tail.prev;
tail.next = null;
}
return 0;
}
public int removeBack()
{
/**if(head == null)
throw new Exception(){};
else**/ if(head == tail)
head = tail = null;
else
{
head = head.next;
head.prev = null;
}
return 0;
}
public String print()
{
String str = new String();
str = "[";
Node<T> curr = head;
do
{
str += "\t" + curr.val.toString();
curr = curr.next;
}
while(curr != null);
str += "]";
return str;
}
}
public class Test
{
public static void main(String a[])
{
DoubleLinkList<Integer> list = new DoubleLinkList();
list.addFront(new Node<Integer>(5));
list.addFront(new Node<Integer>(4));
System.out.println(list.print());
}
}