JAVA LINKED LIST STACK IMPLEMENTATION Two stacks of positive integers are needed
ID: 3884620 • Letter: J
Question
JAVA LINKED LIST STACK IMPLEMENTATION
Two stacks of positive integers are needed, both containing integers with values less than or equal to 1000. One stack contains even integers, the other contains odd integers. The total number of elements in the combined stacks is never more than 200 at anytime. but we cannot predict how many are in each stack. (All of the elements could be in one stack, they could be evenly divided, both stacks could be empty, and so on).
Design ADT (named NumberStack.java) (which is stack in this case).
write an application, generate random 500 numbers between 0 and 5000, then push them (valid numbers) to their corresponding stack.
print the numbers stored in each stack.
Class file include (you must use the names as specified):
NumberStack.java
NumberStackTester.java
Explanation / Answer
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
/* Class LLNode */
class LLNode
{
protected int data;
protected LLNode link;
/* Constructor */
public LLNode()
{
link = null;
data = 0;
}
/* Constructor */
public LLNode(int d,LLNode n)
{
data = d;
link = n;
}
/* Function to set link to next LLNode */
public void setLink(LLNode n)
{
link = n;
}
/* Function to set data to current LLNode */
public void setData(int d)
{
data = d;
}
/* Function to get link to next LLNode */
public LLNode getLink()
{
return link;
}
/* Function to get data from current LLNode */
public int getData()
{
return data;
}
}
/* Class StackUsingLinkedList */
class StackUsingLinkedList
{
protected LLNode top ;
protected int size ;
/* Constructor */
public StackUsingLinkedList()
{
top = null;
size = 0;
}
/* Function to check if stack is empty */
public boolean isEmpty()
{
return top == null;
}
/* Function to get the size of the stack */
public int getSize()
{
return size;
}
/* Function to push an element to the stack */
public void push(int data)
{
LLNode nptr = new LLNode (data, null);
if (top == null)
top = nptr;
else
{
nptr.setLink(top);
top = nptr;
}
size++ ;
}
/* Function to pop an element from the stack */
public int pop()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception") ;
LLNode ptr = top;
top = ptr.getLink();
size-- ;
return ptr.getData();
}
/* Function to check the top element of the stack */
public int peek()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception") ;
return top.getData();
}
/* Function to display the status of the stack */
public void display()
{
System.out.print(" Stack = ");
if (size == 0)
{
System.out.print("Empty ");
return ;
}
LLNode ptr = top;
while (ptr != null)
{
System.out.print(ptr.getData()+" ");
ptr = ptr.getLink();
}
System.out.println();
}
}
class StackUsingLinkedListUse
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of class StackUsingLinkedList */
StackUsingLinkedList ls1 = new StackUsingLinkedList();
StackUsingLinkedList ls2 = new StackUsingLinkedList();
/* Perform Stack Operations */
for(int i=0;i<500;i++){
int randomNumber = ThreadLocalRandom.current().nextInt(0, 5000 + 1);
if(randomNumber<=1000&&randomNumber%2==0){
if(ls1.getSize()<200){
ls1.push(randomNumber);
}
}else if(randomNumber<=1000&&randomNumber%2!=0){
if(ls2.getSize()<200){
ls2.push(randomNumber);
}
}
}
System.out.println("Size of Stack1: " + ls1.getSize());
System.out.println("Size of Stack2: " + ls2.getSize());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.