Using the following definition for the linked list, implementthe member function
ID: 3611603 • Letter: U
Question
Using the following definition for the linked list, implementthe member functions for the linked list class ofintegers. Use the given main function to test out yourimplementation. This is given below:
{
private class node
{
intdata;
nodenext;
}
private node first;//pointer to thefirst node in the list
public LinkedList()
{ //create an empty linked list }
public boolean empty()
{ // return true if the list is empty, otherwise return false}
public void InsertInFront(int x)
{//insert a value x in front of the list}
public void Delete(ElementType x)
{//if value x is in the list, remove x }
public void Display()
{// Display the data values in the linked list }
public int count()
{//Count and return the number of values in the linked list}
}
Use the following main function to test your implementation:
public static void main(String[] arg)
{
LinkedList X = new LinkedList();
for(int a = 1; a<10; a++)
if((a % 2) == 0)
X.InsertInFront(a);
X.Display();
X.Delete (2);
System.out.println (”list after deleting2:”);
X.Display();
X.Delete(6);
System.out.println (”list after deleting6:”);
X.Display()
if(!X.empty())
System.out.println(”List is notempty.”);
System.out.println(“Number of values: ”+X.count());
}
Explanation / Answer
Use the following main function to test your implementation:
public static void main(String[] arg)
{
LinkedList X = new LinkedList();
for(int a = 1; a<10; a++)
if((a % 2) == 0)
X.InsertInFront(a);
X.Display();
X.Delete (2);
System.out.println (”list after deleting2:”);
X.Display();
X.Delete(6);
System.out.println (”list after deleting6:”);
X.Display()
if(!X.empty())
System.out.println(”List is notempty.”);
System.out.println(“Number of values: ”+X.count());
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.