11.6k views
1 vote
****JAVA CODE ONLY*****

Modify the Stacks starter code BELOW. iNSTRUCTIONS AT THE BOTTOM OF CODE.

package stackll;

public class StackLL

// Java program to Implement a stack using singly linked list

public static void main(String[] args)



StackType stack = new StackType(); // insert Stack value stack.push(1);

stack.push(2);

stack.push(3);

stack.push(4);

// display Stack elements while (!stack.isEmpty())



System.out.print(stack.top());

stack.pop();

System.out.println();



// end of main

// end of program class StackLL

// A linked list node

class Node

int data; // integer data

Node next;



// ******** Stack Class ********

class StackType

Node stackTop;

// ******** default constructor ********

// Constructor

StackType()



this.stackTop = null;



// ******** isEmpty ********

// Utility function to check if the stack is empty or not

public boolean isEmpty()



return (stackTop == null);



// ******** isFull ********

// Utility function to check if the stack is full or not

public boolean isFull()



return false;



// ******** push ********

public void push(int x)



if (!this.isFull())



Node node = new Node();

node.data = x;

node.next = stackTop;

stackTop = node;



else

System.out.println("Stack is Full");

// end of push(()

// ******** top ********

// Utility function to return top element in a stack

public int top()



if (!isEmpty())

return stackTop.data;



else

System.out.println("Stack is empty");

return -1;



// end of top()

// ******** pop ********

// Utility function to pop top element from the stack

public void pop() // remove at the beginning



// check for stack underflow

if (!this.isEmpty())

stackTop = stackTop.next;

else

System.out.print("** Can not POP an empty stack ** ");



// end of class StackType

****JAVA CODE ONLY*****

Inside of main(), write the Java code to meet the following requirements: PLEASE USE STARTER CODE ABOVE

o Allow the user to enter 10 integers from the keyboard

o Store odd # in oddStack

o Store even # in evenStack

o Traverse and display the oddStack in LIFO

o Traverse and display the evenStack in LIFO

****JAVA CODE ONLY*****

User Marcelo A
by
7.7k points

1 Answer

6 votes

Here's the modified code that allows the user to enter 10 integers from the keyboard, stores odd numbers in the oddStack, stores even numbers in the evenStack, and then traverses and displays both stacks in LIFO (Last In, First Out) order.

```java

import java.util.Scanner;

public class StackLL {

public static void main(String[] args) {

StackType oddStack = new StackType();

StackType evenStack = new StackType();

Scanner scanner = new Scanner(System.in);

System.out.println("Enter 10 integers:");

for (int i = 0; i < 10; i++) {

int num = scanner.nextInt();

if (num % 2 == 0) {

evenStack.push(num);

} else {

oddStack.push(num);

}

}

System.out.println("Odd Stack (LIFO):");

while (!oddStack.isEmpty()) {

System.out.println(oddStack.top());

oddStack.pop();

}

System.out.println("Even Stack (LIFO):");

while (!evenStack.isEmpty()) {

System.out.println(evenStack.top());

evenStack.pop();

}

scanner.close();

}

// A linked list node

static class Node {

int data; // integer data

Node next;

}

// Stack class

static class StackType {

Node stackTop;

// Default constructor

StackType() {

this.stackTop = null;

}

// Utility function to check if the stack is empty or not

public boolean isEmpty() {

return (stackTop == null);

}

// Utility function to push an element to the stack

public void push(int x) {

Node node = new Node();

node.data = x;

node.next = stackTop;

stackTop = node;

}

// Utility function to return the top element in the stack

public int top() {

if (!isEmpty())

return stackTop.data;

else

throw new IllegalStateException("Stack is empty");

}

// Utility function to pop the top element from the stack

public void pop() {

if (!isEmpty())

stackTop = stackTop.next;

else

throw new IllegalStateException("Can not pop an empty stack");

}

}

}

```

This code prompts the user to enter 10 integers, stores odd numbers in the `oddStack`, even numbers in the `evenStack`, and then traverses and displays both stacks in LIFO order.

Note: Make sure to import the necessary packages and add any additional error handling or validation as per your requirements.

User Deej
by
8.5k points