What's new

Java SOLVED

Status
Not open for further replies.

BRYLLE XIU

Forum Guru
Elite
Joined
Dec 12, 2014
Posts
1,175
Reaction
6,590
Points
1,286
Baka matulungan niyo ko mga papsy kung papa ano magawa itong ganitong output using stack operations.

STACKS.png
 

Attachments

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
 
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.
Back
Top