I need a driver file for my java code I have already written that demonstrates e
ID: 3784188 • Letter: I
Question
I need a driver file for my java code I have already written that demonstrates each of the methods(goToNext, goToPrev, getDataAtCurrent, setDataAtCurrent, insertNodeAfterCurrent, deleteCurrentNode, showList, inList) is working with a double linked list of integers and strings. Please let the answer be typed and NOT a picture of unreadable handwritten code.....
I will provide the java code now:
//GenDoubleLinkedList class
public class GenDoubleLinkedList
{
class ListNode
{
T data;
ListNode nextLink,prevLink;
public ListNode()
{
data=null;
this.nextLink=null;
this.prevLink=null;
}
public ListNode(T data,ListNode next,ListNode prev)
{
this.data=data;
this.nextLink=next;
this.prevLink=prev;
}
}
ListNode head,current;
public GenDoubleLinkedList()
{
this.head=new ListNode();
this.current=this.head;
}
public void goToNext()
{
if(this.current.nextLink!=null)
{
this.current=this.current.nextLink;
}
}
public void goToPrev()
{
if(this.current.prevLink!=null)
{
this.current=this.current.prevLink;
}
}
public T getDataAtCurrent()
{
T result = null;
if(this.current.data!=null)result =this.current.data;
return result;
}
public void setDataAtCurrent(T data)
{
this.current.data=data;
}
public void insertNodeAfterCurrent(T data)
{
ListNode n=new ListNode(data,this.current.nextLink,this.current);
if(this.current!=null)
{
n.nextLink=this.current.nextLink;
this.current.nextLink=n;
}
}
public void deleteCurrentNode()
{
this.current.prevLink.nextLink=this.current.nextLink;
this.current=this.current.nextLink;
}
public void showList()
{
ListNode copy=this.head;
while(copy!=null)
{
System.out.println(copy.data);
copy=copy.nextLink;
}
}
public boolean inList(T data)
{
ListNode copy=this.head;
while(copy!=null)
{
if(copy.data==data)return true;
copy=copy.nextLink;
}
return false;
}
}
Explanation / Answer
// program to implement doubly linked list
import java.util.Scanner;
public class DoubleLinkedList{
static ListNode head = null;
public static void main(String[] args){
DoubleLinkedList list = new DoubleLinkedList();
Scanner in = new Scanner(System.in);
int n, item;
while(true){
System.out.println();
System.out.println("1. Insert an item to list ");
System.out.println("2. Delete an item from list ");
System.out.println("3. Display items of list ");
System.out.println("4. Reverse the list ");
System.out.println("5. Exit ");
System.out.print("Enter your choice: ");
n=in.nextInt();
switch(n){
case 1:
System.out.print("Enter the item to be inserted: ");
item=in.nextInt();
head=list.insert(head, item);
break;
case 2:
System.out.print("Enter the item to be deleted: ");
item=in.nextInt();
head=list.delete(head, item);
break;
case 3:
if(head==null)
System.out.println("List is empty. ");
else
list.display(head);
break;
case 4:
head = list.reverse(head);
break;
case 5:
System.exit(0);
default:
System.out.println("Wrong choice selected. ");
}
}
}
public ListNode insert(ListNode head, int item){
if(head==null){
head = new ListNode(item);
return head;
}
ListNode temp = new ListNode(item);
ListNode ptr = head;
while(ptr.next!=null)
ptr=ptr.next;
ptr.next= temp;
temp.prev = ptr;
return head;
}
public ListNode delete(ListNode head, int item){
if(head == null){
System.out.println("List is empty. ");
return head;
}
if(head.next==null){
if(head.data==item){
head=null;
return head;
}
else{
System.out.println("Item " + item + " not present in list. ");
return head;
}
}
if(head.data == item){
head=head.next;
head.prev=null;
return head;
}
ListNode current = head.next;
while(current.next != null){
if(current.data==item){
current.prev.next = current.next;
current.next.prev = current.prev;
return head;
}
current = current.next;
}
if(current.data == item){
current.prev.next=null;
return head;
}
System.out.println("Item " + item + " not present in list. ");
return head;
}
public void display(ListNode head){
if(head==null)
return;
System.out.print(head.data + " ");
display(head.next);
}
//iterative reverse of list
public ListNode reverse(ListNode head){
if(head == null){
System.out.println("List is empty. ");
return head;
}
ListNode temp, ptr;
temp = head;
ptr = temp.next;
temp.next = null;
temp.prev = ptr;
while(ptr != null){
ptr.prev = ptr.next;
ptr.next = temp;
temp = ptr;
ptr = ptr.prev;
}
head = temp;
System.out.println("List is reversed. ");
return head;
}
}
class ListNode{
int data;
ListNode next;
ListNode prev;
public ListNode(int val){
data=val;
next=null;
prev=null;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.