Created
August 13, 2016 10:12
-
-
Save YanchevskayaAnna/82cb5d382a6299d8969741fd9b8602dc to your computer and use it in GitHub Desktop.
Queue
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.queue; | |
/** | |
* Created by Agryzkov on 11.08.2016. | |
*/ | |
public interface IMyQueue<E> { | |
void insert(E element); | |
E remove(); | |
E getFront(); | |
E getRear(); | |
int getSize(); | |
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.queue; | |
/** | |
* Created by Agryzkov on 11.08.2016. | |
*/ | |
public class MyArrayQueue<E> implements IMyQueue<E> { | |
private E[] arrayQueue; | |
private final int DEFAULT_CAPACITY = 5; | |
private int front, rear; | |
public MyArrayQueue() { //Yanchevskaya A можно вызвать внутри конструктор public MyArrayQueue(int capacity) | |
arrayQueue = (E[])(new Object[DEFAULT_CAPACITY]); | |
front = rear = 0; | |
} | |
public MyArrayQueue(int capacity) { | |
arrayQueue = (E[])(new Object[capacity]); | |
front = rear = 0; | |
} | |
@Override | |
public void insert(E element) { | |
if (isFull()) { | |
expansCapacity(); | |
} | |
if (front != 0 && rear == arrayQueue.length - 1) { | |
shiftElement(); | |
} | |
arrayQueue[rear] = element; | |
rear++; | |
} | |
@Override | |
public E remove() { | |
E result = null; | |
if (isEmpty()) { | |
System.out.println("queue is empty"); | |
} else { | |
result = arrayQueue[front]; //Yanchevskaya A. Но сам элемент ты не обнуляешь? | |
front ++; | |
} | |
return result; | |
} | |
@Override | |
public E getFront() { | |
return arrayQueue[front]; | |
} | |
@Override | |
public E getRear() { | |
return arrayQueue[rear - 1]; //Yanchevskaya A. проверка на rear !=0 | |
} | |
@Override | |
public int getSize() { | |
return rear - front; | |
} | |
@Override | |
public String toString() { | |
String str = ""; //Yanchevskaya A. используй StringBuilder | |
for (int i = front; i < rear; i++){ | |
str+=String.valueOf(arrayQueue[i]) + "\t"; | |
} | |
return str; | |
} | |
private boolean isEmpty() { | |
return rear == front; | |
} | |
private boolean isFull() { | |
return arrayQueue.length == getSize(); | |
} | |
private void expansCapacity(){ | |
E[] largQueue = (E[])(new Object[getSize() * 2]); | |
System.arraycopy(arrayQueue, 0, largQueue, 0, arrayQueue.length); | |
arrayQueue = largQueue; | |
} | |
private void shiftElement() { | |
E[] arrTemp = (E[])(new Object[arrayQueue.length]); | |
System.arraycopy(arrayQueue, front, arrTemp, 0, arrayQueue.length); //Yanchevskaya A. посл. параметр - это кол-во копируемых элементов, копируем size | |
arrayQueue = arrTemp; | |
rear = rear - front; | |
front = 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
package week5.main.queue; | |
import week5.main.stack.Node; | |
/** | |
* Created by Agryzkov on 11.08.2016. | |
*/ | |
public class MyLinkedQueue<E> implements IMyQueue<E> { | |
private int capacity; | |
private Node<E> front, rear; | |
public MyLinkedQueue() { | |
front = rear = null; | |
capacity = 0; | |
} | |
public Boolean isEmpty() { | |
return capacity == 0; | |
} | |
@Override | |
public void insert(E element) { | |
Node<E> node = new Node<E>(element); | |
if (isEmpty()) { | |
front = node; //Yanchevskaya A front = rear = node; | |
} else { | |
rear.setNext(node); | |
} | |
rear = node; | |
capacity++; | |
} | |
@Override | |
public E remove() { | |
E result = null; | |
if (isEmpty()) { | |
System.out.println("stack is empty"); | |
} else { | |
result = front.getValue(); //Yanchevskaya A если всего один элемент, то и хвост надо очищать | |
front = front.getNext(); | |
capacity--; | |
} | |
return result; | |
} | |
@Override | |
public E getFront() { | |
E result = null; | |
if (isEmpty()) { | |
System.out.println("stack is empty"); | |
} else { | |
result = front.getValue(); | |
} | |
return result; | |
} | |
@Override | |
public E getRear() { | |
E result = null; | |
if (isEmpty()) { | |
System.out.println("stack is empty"); | |
} else { | |
result = rear.getValue(); | |
} | |
return result; | |
} | |
@Override | |
public int getSize() { | |
return capacity; | |
} | |
@Override | |
public String toString(){ | |
String result = ""; //Yanchevskaya A use StringBuilder | |
Node<E> current = front; | |
while (current != null){ | |
result += String.valueOf(current.getValue()) + "\t"; | |
current = current.getNext(); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment