What's new

Java SOLVED

Status
Not open for further replies.

BRYLLE XIU

Elite
Joined
Dec 12, 2014
Posts
1,081
Reaction
6,913
Baka matulungan niyo ko mga papsy kung papa ano magawa itong ganitong output using stack operations.

STACKS.png
 

Attachments

lance_

Elite
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.
 

Arjien

Piercing Dark with Light
Contributor
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;
    }
}
 

lance_

Elite
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.

Online statistics

Members online
465
Guests online
818
Total visitors
1,283

Forum statistics

Threads
1,234,051
Posts
21,657,512
Members
1,616,526
Attachments
1,738,761
Profile posts
100,324
Top