Question

Please write a Java interface for an integer stack (should have the methods push, pop, toString)....

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//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

CAWindows\SYSTEM32\cmd.exe Stack Implementation us ing Linked List 1.Push 2.Pop 3.Display 4.Exit Enter your choice: 1 Enter i

//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);
}
}

CAWindows\SYSTEM32\cmd.exe Queue Implementation us ing Linked List 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice

Add a comment
Know the answer?
Add Answer to:
Please write a Java interface for an integer stack (should have the methods push, pop, toString)....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT