Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 579b1e7

Browse files
committed
Added Doubly Linked List Topic
1 parent a655106 commit 579b1e7

File tree

2 files changed

+253
-0
lines changed

2 files changed

+253
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
public class DoublyLinkedList {
2+
3+
// NODE
4+
5+
public class Node {
6+
7+
public String data;
8+
private Node next;
9+
private Node previous;
10+
11+
public Node(String data) {
12+
this.data = data;
13+
this.next = null;
14+
}
15+
16+
public void setNextNode(Node node) {
17+
this.next = node;
18+
}
19+
20+
public void setPreviousNode(Node node) {
21+
this.previous = node;
22+
}
23+
24+
public Node getNextNode() {
25+
return this.next;
26+
}
27+
28+
public Node getPreviousNode() {
29+
return this.previous;
30+
}
31+
32+
}
33+
34+
// NODE CLASS END
35+
36+
public Node head;
37+
public Node tail;
38+
39+
public DoublyLinkedList() {
40+
this.head = null;
41+
this.tail = null;
42+
}
43+
44+
public void addToHead(String data) {
45+
Node newHead = new Node(data);
46+
Node currentHead = this.head;
47+
48+
if (currentHead != null) {
49+
currentHead.setPreviousNode(newHead);
50+
newHead.setNextNode(currentHead);
51+
}
52+
this.head = newHead;
53+
54+
if (this.tail == null) {
55+
this.tail = newHead;
56+
}
57+
}
58+
59+
public void addToTail(String data) {
60+
Node newTail = new Node(data);
61+
Node currentTail = this.tail;
62+
63+
if (currentTail != null) {
64+
currentTail.setNextNode(newTail);
65+
newTail.setPreviousNode(currentTail);
66+
}
67+
this.tail = newTail;
68+
69+
if (this.head == null) {
70+
this.head = newTail;
71+
}
72+
}
73+
74+
public String removeHead() {
75+
Node removedHead = this.head;
76+
77+
if (removedHead == null) {
78+
return null;
79+
}
80+
this.head = removedHead.getNextNode();
81+
82+
if (this.head != null) {
83+
this.head.setPreviousNode(null);
84+
}
85+
if (removedHead == this.tail) {
86+
this.removeTail();
87+
}
88+
return removedHead.data;
89+
}
90+
91+
public String removeTail() {
92+
Node removedTail = this.tail;
93+
94+
if (removedTail == null) {
95+
return null;
96+
}
97+
this.tail = removedTail.getPreviousNode();
98+
99+
if (this.tail != null) {
100+
this.tail.setNextNode(null);
101+
}
102+
if (removedTail == this.head) {
103+
this.removeHead();
104+
}
105+
return removedTail.data;
106+
}
107+
108+
public Node removeByData(String data) {
109+
Node nodeToRemove = null;
110+
Node currentNode = this.head;
111+
112+
while (currentNode != null) {
113+
if (currentNode.data == data) {
114+
nodeToRemove = currentNode;
115+
break;
116+
}
117+
currentNode = currentNode.getNextNode();
118+
}
119+
120+
if (nodeToRemove == null) {
121+
return null;
122+
}
123+
if (nodeToRemove == this.head) {
124+
this.removeHead();
125+
} else if (nodeToRemove == this.tail) {
126+
this.removeTail();
127+
} else {
128+
Node nextNode = nodeToRemove.getNextNode();
129+
Node previousNode = nodeToRemove.getPreviousNode();
130+
nextNode.setPreviousNode(previousNode);
131+
previousNode.setNextNode(nextNode);
132+
}
133+
return nodeToRemove;
134+
}
135+
136+
public String printList() {
137+
Node currentNode = this.head;
138+
String output = "<head> ";
139+
while (currentNode != null) {
140+
output += currentNode.data + " ";
141+
currentNode = currentNode.getNextNode();
142+
}
143+
output += "<tail>";
144+
System.out.println(output);
145+
return output;
146+
}
147+
148+
// MAIN
149+
150+
public static void main(String[] args) {
151+
152+
DoublyLinkedList subway = new DoublyLinkedList();
153+
154+
subway.addToHead("Times Square");
155+
subway.addToHead("Grand Central");
156+
subway.addToHead("Central Park");
157+
subway.printList();
158+
159+
subway.addToTail("Penn Station");
160+
subway.addToTail("Wall Street");
161+
subway.addToTail("Brooklyn Bridge");
162+
subway.printList();
163+
164+
subway.removeHead();
165+
subway.removeTail();
166+
subway.printList();
167+
168+
subway.removeByData("Times Square");
169+
subway.printList();
170+
}
171+
172+
}

Doubly Linked List/readme.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Doubly Linked List
2+
Like a singly linked list, a doubly linked list is comprised of a series of nodes.
3+
4+
Each node contains data and two links (or pointers) to the next and previous nodes in the list.
5+
6+
The head node is the node at the beginning of the list, and the tail node is the node at the end of the list.
7+
8+
The head node’s previous pointer is set to null and the tail node’s next pointer is set to null.
9+
10+
**Common operations on a doubly linked list include:**
11+
12+
- **Adding** nodes to both ends of the list
13+
- **Removing** nodes from both ends of the list
14+
- **Finding**, and removing, a node from anywhere in the list
15+
- **Traversing** (or traveling through) the list
16+
17+
## Adding To The Doubly Linked List
18+
19+
In a doubly linked list, we have to keep track of and set the node’s _previous pointer_ as well as _update_ the _tail_ of the list if necessary.
20+
21+
### Adding To Head
22+
When adding to the head of the doubly linked list, we first need to check if there is a current head to the list.
23+
24+
If there isn’t, then the list is empty, and we can simply make our new node both the head and tail of the list and set both pointers to null.
25+
26+
If the list is not empty, then we will:
27+
28+
- Set the current head’s previous pointer to our new head
29+
- Set the new head’s next pointer to the current head
30+
- Set the new head’s previous pointer to null
31+
32+
### Adding To Tail
33+
Tthere are two cases when adding a node to the tail of a doubly linked list.
34+
35+
If the list is empty, then we make the new node the head and tail of the list and set the pointers to null.
36+
37+
If the list is not empty, then we:
38+
39+
- Set the current tail’s next pointer to the new tail
40+
- Set the new tail’s previous pointer to the current tail
41+
- Set the new tail’s next pointer to null
42+
43+
## Removing Head Or Tail
44+
Due to the extra pointer and tail property, removing the head from a doubly linked list is slightly more complicated than removing the head from a singly linked list.
45+
46+
However, the previous pointer and tail property make it much simpler to remove the tail of the list, as we don’t have to traverse the entire list to be able to do it.
47+
48+
### Removing Head
49+
Removing the head involves updating the pointer at the beginning of the list.
50+
51+
We will set the _previous pointer_ of the new head to _null_, and update the head property of the list.
52+
53+
If the head was also the tail, the tail removal process will occur as well.
54+
55+
### Removing Tail
56+
removing the tail involves updating the pointer at the end of the list.
57+
58+
We will set the _next pointer_ of the new tail to _null_, and update the tail property of the list.
59+
60+
If the tail was also the head, the head removal process will occur as well.
61+
62+
## Removing From The Middle
63+
Since that node is neither the head and nor the tail of the list, there are two pointers that must be updated:
64+
65+
- We must set the removed node’s preceding node’s next pointer to its following node
66+
- We must set the removed node’s following node’s previous pointer to its preceding node
67+
68+
There is no need to change the pointers of the removed node, as updating the pointers of its neighboring nodes will remove it from the list.
69+
70+
If no nodes in the list are pointing to it, the node is orphaned.
71+
72+
## Overview
73+
**Doubly Linked Lists**
74+
75+
- Are comprised of nodes that contain links to the next and previous nodes
76+
- Are bidirectional, meaning it can be traversed in both directions
77+
- Have a pointer to a single head node, which serves as the first node in the list
78+
- Have a pointer to a single tail node, which serves as the last node in the list
79+
- Require the pointers at the head of the list to be updated after addition to or removal of the head
80+
- Require the pointers at the tail of the list to be updated after addition to or removed of the tail
81+
- Require the pointers of the surrounding nodes to be updated after removal from the middle of the list

0 commit comments

Comments
 (0)