Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ListStack {
- int value;
- int size;
- ListStack next;
- public ListStack () {
- this.size = 0;
- }
- private ListStack (int add, ListStack head) {
- this.value = add;
- this.next = head;
- }
- public void push (int add) {
- this.next = new ListStack(this.value, this.next);
- this.value = add;
- this.size++;
- }
- public int pop() {
- if (this.size > 0) {
- int i = this.value;
- this.value = this.next.value;
- this.next = this.next.next;
- this.size--;
- return i;
- } else {
- throw new EmptyListStackException();
- }
- }
- public int peek() {
- return this.value;
- }
- public void print() {
- String aString = "";
- ListStack p = this;
- for (int i = 1; i < this.size; i++, p = p.next) {
- aString += p.value+" -> ";
- }
- aString += "null";
- System.out.println(aString);
- }
- public class EmptyListStackException extends NullPointerException {
- public EmptyListStackException() {
- super("The list stack is empty, you cannot .pop() from it.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement