Skip to content

Instantly share code, notes, and snippets.

@kshitijvarshne1
Created June 26, 2021 05:00
Show Gist options
  • Save kshitijvarshne1/de677fd7f3d18c619916212f43d424e1 to your computer and use it in GitHub Desktop.
Save kshitijvarshne1/de677fd7f3d18c619916212f43d424e1 to your computer and use it in GitHub Desktop.
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 21-Jun-21
* Time: 10:20 AM
* File: DMain.java
*/
package June.jun21_21.two;
public class DMain {
public static void main(String[] args) {
DoublyLinkedList dl = new DoublyLinkedList();
dl.insertAtEnd(new DNode(33));
dl.insertAtEnd(new DNode(44));
dl.insertAtEnd(new DNode(99));
dl.insertAtEnd(new DNode(55));
dl.insertAtHead(new DNode(22));
dl.traverse();
dl.changeFirstLast();
dl.traverse();
}
}
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 21-Jun-21
* Time: 10:03 AM
* File: DNode.java
*/
package June.jun21_21.two;
public class DNode {
private int data;
private DNode next;
private DNode prev;
public DNode(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public DNode getNext() {
return next;
}
public void setNext(DNode next) {
this.next = next;
}
public DNode getPrev() {
return prev;
}
public void setPrev(DNode prev) {
this.prev = prev;
}
}
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 21-Jun-21
* Time: 10:05 AM
* File: DoublyLinkedList.java
*/
package June.jun21_21.two;
public class DoublyLinkedList {
public DNode head;
public DNode tail;
public DoublyLinkedList() {
head = tail = null;
}
public void insertAtHead(DNode newNode) {
if (head == null) {
head = tail = newNode;
} else {
head.setPrev(newNode);
newNode.setNext(head);
head = newNode;
}
}
public void insertAtEnd(DNode newNode) {
if (head == null) {
head = tail = newNode;
} else {
newNode.setPrev(tail);
tail.setNext(newNode);
tail = newNode;
}
}
public void traverse() {
if (head != null) {
DNode tmp = this.head;
while (tmp != null) {
System.out.print(tmp.getData() + " <- ->");
tmp = tmp.getNext();
}
System.out.println();
}
}
public void deleteAtEnd() {
if (head == null) {
return;
} else if (head.getNext() == null) {
head = tail = null;
} else {
tail = tail.getPrev();
tail.getNext().setPrev(null);
tail.setNext(null);
}
}
public void deleteAtBegin() {
if (head == null) {
return;
} else if (head.getNext() == null) {
head = tail = null;
} else {
/*Node temp = head.next;
head.next.prev = null;
head.next = null;
head = temp;*/
head = head.getNext();
head.setPrev(null);
}
}
public void changeFirstLast(){
if(head!=null){
DNode tHead= head;
DNode tTail = tail;
while(tHead!=tTail){
int k =tHead.getData();
tHead.setData(tTail.getData());
tTail.setData(k);
tHead= tHead.getNext();
tTail= tTail.getPrev();
}
}
else{
System.out.println("Empty");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment