-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoublyLinkedList.java
147 lines (115 loc) · 2.73 KB
/
DoublyLinkedList.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
/*
* DOUBLY LINKED LIST:
*
* - allow many operations with time complexity O(1) -
* - including insertions and deletions at arbitrary positions within the list
*
* - it consists -
* - collection of nodes
* - node :
* - element : item on the list
* - next : reference to the node next to it
* - previous : reference to the node previous to it
*
* OPTIMIZATIONS:
*
* HEADER & TRAILER Sentinels:
* - the dummy nodes, which do not carry element of the primary sequence,
* - at both the ends of the list are header and trailer,
* - which avoids some special cases while operating near the boundary of the doubly linked list
*
*/
package com.java.DSA.doublylinkedlist;
public class DoublyLinkedList<E> {
private Node<E> header;
private Node<E> trailer;
private int size;
public DoublyLinkedList() {
header = new Node<E>(null, null, null);
trailer = new Node<E>(null, header, null);
header.setNext(trailer);
size = 0;
}
public int size() {
return size;
}
public boolean isEmpty() {
return (size == 0);
}
public E first() {
if (isEmpty())
return null;
return header.getNext().getElement();
}
public E last() {
if (isEmpty())
return null;
return trailer.getPrev().getElement();
}
public void addFirst(E e) {
addBetween(e, header, header.getNext());
}
public void addLast(E e) {
addBetween(e, trailer.getPrev(), trailer);
}
public E removeFirst() {
if (isEmpty())
return null;
return remove(header.getNext());
}
public E removeLast() {
if (isEmpty())
return null;
return remove(trailer.getPrev());
}
// private methods for updating
private void addBetween(E e, Node<E> previous, Node<E> after) {
Node<E> newest = new Node<E>(e, previous, after);
previous.setNext(newest);
after.setPrev(newest);
size++;
}
private E remove(Node<E> node) {
Node<E> predecessor = node.getPrev();
Node<E> successor = node.getNext();
predecessor.setNext(successor);
successor.setPrev(predecessor);
size--;
return node.getElement();
}
// Doubly Node class
private class Node<E> {
private E element;
private Node<E> next;
private Node<E> prev;
public Node(E element, Node<E> prev, Node<E> next) {
super();
this.element = element;
this.next = next;
this.prev = prev;
}
public Node(E element) {
this.element = element;
this.next = null;
this.prev = null;
}
public E getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
public Node<E> getPrev() {
return prev;
}
public void setPrev(Node<E> prev) {
this.prev = prev;
}
}
}