Please write a Java interface for an integer stack (should have the methods push, pop, toString). Then implement this interface using one of our linked list nodes. Then please write an interface for an integer queue ( should have methods enqueue, dequeue, and toString). Then implement this interface using one of our linked list objects. Please see chapter 3 in the text for a definition of a stack. Please write a program that asks the user for how many numbers the user would like to store. Then ask the user to enter those numbers. Store each number in the stack and queue. After each store, print out the stack and queue. Then one, by one, pop the numbers from the stack, and print out the stack after each pop. Then one by one, dequeue the numbers from the queue and print out the queue after each dequeue.
//Linked_Stack.java
import java.util.Scanner;
interface MyStack
{
public void push();
public void pop();
public String toString();
}
class Node
{
public int value;
public Node nextNode;
public Node(int val)
{
value = val;
nextNode = null;
}
}
class Linked_Stack implements MyStack
{
private Node head;
public Linked_Stack()
{
head = null;
}
public void push()
{
try
{
Scanner in = new Scanner(System.in);
System.out.print("Enter integer to Push: ");
int val = in.nextInt();
Node node = new Node(val);
node.nextNode = head;
head = node;
}
catch(Exception e)
{
System.out.println("Error Occured: input must be integer\n");
}
}
public void pop()
{
if(head == null)
{
System.out.println("No Node to Pop in Stack\n");
}
else
{
Node node =
head;
head =
head.nextNode;
System.out.println("Popped : "+ node.value+" From Stack\n");
}
}
public String toString()
{
if(head==null)
{
return "Stack is empty\n";
}
else
{
Node temp = head;
String out = "\nNodes in Stack are : ";
while(temp != null)
{
out += temp.value + " -> ";
temp = temp.nextNode;
}
out += " NULL\n";
return out;
}
}
}
//StackTester.java
import java.util.Scanner;
class StackTester
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
System.out.println("Stack Implementation using Linked List");
Linked_Stack stack = new Linked_Stack();
int choice=0;
do
{
System.out.println("1.Push\n2.Pop\n3.Display\n4.Exit");
System.out.print("Enter your choice: ");
choice = in.nextInt();
switch(choice)
{
case 1:
stack.push();
break;
case 2:
stack.pop();
System.out.println("Stack
Nodes after pop\n"+stack.toString());
break;
case 3:
System.out.println(stack.toString());
break;
case 4:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid
Choice.");
choice = 0;
}
}
while(choice < 5 && choice>= 0);
}
}
//Sample ouptut
//Linked_Queue.java
import java.util.Scanner;
interface MyQueue
{
public void enqueue();
public void dequeue();
public String toString();
}
class Node
{
public int value;
public Node nextNode;
public Node(int val)
{
value = val;
nextNode = null;
}
}
class Linked_Queue implements MyQueue
{
private Node head;
private Node tail;
public Linked_Queue()
{
head = null;
tail = null;
}
public void enqueue()
{
try
{
Scanner in = new Scanner(System.in);
System.out.print("Enter integer to Enqueue: ");
int val = in.nextInt();
Node node = new Node(val);
if(head == null)
{
head = node;
tail = node;
}
else
{
tail.nextNode = node;
tail = node;
}
}
catch(Exception e)
{
System.out.println("Error Occured: input must be integer\n");
}
}
public void dequeue()
{
if(head == null)
{
System.out.println("No Node to Dequeue in Queue\n");
}
else
{
Node node =
head;
head =
head.nextNode;
System.out.println("Popped : "+ node.value+" From Queue\n");
if(head ==
null)
{
tail = null;
}
}
}
public String toString()
{
if(head==null)
{
return "Queue is empty\n";
}
else
{
Node temp = head;
String out = "\nNodes in Queue are : ";
while(temp != null)
{
out += temp.value + " -> ";
temp = temp.nextNode;
}
out += " NULL\n";
return out;
}
}
}
//QueueTester.java
import java.util.Scanner;
class QueueTester
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
System.out.println("Queue Implementation using Linked List");
Linked_Queue queue = new Linked_Queue();
int choice=0;
do
{
System.out.println("1. Enqueue\n2. Dequeue\n3. Display\n4.
Exit");
System.out.print("Enter your choice: ");
choice = in.nextInt();
switch(choice)
{
case 1:
queue.enqueue();
break;
case 2:
queue.dequeue();
System.out.println("Queue
Nodes after Dequeue:\n"+queue.toString());
break;
case 3:
System.out.println(queue.toString());
break;
case 4:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid
Choice.");
choice = 0;
}
}
while(choice < 5 && choice >= 0);
}
}
Please write a Java interface for an integer stack (should have the methods push, pop, toString)....
Using C Please comment » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...
Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...
e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...
It is C++ problems, please explain your each line of code as well. Task 1: Implement/ Hard Code a Doubly Linked Lists and make it a Stack. Do not use ::list:: class from the STD library for this example. The user will input a string and the program will output the string backwards. Displaying correct Stack implementation. Utilize the conventional static methods as needed. push() pop() empty() peek() peek() Sample Execution Please input String: happy yppah Task 2: Implement /...
Java Write a pseudocode and Java program to implement the Stack using arrays. Write Push(), Pop(),and Display() methods to demonstrate that it works.
(java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E e) // two methods are the same. You could implement either one • •void addFirst(E e) • •E getFirst( ) = E peek( ) // two methods are the same. You could implement either one •E getLast( ) •E removeFirst() •E removeLast() Case 1 : Queue •Create a queue which is an instance of Dequeue. The queue should perform with given following input and print...
help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...
Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...
Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....
Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...