In: Computer Science
Step 1: Create a new Java project called Lab5.5.
Step 2: Now create a new class called
aDLLNode.
class aDLLNode {
aDLLNode prev;
char data;
aDLLNode next;
aDLLNode(char mydata) { // Constructor
data = mydata;
next = null;
prev = null;
}
};
Step 3: In the main() function of the driver class
(Lab5.5), instantiate an object of type aDLLNode and print the
content of its class
public static void main(String[] args) {
System.out.println("-----------------------------------------");
System.out.println("--------Create a new DLLNode-------");
aDLLNode myDLLNode = new aDLLNode('A');
System.out.println(myDLLNode.data);
System.out.println(myDLLNode.next);
System.out.println(myDLLNode.prev);
}
The above is an example of dynamically allocating an object of type
“aDLLNode”. Once the object is created, we can use the “dot”
notation to access and display its properties.
Step 4: Compile and run the program to make sure the above steps are working properly.
Step 5: Now create a new class called
doubleLinkedList
class doubleLinkedList {
aDLLNode head, tail; //keep track of head, tail nodes
int size;
doubleLinkedList() { // Constructor
head = tail = null;
size = 0;
}
//-----------------------------------------------------
public void insert_at_beginning(char value) {
System.out.println("Attempting to Insert " + value + " at
beginning ");
aDLLNode newNode = new aDLLNode(value); // create aNew node
if (size == 0) {
head = tail = newNode;
} else {
newNode.next = head;
head.prev = newNode;
head = newNode;
}
size++;
}
//-----------------------------------------------------
// Delete the first node with the value
// Five cases must be resolved:
// 1) The linked list is empty (head and tail = null)
// 2) The Linked list has one node and it is being deleted (head =
tail)
// 3) The node pointed to by the Head is being deleted.
// 4) The node pointed to by the Tail is being deleted.
// 5) Some node in the middle (not the head or tail) is being
deleted.
public void delete(char deleteValue) {
System.out.println("Attempting to Delete: " + deleteValue);
if (isEmpty()) { // Case 1
System.out.println("Linked List is empty, nothing to
delete");
} else {
if (head == tail) { // Case 2, only one node (head and tail
pointing to it)
if (head.data == deleteValue) {
head = tail = null;
size--;
}
} else {
if (head.data == deleteValue) { // Case 3, Head is being
deleted.
head = head.next;
size--;
} else {
// Case 4 and 5, find the node to be deleted middle or end
boolean found = false;
aDLLNode deletePtr = head; // create a refe
if (deletePtr.data == deleteValue) {
found = true;
if (tail == deletePtr) { // Case 4
deletePtr.prev.next = deletePtr.next;
tail = deletePtr.prev;
} else { // Case 5
deletePtr.prev.next = deletePtr.next;
deletePtr.next.prev = deletePtr.prev;
}
deletePtr = null; // make deletePtr availabe to garbage
collection
size--;
} else {
deletePtr = deletePtr.next;
}
}
if (found == false) {
System.out.println("Not able to find/delete " + deleteValue + " in
the Doubly Linked List");
}
}
}
}
}
//-----------------------------------------------------
public boolean isEmpty() {
if (head == null) {
return (true);
} else {
return (false);
}
}
//-----------------------------------------------------
public void print_from_beginning() {
aDLLNode ptr;
ptr = head;
System.out.print("Printing: Head--> ");
while (ptr != null) {
System.out.print(ptr.data + " --> ");
ptr = ptr.next;
}
System.out.println("NULL / <--Tail " + "(Size = " + size +
")");
}
//-----------------------------------------------------
public int getSize() {
return (size);
}
//-----------------------------------------------------
public void freeAll() {
System.out.println("Freeing Doubly Linked List........ ");
aDLLNode freePtr = head;
while (head != null) {
head = head.next;
// the next two lines are unnecessary, but are included for
// illustration of how memory is freed up
//
freePtr = null; // make the node available for garbage
collector
freePtr = head; // now let the freePtr to the new head
}
head = null;
size = 0;
}
}
Step 6: Compile and run the program to make sure the above steps are still working properly. The result should be still the same as Step 4.
Step 7: Now add the following code after the code in
step 3 (in the main() function). The code below instantiates a
doubly linked list
called “myDLL”, and then proceeds to call it’s method
insert_at_beginning() several times.
doubleLinkedList myDLL = new doubleLinkedList();
System.out.println("-----------------------------------------");
System.out.println("--------Testing
Insert_at_Begining-------");
myDLL.insert_at_beginning('A');
myDLL.insert_at_beginning('b');
myDLL.insert_at_beginning('C');
myDLL.insert_at_beginning('d');
myDLL.print_from_beginning();
Step 8: Compile and run the program.
Step 9: Now, let try running the insert_after() method
of the doubleLinkedList class. Copy the following code and add it
to the bottom of your main() function.
System.out.println("-----------------------------------------");
System.out.println("-----------Testing
Insert_After----------");
myDLL.insert_after('8', 'C');
myDLL.print_from_beginning();
myDLL.insert_after('9', 'D');
myDLL.print_from_beginning();
Step 10: Compile and run the program.
Step 11: Now, let try running the delete() method of the
doubleLinkedList class. Copy the following code and add it to the
bottom of your main() function.
System.out.println("-----------------------------------------");
System.out.println("--------------Testing
Delete-------------");
myDLL.print_from_beginning();
myDLL.delete('d');
myDLL.print_from_beginning();
myDLL.delete('A');
myDLL.print_from_beginning();
myDLL.delete('7');
myDLL.print_from_beginning();
myDLL.freeAll();
myDLL.delete('8');
myDLL.print_from_beginning();
myDLL.insert_at_beginning('8');
myDLL.insert_at_beginning('9');
myDLL.print_from_beginning();
myDLL.delete('8');
myDLL.print_from_beginning();
Step 12: Compile and run the program.
What to submit
The final source code after step 12.
So To be clear I wrote a cripsy code of double linked list all operation in as menu dashboard
all the stuffs is easily understandable.
/*
* Java Program to Implement Doubly Linked List
*/
import java.util.Scanner;
/* Class Node */
class Node
{
protected int data;
protected Node next, prev;
/* Constructor */
public Node()
{
next = null;
prev = null;
data = 0;
}
/* Constructor */
public Node(int d, Node n, Node p)
{
data = d;
next = n;
prev = p;
}
/* Function to set link to next node */
public void setLinkNext(Node n)
{
next = n;
}
/* Function to set link to previous node */
public void setLinkPrev(Node p)
{
prev = p;
}
/* Funtion to get link to next node */
public Node getLinkNext()
{
return next;
}
/* Function to get link to previous node */
public Node getLinkPrev()
{
return prev;
}
/* Function to set data to node */
public void setData(int d)
{
data = d;
}
/* Function to get data from node */
public int getData()
{
return data;
}
}
/* Class linkedList */
class linkedList
{
protected Node start;
protected Node end ;
public int size;
/* Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert element at begining */
public void insertAtStart(int val)
{
Node nptr = new Node(val, null, null);
if(start == null)
{
start = nptr;
end = start;
}
else
{
start.setLinkPrev(nptr);
nptr.setLinkNext(start);
start = nptr;
}
size++;
}
/* Function to insert element at end */
public void insertAtEnd(int val)
{
Node nptr = new Node(val, null, null);
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLinkPrev(end);
end.setLinkNext(nptr);
end = nptr;
}
size++;
}
/* Function to insert element at position */
public void insertAtPos(int val , int pos)
{
Node nptr = new Node(val, null, null);
if (pos == 1)
{
insertAtStart(val);
return;
}
Node ptr = start;
for (int i = 2; i <= size; i++)
{
if (i == pos)
{
Node tmp = ptr.getLinkNext();
ptr.setLinkNext(nptr);
nptr.setLinkPrev(ptr);
nptr.setLinkNext(tmp);
tmp.setLinkPrev(nptr);
}
ptr = ptr.getLinkNext();
}
size++ ;
}
/* Function to delete node at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
if (size == 1)
{
start = null;
end = null;
size = 0;
return;
}
start = start.getLinkNext();
start.setLinkPrev(null);
size--;
return ;
}
if (pos == size)
{
end = end.getLinkPrev();
end.setLinkNext(null);
size-- ;
}
Node ptr = start.getLinkNext();
for (int i = 2; i <= size; i++)
{
if (i == pos)
{
Node p = ptr.getLinkPrev();
Node n = ptr.getLinkNext();
p.setLinkNext(n);
n.setLinkPrev(p);
size-- ;
return;
}
ptr = ptr.getLinkNext();
}
}
/* Function to display status of list */
public void display()
{
System.out.print("\nDoubly Linked List = ");
if (size == 0)
{
System.out.print("empty\n");
return;
}
if (start.getLinkNext() == null)
{
System.out.println(start.getData() );
return;
}
Node ptr = start;
System.out.print(start.getData()+ " <-> ");
ptr = start.getLinkNext();
while (ptr.getLinkNext() != null)
{
System.out.print(ptr.getData()+ " <-> ");
ptr = ptr.getLinkNext();
}
System.out.print(ptr.getData()+ "\n");
}
}
/* Class DoublyLinkedList */
public class DoublyLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of linkedList */
linkedList list = new linkedList();
System.out.println("Doubly Linked List Test\n");
char ch;
/* Perform list operations */
do
{
System.out.println("\nDoubly Linked List Operations\n");
System.out.println("1. insert at begining");
System.out.println("2. insert at end");
System.out.println("3. insert at position");
System.out.println("4. delete at position");
System.out.println("5. check empty");
System.out.println("6. get size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
list.insertAtStart( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to insert");
list.insertAtEnd( scan.nextInt() );
break;
case 3 :
System.out.println("Enter integer element to insert");
int num = scan.nextInt() ;
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos < 1 || pos > list.getSize() )
System.out.println("Invalid position\n");
else
list.insertAtPos(num, pos);
break;
case 4 :
System.out.println("Enter position");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Invalid position\n");
else
list.deleteAtPos(p);
break;
case 5 :
System.out.println("Empty status = "+ list.isEmpty());
break;
case 6 :
System.out.println("Size = "+ list.getSize() +" \n");
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/* Display List */
list.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}