Question: IN JAVA Write an integer linked list class and add some numbers to this linked list. Display the items of this linked list using the

IN JAVA

Write an integer linked list class and add some numbers to this linked list. Display the items of this linked list using the display method. Display the items in reverse order by writing a recursive method.Count the nodes using the recursive method.

public class Linked

{

private Node firstNode; // Reference to first node

public Linked()

{

firstNode = null;

} // end default constructor

/** Sees whether this bag is empty.

@return True if this bag is empty, or false if not. */

public boolean isEmpty()

{

return firstNode == null;

} // end isEmpty

/** Adds a new entry to this bag.

@param newEntry The object to be added as a new entry

@return True if the addition is successful, or false if not. */

public void add(int newEntry) // OutOfMemoryError possible

{

// Add to beginning of chain:

Node newNode = new Node(newEntry);

newNode.next = firstNode; // Make new node reference rest of chain

// (firstNode is null if chain is empty)

firstNode = newNode; // New node is at beginning of chain

} // end add

/** Retrieves all entries that are in this bag.

@return A newly allocated array of all the entries in this bag. */

/** Counts the number of times a given entry appears in this bag.

@param anEntry The entry to be counted.

@return The number of times anEntry appears in this bag. */

// Locates a given entry within this bag.

// Returns a reference to the node containing the entry, if located,

// or null otherwise.

/** Removes all entries from this bag. */

public void clear()

{

while (!isEmpty())

remove();

} // end clear

/** Removes one unspecified entry from this bag, if possible.

@return Either the removed entry, if the removal

was successful, or null. */

public int remove()

{

int result=0;

if (firstNode != null)

{

result = firstNode.data;

firstNode = firstNode.next; // Remove first node from chain

} // end if

return result;

} // end remove

private class Node

{

private int data; // Entry in bag

private Node next; // Link to next node

private Node(int dataPortion)

{

this(dataPortion, null);

} // end constructor

private Node(int dataPortion, Node nextNode)

{

data = dataPortion;

next = nextNode;

} // end constructor

} // end Node

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!