Question
JAVA Implement following code using linked list instead of stack. Please use only scanner. Also, java.util.LinkedList shouldn't be used. import java.util.Scanner; class Stack { private
JAVA
Implement following code using linked list instead of stack. Please use only scanner. Also, java.util.LinkedList shouldn't be used.
import java.util.Scanner;
class Stack { private int array[]; private int top;
Stack() { array = new int[10]; top = -1; }
public void push(int a) { if (top < 10) { array[++top] = a; } else { System.out.println("Sorry we can't process any more orders at the moment."); } }
public void pop() { if (top == -1) { System.out.println("There's no orders to process."); } else { System.out.println("Serving order number: " + array[top]); top--; } }
public void display() { if (top == -1) { System.out.println("There's no orders to show."); } else { int i; System.out.println("The list of orders is: "); for (i = 0; i <= top; i++) { System.out.println(array[i]); } } } }
public class Doughnuts { private static Scanner sc;
public static void main(String args[]) { Stack obj; obj = new Stack(); int ch, orderId; orderId = 1; ch = -1; while (ch != 4) { System.out.println( " Welcome to the doughnut machine 1: Place an order 2: Recieve the order 3: Display order list 4: Exit "); System.out.print(" Please enter your choice: "); sc = new Scanner(System.in); ch = sc.nextInt(); switch (ch) { case 1: { obj.push(orderId++); break; } case 2: { obj.pop(); break; } case 4: { System.out.println("Thank You for using our service!"); break; } case 3: { obj.display(); break; } default: { System.out.println("Please enter a valid choice!"); break; } } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started