System.out.println("Stack is Empty,You cannot perform pop...!!");
return -1;
}
else
{
popped_element = stack[top];
top--;
}
returnpopped_element;
}
@Override
publicintpeek()
{
if(isEmptyStack())
{
System.out.println("Stack is Empty..!! (top = -1)");
return -1;
}
else {
returnstack[top];
}
}
publicvoiddisplay_stack()
{
if(isEmptyStack())
{
System.out.println("Stack is Empty..!!");
}
else {
for (inti = top; i >= 0; i--) {
System.out.println(stack[i]);
}
}
System.out.println();
}
publicbooleanisEmptyQueue()
{
if((front == -1 && rear == -1) || front > rear)
{
returntrue;
}
else
{
returnfalse;
}
}
publicbooleanisFullQueue()
{
if(rear == n - 1)
{
returntrue;
}
else
{
returnfalse;
}
}
@Override
publicvoidenqueue(intdata)
{
if (front == -1 && rear == -1) {
front = 0;
rear++;
queue[rear] = data;
} elseif (rear != -1) {
++rear;
queue[rear] = data;
} else {
System.out.println("Queue is full..!!");
}
}
@Override
publicintdequeue()
{
if (isEmptyQueue())
{
System.out.println("Queue is Empty ,Nothing to Dequeue");
return -1;
}
elseif (front == rear)
{
intdata = queue[front];
front = -1;
rear = -1;
returndata;
}
else
{
intdata = queue[front];
front++;
returndata;
}
}
@Override
publicvoiddisplay_queue() {
if (isEmptyQueue())
{
System.out.println("Queue is Empty ,Nothing to Dequeue");
}
else{
for(inti = front;i <= rear;i++){
System.out.print(queue[i]+" ");
}
}
}
}
publicclassstackqueue_try {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
StackQueueobj = newStackQueue();
booleanflag = true;
System.out.println("1)Push element in Stack\n2)Pop element from stack\n3)Display peek of Stack\n4)Display Stack\n5)Enqueue element in Queue\n6)Dequeue element from Queue\n7)Display queue\n8)Exit");
while(flag)
{
System.out.println("Enter choice : ");
intchoice = sc.nextInt();
switch (choice) {
case1 -> {
System.out.println("Enter element to add in Stack : ");
intstack_data = sc.nextInt();
obj.push(stack_data);
}
case2 -> {
intstack_element = obj.pop();
System.out.println("Popped element is : "+stack_element);
System.out.println();
}
case3 -> {
intpeek = obj.peek();
System.out.println("Peek of the stack is : "+peek);
System.out.println();
}
case4 -> {
obj.display_stack();
}
case5 -> {
System.out.println("Enter element to add in queue : ");
intqueue_data = sc.nextInt();
obj.enqueue(queue_data);
}
case6 -> {
intqueue_element = obj.dequeue();
System.out.println("Dequeued element is : "+queue_element);
System.out.println();
}
case7 -> {
obj.display_queue();
}
case8 -> {
flag = false;
}
default -> System.out.println("Enter the valid choice...!!!!");