Drop Out Stack

Efficient Array Based Drop out Stack

public class DropOutStack implements Stack {
private static final int DEFAULT_CAPACITY = 10;
 private final T[] elements;
 private int top;
 private int count;

public DropOutStack() {
 this(DEFAULT_CAPACITY);
 }

@SuppressWarnings("unchecked")
 public DropOutStack(final int size) {
 assert size > 0;
 this.elements = ((T[]) new Object[size]);
 this.top = 0;
 this.count = 0;
 }

public final void push(final T element) {
 this.elements[top] = element;
 this.top = (this.top + 1) % this.elements.length;
 if (this.count != this.elements.length) {
 this.count++;
 }
 }

public final T pop() {
 if (isEmpty()) {
 throw new CollectionEmptyException();
 }
 this.top = (this.top + this.elements.length - 1) % this.elements.length;
 T result = this.elements[this.top];
 this.elements[this.top] = null;

this.count--;
 return result;
 }

public final int size() {
 return this.count;
 }

public final T peek() {
 return this.elements[this.top - 1];
 }

public final boolean isEmpty() {
 return this.count == 0;
 }
}
Advertisements

One thought on “Drop Out Stack

  1. Could you please provide your email address I have a problem on DROP OUT STACK.
    Design and implement a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, when the n+1 element is pushed, the first element is lost. Implement a drop-out stack using array. (Hint: a circular array implementation would make sense.) Stack should throw an exception if the user attempts to pop an empty stack. Pop operation will also follow circular array implementation.
    NB: (Optional Task) Implement the drop-out stack in Postfix Expression Evaluation.
    Design notes:
    1. You will need a class and then a main that creates String, Character and Integer object type instances of the class for testing purposes.
    2. The program should use generic type StackADT interface with at least push, pop, isEmpty, toSting methods.
    3. During pop operation isEmpty method will use and throw exception while pop operation will attempt in an empty stack (top=0).
    4. The program will take the capacity (n), and a sequence of push or pop operations with appropriate type element and display the full stack as output after executing each push/pop operation. The program will terminate/stop to take input while he receives “End” text.

    Set A-String type Object

    Input:

    Output:
    5
    push “ABC”
    push “DA”
    push “A”
    push “DAC”
    push “ABL”
    push “TAD”
    pop
    pop
    pop
    pop
    pop
    pop
    End
    ABC
    ABC,DA
    ABC,DA,A
    ABC,DA,A,DAC
    ABC,DA,A,DAC,ABL
    TAD,DA,A,DAC,ABL
    TAD,DA,A,DAC
    TAD,DA,A
    TAD,DA
    TAD

    Exception: Stack is Empty

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s