Question: Please help me on this java coding! Create a program (class DisplayArray ) with two methods to use recursion and a stack to display the
Please help me on this java coding!
Create a program (class DisplayArray ) with two methods to use recursion and a stack to display the elements in an array.
public class DisplayArray { private int array[]= {25,96,87,41};
...
}
Sample main method and inner class Record:
public static void main(String[] args) { DisplayArray da = new DisplayArray(); da.displayArrayRecursively(0,da.array.length-1); System.out.println(); da.displayArrayWithStack(0,da.array.length-1); } private class Record { private int first, last; private Record(int firstIndex, int lastIndex) { first = firstIndex; last = lastIndex; } // end constructor } // end Record
This is StackInterface.java
/**
An interface for the ADT stack.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public interface StackInterface
{
/** Adds a new entry to the top of this stack.
@param newEntry An object to be added to the stack. */
public void push(T newEntry);
/** Removes and returns this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty before the operation. */
public T pop();
/** Retrieves this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty. */
public T peek();
/** Detects whether this stack is empty.
@return True if the stack is empty. */
public boolean isEmpty();
/** Removes all entries from this stack. */
public void clear();
} // end StackInterface
This is LinkedStack
package stack;
public class LinkedStack
{
private Node topNode; // references the first node in the chain
public LinkedStack()
{
topNode = null;
} // end default constructor
public void push(T newEntry)
{
Node newNode = new Node(newEntry, topNode);
topNode = newNode;
}
public T peek()
{
T top = null;
if (topNode != null)
top = topNode.getData();
return top;
}
public T pop()
{
T top = peek();
if (topNode != null)
topNode = topNode.getNextNode();
return top;
}
public boolean isEmpty()
{
return topNode == null;
}
public void clear()
{
topNode = null;
}
private class Node implements java.io.Serializable
{
private T data; // entry in stack
private Node next; // link to next node
private Node(T dataPortion)
{
data = dataPortion;
next = null;
} // end constructor
private Node(T dataPortion, Node linkPortion)
{
data = dataPortion;
next = linkPortion;
} // end constructor
private T getData()
{
return data;
} // end getData
private void setData(T newData)
{
data = newData;
} // end setData
private Node getNextNode()
{
return next;
} // end getNextNode
private void setNextNode(Node nextNode)
{
next = nextNode;
} // end setNextNode
} // end Node
}
I need help for coding for DisplayArray. java
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
