Last active
April 7, 2017 14:01
-
-
Save chicobentojr/8992d9b3b336837be49156f59be23dc3 to your computer and use it in GitHub Desktop.
MyStack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package mystack; | |
/** | |
* | |
* @author guest-BJ2kKf | |
*/ | |
public class EEmptyStack extends Exception { | |
public EEmptyStack() { | |
super("Stack is empty."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package mystack; | |
/** | |
* | |
* @author guest-BJ2kKf | |
*/ | |
public class EInvalidConstant extends Exception { | |
public EInvalidConstant() { | |
super("Constant parameter should be greater than 0."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package mystack; | |
/** | |
* | |
* @author guest-BJ2kKf | |
*/ | |
public interface IStack { | |
Object pop() throws Exception; | |
void push(Object o); | |
Object top() throws Exception; | |
int size(); | |
boolean isEmpty(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package mystack; | |
/** | |
* | |
* @author guest-BJ2kKf | |
*/ | |
public class MyStack { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) throws Exception { | |
// TODO code application logic here | |
Stack s = new Stack(); | |
for (int i = 0; i < 55; i++) { | |
s.push(i); | |
} | |
System.out.println("My stack: " + s.size()); | |
while (!s.isEmpty()) { | |
int r = (int) s.pop(); | |
System.out.println("Returns from stack: " + r); | |
} | |
System.out.println("My stack: " + s.size()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package mystack; | |
/** | |
* | |
* @author guest-BJ2kKf | |
*/ | |
public class Stack implements IStack{ | |
private int size; | |
private Object[] stack; | |
private int constant; | |
public Stack() throws Exception { | |
this(0); | |
} | |
public Stack(int constant) throws Exception { | |
if (constant < 0) { | |
throw new EInvalidConstant(); | |
} | |
this.constant = constant; | |
this.size = 0; | |
this.stack = new Object[20]; | |
} | |
@Override | |
public Object pop() throws Exception { | |
if (this.isEmpty()) { | |
throw new EEmptyStack(); | |
} | |
Object o = this.stack[--size]; | |
this.stack[size] = null; | |
return o; | |
} | |
@Override | |
public void push(Object o) { | |
if (size == this.stack.length) { | |
if (this.constant == 0) { | |
this.duplicate(); | |
} | |
else { | |
this.expand(); | |
} | |
} | |
this.stack[size++] = o; | |
} | |
@Override | |
public Object top() throws Exception { | |
if (this.isEmpty()) { | |
throw new EEmptyStack(); | |
} | |
return this.stack[size]; | |
} | |
@Override | |
public int size() { | |
return this.size; | |
} | |
@Override | |
public boolean isEmpty() { | |
return this.size == 0; | |
} | |
private void expand() { | |
this.stack = this.makeStack(this.stack, this.size + this.constant); | |
} | |
private void duplicate() { | |
this.stack = this.makeStack(this.stack, this.size * 2); | |
} | |
private Object[] makeStack(Object[] stack, int newSize) { | |
Object[] s = new Object[newSize]; | |
for (int i = 0; i < stack.length; i++) { | |
s[i] = stack[i]; | |
} | |
return s; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment