I started writing the code but I am stuck, I need help please. I am writing a ja
ID: 3650931 • Letter: I
Question
I started writing the code but I am stuck, I need help please. I am writing a java linked list program that takes in 5 numbers 1 2 3 4 5 then deletes a given number in this case 3, please see my code below, I need help completing the program and writing the print method. Thanks.As you see in the code the numbers are 1 2 3 4 5
Output should be: 1 2 4 5
Thanks.
Meredith
See my code below:
public class printNum
{
public static void main(String[] args)
{
intList temp= new intList (1,null);
pointer = temp;
temp = new intList (2, null);
pointer.next = temp;
pointer = temp;
temp = new intList (3, null);
pointer.next=temp;
pointer=temp;
temp = new intList (4, null);
pointer.next=temp;
pointer=temp;
temp = new intList (5, null);
pointer.next=temp;
pointer=temp;
}
public static print()
{
}
Class intList
{
intList head = null;
int item;
intList next;
intList temp;
intList ();
{
if (head == null)
head = this;
this.item = null;
this.next = null;
}
intList (int item0, intList next0)
{
if (head == null)
head = this;
this.item = item0;
this.next = next0;
}
}
Explanation / Answer
Please rate...
Copy all the files into a single directory.
A main() file "LinkedListTest.java" has been provided for testing
=====================================================
Program Node.java
=====================================================
public class Node
{
private int info;
private Node next;
public Node()
{
info = 0;
next = null;
}
public void setInfo(int i)
{
info = i;
}
public void setNext(Node L)
{
next = L;
}
public int getInfo()
{
return info;
}
public Node getNext()
{
return next;
}
}
====================================================
Program LinkedList.java
====================================================
public class LinkedList
{
private Node first;
public LinkedList()
{
first = new Node();
}
public boolean isEmpty()
{
return (first.getNext() == null);
}
public boolean search(int x)
{
Node current = first.getNext();
while(current != null)
{
if (current.getInfo() == x) return true;
current = current.getNext();
}
return false;
}
public void insert(int x)
{
Node p = new Node();
p.setInfo(x);
p.setNext(first.getNext());
first.setNext(p);
}
public void remove(int x)
{
Node old = first.getNext(),p = first;
//Finding the reference to the node before the one to be deleted
boolean found = false;
while (old != null && !found)
{
if (old.getInfo() == x) found = true;
else
{
p = old;
old = p.getNext();
}
}
//if x is in the list, remove it.
if (found) p.setNext(old.getNext());
}
public int length()
{
int l=0;
Node current = first.getNext();
while (current != null)
{
l++;
current = current.getNext();
}
return l;
}
public void clear()
{
first=new Node();
}
public void insertEnd(int item)
{
Node p = new Node();
p.setInfo(item);
p.setNext(first.getNext());
first.setNext(p);
}
public void replace(int location,int item)
{
Node old = first.getNext(),p = first;
int l=1;
while (l<location)
{
p = old;
old = p.getNext();
l++;
}
old.setInfo(item);
p.setNext(old);
}
public int get(int location)
{
int i;
Node current = first.getNext();
for(i=1;i<location;i++)
{
current = current.getNext();
}
return current.getInfo();
}
@Override
public String toString()
{
Node current = first.getNext();
String s="";
while (current != null)
{
s=s+current.getInfo() + " ";
current = current.getNext();
}
s=s+" ";
return s;
}
}
======================================================
Program LinkedListTest.java
======================================================
import java.util.Scanner;
class LinkedListTest
{
public static void main(String args[])
{
LinkedList intList = new LinkedList();
System.out.print("List of numbers before list creation: ");
for (int i =0; i < 10; i++)
{
int info = (int)(Math.random()*10);
System.out.print(info + " ");
intList.insert(info);
}
System.out.print(" List of numbers after Linked List creation: ");
System.out.println(intList.toString());
System.out.println("Length: "+intList.length());
Scanner s=new Scanner(System.in);
int n;
System.out.print("Testing Remove(): Enter a number to be removed: ");
n=s.nextInt();
if(intList.search(n))
{
intList.remove(n);
System.out.println("The element is removed");
}
else System.out.println("The element is not present so it can't be removed");
System.out.print(" List of numbers after removal of number: ");
System.out.println(intList.toString());
System.out.println("Length: "+intList.length());
}
}
======================================================
Sample output: