Question

A leaky stack is implemented with an array as its underlying storage. When a push operation...

A leaky stack is implemented with an array as its underlying storage. When a push operation is
performed on a full leaky stack, however, the oldest element in the stack is \leaked" or removed out the bottom
to make room for the new element being pushed. In every other case, a leaky stack behaves the same as a normal
stack.
Write an implementation of the leaky stack data structure. Your class should be generic and implement the
following public methods: push, pop, size, and isEmpty. Your class must also contain at least two constructors:
one where the user does not specify a capacity and a default capacity of 1000 is used, and one where the user
does specify a capacity.

Hint: code should be as below

public class LeakyStack<E> implements Stack<E> {
public static final int DEFAULT = 1000;
private E[] stack;
private int size = 0;
private int stackTop = -1;
public LeakyStack() {
this(DEFAULT);
}
public LeakyStack(int c);
public void push(E e);
public E pop();
public boolean isEmpty();
public int size();
}

// stack interface code is below//

package stacks;

public interface Stack<E> {
   // Interfaces only contain abstract methods, i.e.
   // only the signatures, not the implementation.
   public void push(E e);
   public E pop();
   public int size();
   public boolean isEmpty();
   public E top();
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1
interface Stack<E> {
        // Interfaces only contain abstract methods, i.e.
        // only the signatures, not the implementation.
        public void push(E e);

        public E pop();

        public int size();

        public boolean isEmpty();

        public E top();
}

public class LeakyStack<E> implements Stack<E> {

        public static final int DEFAULT = 1000;

        private E[] stack; // Generic array for stack storage.
        private int stackTop = -1; // Index to top of stack./*** Constructors ***/
        private int size = 0;

        public LeakyStack() {
                this(DEFAULT);
        }

        // Default constructor
        public LeakyStack(int capacity) {
                // Constructor that takes intparameter.
                stack = (E[]) new Object[capacity];
        }

        /*** Required methods from interface ***/
        public int size() {
                return size;
        }

        public boolean isEmpty() {
                return (stackTop == -1);
        }

        public void push(E e) {
                if (size == stack.length) {
                        // shift one element to left.
                        for (int i = 0; i < size - 1; i++) {
                                stack[i] = stack[i + 1];
                        }
                        stack[size - 1] = e;
                } else {
                        stack[++stackTop] = e;
                }
        }

        public E top() {
                if (isEmpty()) {
                        return null;
                }
                return stack[stackTop];
        }

        public E pop() {
                if (isEmpty()) {
                        return null;
                }
                E answer = stack[stackTop];
                stack[stackTop] = null;
                stackTop--;
                return answer;
        }
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
A leaky stack is implemented with an array as its underlying storage. When a push operation...
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