Created
August 13, 2016 10:34
-
-
Save YanchevskayaAnna/98acaf3e17eeb5cf8edc5b374efd3788 to your computer and use it in GitHub Desktop.
Stack
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
package week5.main.stack; | |
/** | |
* Created by sasha on 09.08.2016. | |
*/ | |
public interface IMyStack<E> { | |
void push(E element); | |
E pop(); | |
E peek(); | |
Integer stackSize(); | |
String toString (); | |
} |
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
package week5.main.stack; | |
/** | |
* Created by sasha on 09.08.2016. | |
*/ | |
public class MyArrayStack<E> implements IMyStack<E> { | |
private final int DEFAULT_CAPACITY = 5; | |
private int top; | |
private E[] arrayStack; | |
public MyArrayStack(){ | |
top = 0; | |
arrayStack = (E[])(new Object[DEFAULT_CAPACITY]); | |
} | |
public MyArrayStack(int startCapacity){ | |
top = 0; | |
arrayStack = (E[])(new Object[startCapacity]); | |
} | |
private void expansCapacity(){ | |
E[] largStack = (E[])(new Object[stackSize() * 2]); | |
System.arraycopy(arrayStack, 0, largStack, 0, stackSize()); | |
arrayStack = largStack; | |
} | |
private boolean isEmpty(){ | |
return top == 0; | |
} | |
private boolean isFull(){ | |
return arrayStack.length == stackSize(); | |
} | |
@Override | |
public void push(E element) { | |
if (isFull()){ | |
expansCapacity(); | |
} | |
arrayStack[top] = element; //Yanchevskaya A перепиши в одну строку | |
top++; | |
} | |
@Override | |
public E pop() { | |
E result = null; | |
if (!isEmpty()){ | |
result = arrayStack[top - 1]; | |
arrayStack[top - 1] = null; //Yanchevskaya A перепиши в одну строку | |
top--; | |
}else { | |
System.out.println("stack is empty"); | |
} | |
return result; | |
} | |
@Override | |
public E peek() { | |
E result = null; | |
if (!isEmpty()){ | |
result = arrayStack[top - 1]; | |
} else { | |
System.out.println("stack is empty or index outside of stack"); | |
} | |
return result; | |
} | |
@Override | |
public Integer stackSize() { | |
return top; | |
} | |
@Override | |
public String toString() { | |
String str = ""; //Yanchevskaya A use StringBuilder | |
for (int i = top - 1; i >= 0; i--){ | |
str+=String.valueOf(arrayStack[i]) + "\t"; | |
} | |
return str; | |
} | |
} |
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
package week5.main.stack; | |
/** | |
* Created by Agryzkov on 10.08.2016. | |
*/ | |
public class MyLinkedStack<E> implements IMyStack<E> { | |
private int capasity; | |
private Node<E> top; | |
public MyLinkedStack() { | |
top = null; | |
capasity = 0; | |
} | |
private boolean isEmpty() { | |
return capasity == 0; | |
} | |
@Override | |
public void push(E element) { | |
Node<E> stack = new Node<E>(element); | |
stack.setNext(top); | |
top = stack; | |
capasity++; | |
} | |
@Override | |
public E pop() { | |
E result = null; | |
if (isEmpty()) { | |
System.out.println("stack is empty"); | |
} else { | |
result = top.getValue(); | |
top = top.getNext(); | |
capasity--; | |
} | |
return result; | |
} | |
@Override | |
public E peek() { | |
E result = null; | |
if (isEmpty()) { | |
System.out.println("stack is empty"); | |
} else { | |
result = top.getValue(); | |
} | |
return result; | |
} | |
@Override | |
public Integer stackSize() { | |
return capasity; | |
} | |
@Override | |
public String toString(){ | |
String result = ""; //Yanchevskaya A use StringBuilder | |
Node<E> current = top; | |
while (current != null){ | |
result += String.valueOf(current.getValue()) + "\t"; | |
current = current.getNext(); | |
} | |
return result; | |
} | |
} |
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
package week5.main.stack; | |
/** | |
* Created by sasha on 10.08.2016. | |
*/ | |
public class Node<E> { | |
private E value; | |
private Node<E> next; | |
public Node(){}; | |
public Node(E value) { | |
this.value = value; | |
} | |
public E getValue() {return value;} | |
public void setNext(Node<E> next) { this.next = next; } | |
public Node<E> getNext() { return next; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment