0% found this document useful (0 votes)
13 views

ResizableStack

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

ResizableStack

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//

public interface StackCreate {


void push(int element);
int pop() throws StackIsEmptyException;
int peek();
boolean isEmpty();
boolean isFull();

//
public class ResizableStack implements StackCreate {
private int[] stackData;
private int top;

public ResizableStack(int n) {
stackData = new int[n];
top = -1;
}

@Override
public void push(int element) {
if(isFull()) {
int[] stackData2 = new int[2*stackData.length];
for(int i=0;i<stackData.length;i++) {
stackData2[i]=stackData[i];
}
stackData=stackData2;
}
top++;
stackData[top]=element;

}
@Override
public int pop() throws StackIsEmptyException {
if(isEmpty()) {
throw new StackIsEmptyException("stack raikama aahe");
}
int result = stackData[top];
top--;

return result;
}
@Override
public int peek() {

return stackData[top];
}
@Override
public boolean isEmpty() {
if (top == -1) {
return true;
}else {
return false;
}
}
@Override
public boolean isFull() {
if(top == stackData.length -1) {
return true;
}else {
return false;
}
}

public int getStackData(int a) {


return stackData[a];
}
public int getsize() {
return stackData.length;
}
}

//

public class StackIsEmptyException extends Exception {

public StackIsEmptyException(String msg) {


super(msg);
}

//

public class StackTester {


public static void main(String[] args) throws StackIsEmptyException {
ResizableStack Stack = new ResizableStack(5);

System.out.println(Stack.isEmpty()+" empty");
Stack.push(2);
System.out.println(Stack.isEmpty()+" empty");
Stack.push(4);
Stack.push(2);
System.out.println(Stack.isFull()+" full");
Stack.push(4);
Stack.push(2);
System.out.println(Stack.isFull()+" full");
Stack.push(4);
Stack.push(4);
Stack.push(2);
System.out.println(Stack.peek()+" peek");
Stack.push(7);
Stack.push(4);
System.out.println(Stack.pop()+" poped");
System.out.println(Stack.pop()+" poped");
Stack.pop();

for(int i = 0;i<Stack.getsize();i++) {
System.out.println(Stack.getStackData(i));
}
}

You might also like