Answer to Question #199824 in Java | JSP | JSF for Rose

Question #199824

B. Identify a real-world scenario for the below task:

 5

 

  A task where the next one to work on is always the one that has been waiting the longest.

i. For the above task, explain your choice of an appropriate data structure for such a task. [3 marks]

ii. Provide the corresponding algorithm for solving such a problem based on the data structure you chose above. You can represent each step diagrammatically. [5 marks]


1
Expert's answer
2021-05-28T10:32:12-0400

As we know that the stack is a data structure in which the last in first out kind of concept is applicable. Or we can say the first element will come out last after the completion of the all the statement. So for this kind of data structure we follow the stack.

class Test
{
    
    static void stack_push(Stack<Integer> stack)
    {
        for(int i = 0; i < 5; i++)
        {
            stack.push(i);
        }
    }
    

    static void stack_pop(Stack<Integer> stack)
    {
        System.out.println("Pop Operation:");

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


    static void stack_peek(Stack<Integer> stack)
    {
        Integer element = (Integer) stack.peek();
        System.out.println("Element on stack top: " + element);
    }
    

    static void stack_search(Stack<Integer> stack, int element)
    {
        Integer pos = (Integer) stack.search(element);

        if(pos == -1)
            System.out.println("Element not found");
        else
            System.out.println("Element is found at position: " + pos);
    }

    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);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS