โ˜• Java SOLVED

Status
Not open for further replies.

XIU

Elite
Baka matulungan niyo ko mga papsy kung papa ano magawa itong ganitong output using stack operations.

STACKS.webp
 

Attachments

  • STACKS.webp
    STACKS.webp
    11.6 KB · Views: 24
Java:
// Java code for stack implementation

import java.io.*;
import java.util.*;

public class Test
{
    // Pushing element on the top of the stack
    static void stack_push(Stack<Integer> stack)
    {
        for(int i = 0; i < 11; i++)
        {
            stack.push(i);
        }
    }
    
    // Popping element from the top of the stack
    static void stack_pop(Stack<Integer> stack)
    {

        for(int i = 0; i < 11; i++)
        {
            Integer y = (Integer) stack.pop();
            System.out.println(y);
        }
    }

    // Displaying element on the top of the stack
    static void stack_peek(Stack<Integer> stack)
    {
        Integer element = (Integer) stack.peek();
        System.out.println("Element on stack top: " + element);
    }
    
    // Searching element in the stack
    static void stack_search(Stack<Integer> stack, int element)
    {
        Integer pos = (Integer) stack.search(element);

    }


    public static void main (String[] args)
    {
        Stack<Integer> stack = new Stack<Integer>();

        stack_push(stack);
        stack_pop(stack);
        stack_push(stack);
        stack_peek(stack);
        stack_search(stack, 2);
        stack_search(stack, 6);
        System.out.println("Stack: " + stack);
        System.out.println("The size of stack is: " + stack.size());
    }
}

Explore mo na lang.

Reference: Google
 
  • Love
Reactions: XIU
Java:
// Java code for stack implementation

import java.io.*;
import java.util.*;

public class Test
{
    // Pushing element on the top of the stack
    static void stack_push(Stack<Integer> stack)
    {
        for(int i = 0; i < 11; i++)
        {
            stack.push(i);
        }
    }
   
    // Popping element from the top of the stack
    static void stack_pop(Stack<Integer> stack)
    {

        for(int i = 0; i < 11; i++)
        {
            Integer y = (Integer) stack.pop();
            System.out.println(y);
        }
    }

    // Displaying element on the top of the stack
    static void stack_peek(Stack<Integer> stack)
    {
        Integer element = (Integer) stack.peek();
        System.out.println("Element on stack top: " + element);
    }
   
    // Searching element in the stack
    static void stack_search(Stack<Integer> stack, int element)
    {
        Integer pos = (Integer) stack.search(element);

    }


    public static void main (String[] args)
    {
        Stack<Integer> stack = new Stack<Integer>();

        stack_push(stack);
        stack_pop(stack);
        stack_push(stack);
        stack_peek(stack);
        stack_search(stack, 2);
        stack_search(stack, 6);
        System.out.println("Stack: " + stack);
        System.out.println("The size of stack is: " + stack.size());
    }
}

Explore mo na lang.

Reference: Google

Thank you sir, Will explore.
 
BRYLLE XIU complete code improved can repeat process.
one condition ipasa mo yung code as is. pag aralan mo na din. goodluck.!
online compiler : You do not have permission to view the full content of this post. Log in or register now.
Code:
import java.util.*;
public class stack {
    public static void main(String args[]) {
        start();
    }
    public static void start(){
        Scanner scan = new Scanner(System.in);
        int min = 10;
        int max = 20;
        int stack_limit = limit(min, max);
        int size = 0;

        try {
            if (stack_limit >= min & stack_limit <= max) {
                Stack<Integer> stack = new Stack<Integer>();
                System.out.println("Input Stack: ");
                do {
                    if (scan.hasNextInt()) {
                        stack.push(scan.nextInt());
                        size = stack.size();
                        if (size == stack_limit) {
                            System.out.println("Stack : " + stack);
                            System.out.println("The top of the stack : " + stack.lastElement());
                            System.out.println("The size of the stack is: " + stack.size());
                            return;
                        }
                    } else {
                        System.out.println("Invalid Input. Try Again.! !!!");
                        start();
                    }
                } while (size <= stack_limit);
            } else {
                System.out.println("Invalid minimum/maximum input. !!!");
            }
        }finally {
            Scanner r_scan = new Scanner(System.in);
            System.out.println("wanna go back? yes: y  no: n");
            String input = (r_scan.next());
            if (input.equals("y")){
                System.out.println("not done yet.? !!!");
                stack.start();
            }else{
                if(input.equals("n")){
                    System.out.println("ur you done.? !!!");
                }else{
                    System.out.println("wrong input. try again.? !!!");
                    start();
                }
            }
        }
    }
    public static int limit(int min, int max){
        Scanner scan = new Scanner(System.in);
        System.out.println("Minimum = "+ min+""+"    "+"Maximum = "+ max+"");
        System.out.println("Enter how many number stack:");
        int stack_limit = (scan.nextInt());
        return stack_limit;
    }
}
 
BRYLLE XIU complete code improved can repeat process.
one condition ipasa mo yung code as is. pag aralan mo na din. goodluck.!
online compiler : You do not have permission to view the full content of this post. Log in or register now.
Code:
import java.util.*;
public class stack {
    public static void main(String args[]) {
        start();
    }
    public static void start(){
        Scanner scan = new Scanner(System.in);
        int min = 10;
        int max = 20;
        int stack_limit = limit(min, max);
        int size = 0;

        try {
            if (stack_limit >= min & stack_limit <= max) {
                Stack<Integer> stack = new Stack<Integer>();
                System.out.println("Input Stack: ");
                do {
                    if (scan.hasNextInt()) {
                        stack.push(scan.nextInt());
                        size = stack.size();
                        if (size == stack_limit) {
                            System.out.println("Stack : " + stack);
                            System.out.println("The top of the stack : " + stack.lastElement());
                            System.out.println("The size of the stack is: " + stack.size());
                            return;
                        }
                    } else {
                        System.out.println("Invalid Input. Try Again.! !!!");
                        start();
                    }
                } while (size <= stack_limit);
            } else {
                System.out.println("Invalid minimum/maximum input. !!!");
            }
        }finally {
            Scanner r_scan = new Scanner(System.in);
            System.out.println("wanna go back? yes: y  no: n");
            String input = (r_scan.next());
            if (input.equals("y")){
                System.out.println("not done yet.? !!!");
                stack.start();
            }else{
                if(input.equals("n")){
                    System.out.println("ur you done.? !!!");
                }else{
                    System.out.println("wrong input. try again.? !!!");
                    start();
                }
            }
        }
    }
    public static int limit(int min, int max){
        Scanner scan = new Scanner(System.in);
        System.out.println("Minimum = "+ min+""+"    "+"Maximum = "+ max+"");
        System.out.println("Enter how many number stack:");
        int stack_limit = (scan.nextInt());
        return stack_limit;
    }
}
mamaw lodi
 
Status
Not open for further replies.

About this Thread

  • 12
    Replies
  • 1K
    Views
  • 3
    Participants
Last reply from:
Arjienx

Online now

Members online
896
Guests online
699
Total visitors
1,595

Forum statistics

Threads
2,276,945
Posts
28,973,258
Members
1,229,658
Latest member
hihiwae
Back
Top