JAVA Using an array of 10 elements String or int Implement b
JAVA
Using an array of 10 elements (String or int) Implement both a Stack and a Queue Submit a separate Driver for each Be sure all operations are functioning properly Example Can\'t \"peek\" at an empty stack or queue (should return null, zero, or let user know the stack/queue is empty) Can\'t push/add when the stack/queue is full (this would result in a stack overflow) Can\'t pop/poll when the stack/queue is emptySolution
ANS:
Stack program:
import java.util.Scanner;
 public class StackDemo
 {
    int[] stack=new int[10];
    int top=-1;
    void push(int ele)
    {
        if(top==stack.length-1)
        {
            System.out.println(\"Stack is OverFlow\");
        }
        else
        {
            top++;
            stack[top]=ele;
        }
    }
    void pop(){
    if(top==-1)
        {
        System.out.println(\"UnderFlow(Stack is empty\");
        }
        else
        {
            int k;
            k=stack[top];
            top--;
            System.out.println(\"Deleted element is \"+k);
        }
    }
    void peek(){
    if(top==-1)
        {
        System.out.println(\"Stack is empty\");
        }
 else
        {
        System.out.println(\"Top Most Element=\"+stack[top]);
        }
    }
    void display(){
        if(top==-1){
        System.out.println(\"underflow\");
    }
        else
        {
            for(int i=0;i<=top;i++)
                System.out.print(stack[i]+\" \");
            System.out.println();//empty line at the end
        }
    }
 public static void main(String args[])
    {
    int ch,ele;
    Scanner sc=new Scanner(System.in);
 StackDemo p=new StackDemo();
    while(true){
    System.out.println(\"Enter 1 for push,2 for pop,3 for peek, 4 for display,5 for exit\");
 ch=sc.nextInt();
    switch(ch)
        {
        case 1:System.out.println(\"Ente element\");ele=sc.nextInt();p.push(ele);break;
        case 2:p.pop();break;
        case 3:p.peek();break;
        case 4:p.display();break;
        case 5:System.exit(0);
        }
    }
    }
 }
Queue program:
import java.util.Scanner;
 public class QueueDemo
 {
    int[] q=new int[10];
    int front=-1,rear=-1;
    void add(int ele)
    {
        if(rear==q.length-1)
        {
            System.out.println(\"Queue is full\");
        }
        else
        {
            rear++;
            q[rear]=ele;
            if(front == -1)
                front++;
        }
    }
    void poll(){
    if(front==-1)
        {
        System.out.println(\"Queue is empty\");
        }
        else
        {
            int k;
            k=q[front];
            if(front==rear)
                front=rear=-1;
            else
            front++;
            System.out.println(\"Deleted element is \"+k);
        }
    }
    void peek(){
    if(front==-1)
        {
        System.out.println(\"Queue is empty\");
        }
 else
        {
        System.out.println(\"Top Most Element=\"+q[front]);
        }
    }
    void display(){
        if(front==-1){
        System.out.println(\"Queue is empty\");
    }
        else
        {
            for(int i=front;i<=rear;i++)
                System.out.print(q[i]+\" \");
            System.out.println();//empty line at the end
        }
    }
 public static void main(String args[])
    {
    int ch,ele;
    Scanner sc=new Scanner(System.in);
 QueueDemo p=new QueueDemo();
    while(true){
    System.out.println(\"Enter 1 for add,2 for poll,3 for peek, 4 for display,5 for exit\");
 ch=sc.nextInt();
    switch(ch)
        {
        case 1:System.out.println(\"Ente element\");ele=sc.nextInt();p.add(ele);break;
        case 2:p.poll();break;
        case 3:p.peek();break;
        case 4:p.display();break;
        case 5:System.exit(0);
        }
    }
    }
 }



