Hey guys Have doing some java task today but i have some trouble with just one o
ID: 3642321 • Letter: H
Question
Hey guys
Have doing some java task today but i have some trouble with just one of them. Like i was asking before the same problem with this like some others. Im only getting errors... like before on some other tasksand the program is just messy.
Task im talking about is this one:
// ***************************************************************
// DoubleLinked.java
// A class using a doubly linked list to represent a list of integers.
// ***************************************************************
public class DoubleLinked
{
private IntNode list;
// ----------------------------------------------
// Constructor -- initializes list
// ----------------------------------------------
public DoubleLinked()
{
list = null;
}
// ----------------------------------------------
// Prints the list elements
// ----------------------------------------------
public void print()
{
for (IntNode temp = list; temp != null; temp = temp.next)
System.out.println(temp.val);
}
// ----------------------------------------------
// Adds new element to front of list
// ----------------------------------------------
public void addToFront(int val)
{
IntNode newNode = new IntNode(val);
newNode.next = list;
if (list != null)
list.prev = newNode;
list = newNode;
}
//***************************************************************
// An inner class that represents a list element.
//***************************************************************
private class IntNode
{
public int val;
public IntNode next;
public IntNode prev;
public IntNode(int val)
{
this.val = val;
this.next = null;
this.prev = null;
}
}
}
// ***********************************************************
// DoubleLinkedTest.java
// Driver to test DoubleLinked methods.
// ***********************************************************
import java.util.Scanner;
public class DoubleLinkedTest
{
private static Scanner scan;
private static DoubleLinked list = new DoubleLinked();
//----------------------------------------------------------------
// Creates a list, then repeatedly prints the menu and does what
// the user asks until they quit.
//----------------------------------------------------------------
public static void main(String[] args)
{
scan = new Scanner(System.in);
printMenu();
int choice = scan.nextInt();
while (choice != 0)
{
dispatch(choice);
printMenu();
choice = scan.nextInt();
}
}
//---------------------------------------
// Does what the menu item calls for.
//---------------------------------------
public static void dispatch(int choice)
{
int newVal;
switch(choice)
{
case 0:
System.out.println("Bye!");
break;
case 1: //print
System.out.println("** List elements **");
list.print();
break;
case 2: //add to front
System.out.println("Enter integer to add to front");
newVal = scan.nextInt();
list.addToFront(newVal);
break;
default:
System.out.println("Sorry, invalid choice");
}
}
//-----------------------------------------
// Prints the user's choices
//-----------------------------------------
public static void printMenu()
{
System.out.println(" Menu ");
System.out.println(" ====");
System.out.println("0: Quit");
System.out.println("1: Print list");
System.out.println("2: Add an integer to the front of the list");
System.out.print(" Enter your choice: ");
}
}
Explanation / Answer
please rate
I have written the functions for addToEnd, removeFirst, removeLast and remove. I am not writing the driver program for those as you can easily modify you DoubleLinkedTest.java for those. I think i had already given you the program for IntList please rate that also. This program is also similar to that one main difference is in modifing the list nodes for both previous node and next node.
class IntNode{
private IntNode next;
private IntNode prev;
private int data;
/** Constructor to create a new node. Node must contain value for data*/
public IntNode(int data){
this.data = data;
next = null;
prev = null;
}
/** Used to change data stored in node */
public void changeData(int data){
this.data = data;
}
/**
* Returns the data stored in node
* @return data
*/
public int getData(){
return data;
}
/**
* Returns the next node in linked list
* @return next IntNode
*/
public IntNode getNextNode(){
return next;
}
/**
* Use to change the next node of the current node
* @param next node
*/
public void setNextNode(IntNode next){
this.next = next;
}
public IntNode getPrevNode(){
return prev;
}
public void setPrevNode(IntNode prev){
this.prev = prev;
}
}
public class DoubleLinked {
IntNode head;
public DoubleLinked(){
head = new IntNode(0);
}
public void addToFront(int val){
IntNode newNode = new IntNode(val);
newNode.setNextNode(head.getNextNode());
newNode.setPrevNode(head);
head.setNextNode(newNode);
}
public void addToEnd(int val){
IntNode newNode = new IntNode(val);
IntNode temp = head;
while(temp.getNextNode()!=null){
temp = temp.getNextNode();
}
temp.setNextNode(newNode);
newNode.setPrevNode(temp);
}
public void print(){
IntNode temp = head.getNextNode();
System.out.println("Contents in doubly linked list ");
while(temp!=null){
System.out.println(temp.getData());
temp = temp.getNextNode();
}
}
public void removeFirst(){
IntNode temp = head.getNextNode();
if(head.getNextNode()==null)
return;
head.setNextNode(temp.getNextNode());
if(temp.getNextNode()!=null){
temp.getNextNode().setNextNode(head);
}
}
public void removeLast(){
IntNode temp = head;
while(temp.getNextNode()!=null){
temp = temp.getNextNode();
}
if(temp!=head){
temp.getPrevNode().setNextNode(null);
}
}
public void remove(int oldval){
IntNode temp = head.getNextNode();
while(temp!=null){
if(temp.getData()==oldval){
System.out.println("Value found. Removing node.");
IntNode prev = temp.getPrevNode();
IntNode next = temp.getNextNode();
prev.setNextNode(next);
if(next!=null){
next.setPrevNode(prev);
}
break;
}
temp = temp.getNextNode();
}
}
}