Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Doubly Linked List.java #133

Merged
merged 1 commit into from
Oct 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 63 additions & 0 deletions Java/DoublyLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
public class DoublyLinkedList {

class Node{
int data;
Node previous;
Node next;

public Node(int data) {
this.data = data;
}
}


Node head, tail = null;


public void addNode(int data) {

Node newNode = new Node(data);


if(head == null) {

head = tail = newNode;

head.previous = null;

tail.next = null;
}
else {
tail.next = newNode;
newNode.previous = tail;
tail = newNode;
tail.next = null;
}
}

public void display() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of doubly linked list: ");
while(current != null) {

System.out.print(current.data + " ");
current = current.next;
}
}

public static void main(String[] args) {

DoublyLinkedList dList = new DoublyLinkedList();
dList.addNode(1);
dList.addNode(2);
dList.addNode(3);
dList.addNode(4);
dList.addNode(5);

dList.display();
}
}