( Animation: doubly linked list ) Write a Java program to animate search, insert
ID: 3685945 • Letter: #
Question
(Animation: doubly linked list) Write a Java program to animate search, insertion,
and deletion in a doubly linked list, as shown in Figure 24.24. The Search button
searches the specified value in the list. The Delete button deletes the specified
value from the list. The Insert button appends the value into the list if the index
is not specified; otherwise, it inserts the value into the specified index in the
list. Also add two buttons named Forward Traversal and Backward Traversal
for displaying the elements in a forward and backward order, respectively, using iterators.
Name your program Exercise24_11
The output should match the results in Figure 24.24
Explanation / Answer
import java.util.Scanner;
/* This is the Class Node */
class Node
{
protected int data;
protected Node next, previous;
/* This is the constructor for creating a doubly linked list */
public Node()
{
next = null;
prev = null;
data = 0;
}
/* parameterized Constructor */
public Node(int d, Node n, Node p)
{
data = d;
next = n;
previous = p;
}
/* This is the function to set link to next node */
public void tosetLinkNext(Node n)
{
next = n;
}
/* This is the function to set the link to previous node */
public void tosetLinkPrev(Node p)
{
previous = p;
}
/* This is the function to get link to next node */
public Node togetLinkNext()
{
return next;
}
/* This is the function to get link to previous node */
public Node togetLinkPrev()
{
return previous;
}
/* This is the function to set data to node */
public void tosetData(int d)
{
data = d;
}
/* Function to get data from node */
public int togetData()
{
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 get size of list */
public int getSize()
{
return size;
}
/* Fhis is the function to insert element at begining */
public void toinsertAtStart(int value)
{
Node nptr1 = new Node(value, null, null);
if(start == null)
{
start = nptr1;
end = start;
}
else
{
start.tosetLinkPrev(nptr);
nptr1.tosetLinkNext(start);
start = nptr1;
}
size++;
}
/* this is the function to insert element at end */
public void toinsertAtEnd(int value)
{
Node nptr1 = new Node(value, null, null);
if(start == null)
{
start = nptr1;
end = start;
}
else
{
nptr1.tosetLinkPrev(end);
end.tosetLinkNext(nptr1);
end = nptr1;
}
size++;
}
/* This is the function to insert element at position */
public void toinsertAtPos(int value , int pos)
{
Node nptr1 = new Node(value, null, null);
if (pos == 1)
{
toinsertAtStart(value);
return;
}
Node ptr1 = start;
for (int i = 2; i <= size; i++)
{
if (i == pos)
{
Node tmp1 = ptr.togetLinkNext();
ptr.tosetLinkNext(nptr1);
nptr.tosetLinkPrev(ptr1);
nptr.tosetLinkNext(tmp1);
tmp1.tosetLinkPrev(nptr1);
}
ptr1 = ptr1.getLinkNext();
}
size++ ;
}
/* this is the function to delete node at position */
public void todeleteAtPos(int position)
{
if (position == 1)
{
if (size == 1)
{
start = null;
end = null;
size = 0;
return;
}
start = start.togetLinkNext();
start.tosetLinkPrev(null);
size--;
return ;
}
if (position == size)
{
end = end.togetLinkPrev();
end.tosetLinkNext(null);
size-- ;
}
Node ptr = start.togetLinkNext();
for (int i = 2; i <= size; i++)
{
if (i == position)
{
Node p = ptr.togetLinkPrev();
Node n = ptr.togetLinkNext();
p.tosetLinkNext(n);
n.tosetLinkPrev(p);
size-- ;
return;
}
ptr = ptr.togetLinkNext();
}
}
/* Function to display list */
public void display()
{
System.out.print(" Doubly Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return;
}
if (start.togetLinkNext() == null)
{
System.out.println(start.togetData() );
return;
}
Node ptr = start;
System.out.print(start.togetData()+ " <-> ");
ptr = start.togetLinkNext();
while (ptr.togetLinkNext() != null)
{
System.out.print(ptr.togetData()+ " <-> ");
ptr = ptr.togetLinkNext();
}
System.out.print(ptr.togetData()+ " ");
}
}
/* This is the Class DoublyLinkedList */
public class createDoublyLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* This is done to create an object of linkedList */
linkedList listhere = new linkedList();
System.out.println("Doubly Linked List ");
char choiceone;
/* This is to Perform the list operations */
do
{
System.out.println(" Doubly Linked List Operations ");
System.out.println("1. insertion at begining");
System.out.println("2. inserion at end");
System.out.println("3. insertion at position");
System.out.println("4. deletion from a given position");
int choicehere = scan.nextInt();
switch (choicehere)
{
case 1 :
System.out.println("Please enter integer element toinsert"); listhere.toinsertAtStart( scan.nextInt() );
break;
case 2 :
System.out.println(" Please enter integer element toinsert");
listhere.toinsertAtEnd( scan.nextInt() );
break;
case 3 :
Sysem.out.println("Please enter integer element to insert");
int num = scan.nextInt() ;
System.out.println("Enter the position to insert");
int position = scan.nextInt() ;
if (position < 1 || position > listhere.togetSize() )
System.out.println("this is an Invalid position ");
else
listhere.toinsertAtPos(num, pos);
break;
case 4 :
System.out.println("please enter a position");
int p1 = scan.nextInt() ;
if (p1 < 1 || p1 > listhere.togetSize() )
System.out.println("Invalid position ");
else
list.todeleteAtPos(p1);
break;
default:
/* To display the List */
listhere.todisplay();
System.out.println(" Do you want to continue (Type y or n) ");
choiceone = scan.next().charAt(0);
} while (choiceone == 'Y'|| choiceone == 'y');
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.