As part of the post, provide a snippet of code that utilizes the Pop and Peek operations on a stack that is not empty. Provide an explanation of your snippet. Your initial post must be a minimum of...

1 answer below »
Please see attachment requesting a Java program with zip file and a word document.


As part of the post, provide a snippet of code that utilizes the Pop and Peek operations on a stack that is not empty. Provide an explanation of your snippet. Your initial post must be a minimum of 250 words. In this post, utilize the queue function by creating a Java program that uses the add(), element(), offer(), peek(), and poll() methods for the queue function. Post your code and provide an explanation of your code and what each method is performing in relationship to your code. Your initial post must be a minimum of 250 words.
Answered 14 days AfterFeb 03, 2022

Answer To: As part of the post, provide a snippet of code that utilizes the Pop and Peek operations on a stack...

Manikandan answered on Feb 11 2022
100 Votes
Stack:
    Stack is a type of ADT(Abstract Data Type). And it is a Linear Data structure. Which will be used for storage elements. Which will follow LIFO(Last In First Out) principle. Here it will insert a
n element from the 0 indexes then the top index value will get increased. That is called the top element.
    Stack will offer many operation Like push(), pop(), peek() etc.
Push() -> this is one of operation of stack. This function is used to insert an element into the stack. Before inserting it will check whether the stack is filled or not. If it is filled then it will throw an error as Size exceeds.
    /**
     *
     * @param element
     * push is an operation is used to insert an element into a stack
     * which will have an element as an input
     * once size is exceeded it will say size is exceeded.
     */
    public void push(E element) {
        if(top>=(size-1)) {
            throw new RuntimeException("Size Exceeds");
        }
        
        stElements[++top] = element;    
        
        System.out.println("Item "+element+" is pushed");
    }
Pop() -> this an another function. This function is used to delete the top element and return that. Before deleting the element it will check whether the stack is empty or not. If it is empty then will show stack is empty. If it is not empty then will delete the top element and return it from the stack
/**
     * pop is an operation and it is used to delete the recently inserted element(last element)
     * if all the elements are popped out then it will say stack is empty
     * @return
     */
    public E pop() {
        //if stack is empty then it will throw error
        if(top==-1) {
            throw new RuntimeException("Stack is empty");
        }
        
        E value = (E)stElements[top];
        stElements[top] = null;
        top--;
        System.out.println("Item "+ value +" is poped");
        return value;
    }
Peek() -> this is an another function in stack. This is used to retrieve the top element. Before retrieving the element it will check the stack is empty or not. If not empty then it will retrieve the top element.
/**
     *
     * @return
    ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here