In: Computer Science
Build a doubly linked list with these operations:
AddToHead(10); AddToHead(20); AddToTail(30); AddToTail(40);
Build a sorted doubly linked list with these operations:
Add(30), Add(20), Add(40), Add(15), Add(35);
Build a doubly linked list with these operations:
AddToHead(10); AddToHead(20); AddToTail(30);
AddToTail(40);
Code For Above Problem:(IN JAVA)
public class DoublyLinkedList {
// Node of a doubly linked list
private class Node {
int data;
Node next, prev;
public Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
Node head;// refernce to the head node
Node tail;// reference to the tail node
public void AddToHead(int data) {
/*
* allocate node put in the data
*/
Node newNode = new Node(data);
// If list is empty
if (head == null) {
// Both head and tail will point to newNode
head = tail = newNode;
} else {
// newNode will be added before head such that head prev will point to newNode
head.prev = newNode;
// newNode's next will point to head
newNode.next = head;
// newNode will become new head
head = newNode;
}
}
public void AddToTail(int data) {
/*
* allocate node put in the data
*/
Node newNode = new Node(data);
// If list is empty
if (tail == null) {
// Both head and tail will point to newNode
head = tail = newNode;
} else {
// newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
// newNode's previous will point to tail
newNode.prev = tail;
// newNode will become new tail
tail = newNode;
}
}
//Method to print the contents Of List
public void print() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
}
public static void main(String[] args) {
//create new Object of DoublyLinkedList
DoublyLinkedList list = new DoublyLinkedList();
//perform given operations
list.AddToHead(10);
list.AddToHead(20);
list.AddToTail(30);
list.AddToTail(40);
//print the list
list.print();
}
}
Output Of Above Code:
20 10 30 40
Images Of Code:
Image Of Output:
Build a sorted doubly linked list with these
operations:
Add(30), Add(20), Add(40), Add(15), Add(35);
Code For Above Problem:(IN JAVA)
public class SortedDoublyLinkedList {
// Node of a doubly linked list
private class Node {
int data;
Node next, prev;
public Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
Node head;// refernce to the head node
Node tail;// reference to the tail node
public void Add(int data) {
/*
* allocate node put in the data
*/
Node newNode = new Node(data);
Node current;
// if list is empty
if (head == null)
head = newNode;
// if the node is to be inserted at the beginning
// of the doubly linked list
else if (head.data >= newNode.data) {
newNode.next = head;
newNode.next.prev = newNode;
head = newNode;
}
else {
current = head;
// locate the node after which the new node
// is to be inserted
while (current.next != null && current.next.data < newNode.data)
current = current.next;
/* Make the appropriate links */
newNode.next = current.next;
// if the new node is not inserted
// at the end of the list
if (current.next != null)
newNode.next.prev = newNode;
current.next = newNode;
newNode.prev = current;
}
}
// Method to print the contents Of List
public void print() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
}
public static void main(String[] args) {
//create new Object of SortedDoublyLinkedList
SortedDoublyLinkedList list = new SortedDoublyLinkedList();
// perform given operations
list.Add(10);
list.Add(20);
list.Add(30);
list.Add(40);
list.print();
}
}
Output Of Code:
10 20 30 40
Images Of Code:
Image Of Output: