• Create a class that acts as a doubly linked list node. It should contain all t
ID: 3684333 • Letter: #
Question
• Create a class that acts as a doubly linked list node. It should contain all the required data fields, at least 1 constructor, and a method that prints the hero’s information. • Create a data file with hero information. Each line should be of the following format: heroName Nemesis Time where Nemesis is 1 for the nemesis and 0 for everyone else and Time is in hours. • Modify your list class to include the following stack methods: push, pop, popAll, printList. • Create a class that contains a main method for testing/demonstrating. • In your main method, thoroughly test/demonstrate the methods from your list that you’ve implemented. You should be able to read your data file and use it to push heroes. hint: I find it helpful to use the scanner class for this but you can use something else if you prefer. You can find the scanner class documentation here: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html.
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.