Question

(JAVA)Define a method getFrequency(anEntry) for the Linked List implementation of stack ADT. The method returns the...

(JAVA)Define a method getFrequency(anEntry) for the Linked List implementation of stack ADT. The method returns the number of occurrences of anEntry in the stack.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.util.LinkedList;


class MyStack{
    LinkedList<Integer> stack;
  
    MyStack(){
        stack = new LinkedList();
    }
  
    public void push(Integer e){
        stack.push(e);
    }
  
    public Integer pop(){
        return stack.pop();
    }
  
    public Integer peek(){
        return stack.peek();
    }
  
    public int frequency(int element){
        int count = 0;
        for(Integer x : stack){
            if(x==element){
                count++;
            }
        }
        return count;
    }
    @Override
    public String toString(){
        return stack.toString();
    }
}
class Main {

    public static void main(String[] args) {

        MyStack stack = new MyStack();
        stack.push(2);
        stack.push(2);
        stack.push(3);
        stack.push(2);
        stack.push(5);
        System.out.println("Stack is "+stack);
        System.out.println(stack.frequency(2));
    }
}


OUTPUT :

Add a comment
Know the answer?
Add Answer to:
(JAVA)Define a method getFrequency(anEntry) for the Linked List implementation of stack ADT. The method returns the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT