diff --git a/.gitignore b/.gitignore index fb3ac8e..aa5d6b0 100644 --- a/.gitignore +++ b/.gitignore @@ -21,5 +21,12 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +# Intellij .idea/ -*.iml \ No newline at end of file +*.iml +*.iws + +# Maven +log/ +target/ diff --git a/hw2analysis/src/Array.java b/hw2analysis/src/Array.java new file mode 100644 index 0000000..f9a9657 --- /dev/null +++ b/hw2analysis/src/Array.java @@ -0,0 +1,20 @@ +public interface Array { + + void add(E value); + + E get(int index); + + boolean remove(E value); + + boolean contains(E value); + int indexOf(E value); + + int getSize(); + boolean isEmpty(); + + void sortBubble(); + + void sortSelect(); + + void sortInsert(); +} diff --git a/hw2analysis/src/ArrayImpl.java b/hw2analysis/src/ArrayImpl.java new file mode 100644 index 0000000..46c5572 --- /dev/null +++ b/hw2analysis/src/ArrayImpl.java @@ -0,0 +1,151 @@ +import java.util.Arrays; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class ArrayImpl> implements Array { + + private static final int INITIAL_CAPACITY = 16; + + protected E[] data; + protected int currentSize; + + public ArrayImpl() { + this(INITIAL_CAPACITY); + } + + @SuppressWarnings("unchecked") + public ArrayImpl(int initialCapacity) { + this.data = (E[]) new Object[initialCapacity]; + } + + @Override//O(N) + public void add(E value) { + if (currentSize == data.length) { + data = grow(); + } + data[currentSize++] = value; + } + + protected E[] grow() { + return Arrays.copyOf(data, data.length * 2); + } + + @Override//O(1) + public E get(int index) { + if (index < 0 || index >= currentSize) { + throw new IllegalArgumentException("Invalid index value"); + } + return data[index]; + } + + @Override//O(N) + public boolean remove(E value) { + int index = indexOf(value); + if (index == -1) { + return false; + } + + for (int i = index; i < currentSize - 1; i++) { + data[i] = data[i + 1]; + } + + data[currentSize - 1] = null; + currentSize--; + + return true; + } + + @Override + public boolean contains(E value) { + return indexOf(value) != -1; + } + + @Override//O(N) + public int indexOf(E value) { + for (int i = 0; i < currentSize; i++) { + if (value.equals(data[i])) { + return i; + } + } + + return -1; + } + + @Override + public int getSize() { + return currentSize; + } + + @Override + public boolean isEmpty() { + return currentSize == 0; + } + + @Override + public void sortBubble() { + for (int i = 0; i < currentSize - 1; i++) { + for (int j = 0; j < currentSize - 1 - i; j++) { + if ( data[j].compareTo(data[j + 1]) > 0 ) { + swap(j, j + 1); + } + } + } + } + + @Override + public void sortSelect() { + for (int i = 0; i < currentSize - 1; i++) { + int minIndex = i; + for (int j = i + 1; j < currentSize; j++) { + if (data[j].compareTo(data[minIndex]) < 0) { + minIndex = j; + } + } + if (minIndex != i) { + swap(minIndex, i); + } + } + } + + @Override + public void sortInsert() { + for (int i = 1; i < currentSize; i++) { + E temp = data[i]; + + int in = i; + while (in > 0 && data[in - 1].compareTo(temp) >= 0) { + data[in] = data[in -1]; + in--; + } + + data[in] = temp; + } + } + + private void swap(int index1, int index2) { + E temp = data[index1]; + data[index1] = data[index2]; + data[index2] = temp; + } + + @Override + public String toString() { + return Stream.of(data) + .limit(currentSize) +// .map(element -> String.valueOf(element)) + .map(String::valueOf) + .collect(Collectors.joining(", ", "[", "]")); + +// StringBuilder sb = new StringBuilder("["); +// for (int i = 0; i < currentSize; i++) { +// sb.append(data[i]); +// if (i < currentSize - 1) { +// sb.append(", "); +// } +// } +// +// sb.append("]"); +// +// return sb.toString(); + } +} diff --git a/hw2analysis/src/ConcurrencyMainDZ.java b/hw2analysis/src/ConcurrencyMainDZ.java new file mode 100644 index 0000000..ecc6a1c --- /dev/null +++ b/hw2analysis/src/ConcurrencyMainDZ.java @@ -0,0 +1,80 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.concurrent.*; +import java.util.function.Supplier; + +public class ConcurrencyMainDZ { + + private static final int ARRAY_CAPACITY = 1_000_000; + + + public static final int MAX_VALUE = 10_000; + + public static void main(String[] args) throws InterruptedException, TimeoutException, ExecutionException { + Supplier constructor = ArrayImpl::new; +// Supplier constructor = SortedArrayImpl::new; + + Array arr1 = createArray(constructor); + Array arr2 = createArray(constructor); + Array arr3 = createArray(constructor); + + randomInitialize(arr1, arr2, arr3); + + ExecutorService executorService = Executors.newFixedThreadPool(3); + +// List tasks = List.of( +// measureTime(arr1::sortBubble, "Sort Bubble"), +// measureTime(arr2::sortSelect, "Sort Select"), +// measureTime(arr3::sortInsert, "Sort Insert") +// ); + + List tasks = new ArrayList(); + tasks.add(measureTime(arr1::sortBubble, "Sort Bubble")); + tasks.add(measureTime(arr1::sortSelect, "Sort Bubble")); + tasks.add(measureTime(arr1::sortInsert, "Sort Bubble")); + +// tasks.forEach(executorService::execute); + +// for (Runnable task : tasks) { +// executorService.execute(task); +// } + + for (Object task : tasks) { + executorService.execute((Runnable) task); + } + + executorService.shutdown(); + executorService.awaitTermination(1, TimeUnit.MINUTES); + } + + private static void randomInitialize(Array... arrays) { + Random random = new Random(); + + for (int i = 0; i < ARRAY_CAPACITY; i++) { + int value = random.nextInt(MAX_VALUE); + for (Array array : arrays) { + array.add(value); + } + } + } + + private static Runnable measureTime(Runnable action, String actionName) { + return () -> { + long startTime = System.nanoTime(); + action.run(); + long finishTime = System.nanoTime(); + long duration = finishTime - startTime; + + System.out.println(String.format("%s took time: %d ms.", + actionName, + TimeUnit.NANOSECONDS.toMicros(duration))); + }; + + } + + private static Array createArray(Supplier factory) { + return factory.get(); + } + +} \ No newline at end of file diff --git a/hw2analysis/src/Main2.java b/hw2analysis/src/Main2.java new file mode 100644 index 0000000..c264337 --- /dev/null +++ b/hw2analysis/src/Main2.java @@ -0,0 +1,66 @@ +import java.util.concurrent.TimeUnit; + +public class Main2 { + + public static void main(String[] args) { +// testJdkArray(); +// testArray(); + + Array array = new ArrayImpl<>(); + array.add(2); + array.add(1); + array.add(4); + array.add(5); + array.add(3); + + System.out.println(array); +// array.sortBubble(); +// array.sortSelect(); + array.sortInsert(); +// System.nanoTime(); +// TimeUnit.NANOSECONDS.toMillis(); + System.out.println(array); + + } + + private static void testArray() { + // Array array = new ArrayImpl<>(); + Array array = new SortedArrayImpl<>(); + System.out.println(array); + array.add(2); + array.add(1); + array.add(4); + array.add(5); + array.add(3); + + System.out.println(array); + + System.out.println("Find 5 = " + array.contains(5)); + System.out.println("Index of 3 = " + array.indexOf(3)); + System.out.println("Index of 33 = " + array.indexOf(33)); + + System.out.println("Remove 3: " + array.remove(3)); + System.out.println("Remove 33: " + array.remove(33)); + System.out.println("Index of 3 = " + array.indexOf(3)); + + System.out.println(array); + System.out.println("Size is " + array.getSize()); + + System.out.println("----------"); + for (int i = 0; i < array.getSize(); i++) { + System.out.println(array.get(i)); + } + } + + private static void testJdkArray() { + // Integer[] array = new Integer[] {1, 2, 3, 4, 5}; + Integer[] array = {1, 2, 3, 4, 5}; + + +// array[0] = 5; +// array[2] = 48; + for (Integer value : array) { + System.out.println(value); + } + } +} diff --git a/hw2analysis/src/SortedArrayImpl.java b/hw2analysis/src/SortedArrayImpl.java new file mode 100644 index 0000000..fdc3c74 --- /dev/null +++ b/hw2analysis/src/SortedArrayImpl.java @@ -0,0 +1,43 @@ +public class SortedArrayImpl> extends ArrayImpl { + + @Override//O(logN) + public int indexOf(E value) { + int low = 0; + int high = currentSize - 1; + while (low <= high) { + int mid = (low + high) / 2; + if ( data[mid].equals(value) ) { + return mid; + } + else if ( value.compareTo(data[mid]) > 0 ) { + low = mid + 1; + } + else { + high = mid - 1; + } + + } + + return -1; + } + + @Override//O(N) + public void add(E value) { + if (currentSize == data.length) { + data = grow(); + } + + int index; + for (index = 0; index < currentSize; index++) { + if (data[index].compareTo(value) > 0) + break; + } + + for (int i = currentSize; i > index; i--) { + data[i] = data[i - 1]; + } + + data[index] = value; + currentSize++; + } +} diff --git a/hw3/src/Main31.java b/hw3/src/Main31.java new file mode 100644 index 0000000..19dd30e --- /dev/null +++ b/hw3/src/Main31.java @@ -0,0 +1,94 @@ +import queue.Queue; +import stack.Stack; + +public class Main31 { + public static void main(String[] args) { + testQueue(); + System.out.println("-----------------"); + + testStack(); + System.out.println("-----------------"); + + String rString = reverseString("Строка для преобразования"); + System.out.println(rString); + + + } + + private static String reverseString(String string) { + char[] chars = string.toCharArray(); + Stack stack = new Stack<>(chars.length); + for (char aChar : chars) { + stack.push(aChar); + } + StringBuilder sb = new StringBuilder(); + while (!stack.isEmpty()) { + sb.append(stack.pop()); + } + + return sb.toString(); + } + + private static void testStack() { + Stack stack = new Stack(6); + + stack.push(5); + stack.push(4); + stack.push(8); + stack.push(9); + stack.push(7); + stack.push(12); + + System.out.println("Размер stack: " + stack.getSize()); + System.out.println(stack); + + System.out.println(); + + System.out.println("Получение элемента из стека pop(): "); + System.out.println(stack.pop()); + System.out.println("Размер stack: " + stack.getSize()); + System.out.println(stack); + + System.out.println(); + + System.out.println("Получение элемента из стека pop(): "); + System.out.println(stack.pop()); + System.out.println("Размер stack: " + stack.getSize()); + System.out.println(stack); + } + + private static void testQueue() { + Queue queue = new Queue<>(6); + + queue.insert(5); + queue.insert(4); + queue.insert(8); + queue.insert(9); + queue.insert(7); + queue.insert(12); + + System.out.println("Размер queue: " + queue.getSize()); + System.out.println(queue); + + System.out.println(); + + System.out.println("Поиск первого элемента: peek():"); + System.out.println(queue.peek()); + System.out.println(queue); + + + System.out.println(); + + System.out.println("Производим удаление элемента"); + System.out.println(queue.remove()); + System.out.println("Размер queue: " + queue.getSize()); + System.out.println(queue); + + System.out.println(); + + System.out.println("Производим удаление элемента"); + System.out.println(queue.remove()); + System.out.println("Размер queue: " + queue.getSize()); + System.out.println(queue); + } +} diff --git a/hw3/src/queue/Queue.java b/hw3/src/queue/Queue.java new file mode 100644 index 0000000..18384c7 --- /dev/null +++ b/hw3/src/queue/Queue.java @@ -0,0 +1,27 @@ +package queue; + +import resources.ArrayImpl; + +public class Queue> extends ArrayImpl { + public Queue(int capacity) { + super(capacity); + } + + public void insert(E value) { + super.add(value); + } + + public E remove() { + if (isEmpty()) { + throw new RuntimeException("Queue is empty"); + } + E value = peek(); + super.remove(value); + return value; + } + + public E peek() { + E value = get(0); + return value; + } +} diff --git a/hw3/src/resources/Array.java b/hw3/src/resources/Array.java new file mode 100644 index 0000000..ea6307c --- /dev/null +++ b/hw3/src/resources/Array.java @@ -0,0 +1,22 @@ +package resources; + +public interface Array { + + void add(E value); + + E get(int index); + + boolean remove(E value); + + boolean contains(E value); + int indexOf(E value); + + int getSize(); + boolean isEmpty(); + + void sortBubble(); + + void sortSelect(); + + void sortInsert(); +} diff --git a/hw3/src/resources/ArrayImpl.java b/hw3/src/resources/ArrayImpl.java new file mode 100644 index 0000000..611624d --- /dev/null +++ b/hw3/src/resources/ArrayImpl.java @@ -0,0 +1,153 @@ +package resources; + +import java.util.Arrays; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class ArrayImpl> implements Array { + + private static final int INITIAL_CAPACITY = 16; + + protected E[] data; + protected int currentSize; + + public ArrayImpl() { + this(INITIAL_CAPACITY); + } + + @SuppressWarnings("unchecked") + public ArrayImpl(int initialCapacity) { + this.data = (E[]) new Object[initialCapacity]; + } + + @Override//O(N) + public void add(E value) { + if (currentSize == data.length) { + data = grow(); + } + data[currentSize++] = value; + } + + protected E[] grow() { + return Arrays.copyOf(data, data.length * 2); + } + + @Override//O(1) + public E get(int index) { + if (index < 0 || index >= currentSize) { + throw new IllegalArgumentException("Invalid index value"); + } + return data[index]; + } + + @Override//O(N) + public boolean remove(E value) { + int index = indexOf(value); + if (index == -1) { + return false; + } + + for (int i = index; i < currentSize - 1; i++) { + data[i] = data[i + 1]; + } + + data[currentSize - 1] = null; + currentSize--; + + return true; + } + + @Override + public boolean contains(E value) { + return indexOf(value) != -1; + } + + @Override//O(N) + public int indexOf(E value) { + for (int i = 0; i < currentSize; i++) { + if (value.equals(data[i])) { + return i; + } + } + + return -1; + } + + @Override + public int getSize() { + return currentSize; + } + + @Override + public boolean isEmpty() { + return currentSize == 0; + } + + @Override + public void sortBubble() { + for (int i = 0; i < currentSize - 1; i++) { + for (int j = 0; j < currentSize - 1 - i; j++) { + if ( data[j].compareTo(data[j + 1]) > 0 ) { + swap(j, j + 1); + } + } + } + } + + @Override + public void sortSelect() { + for (int i = 0; i < currentSize - 1; i++) { + int minIndex = i; + for (int j = i + 1; j < currentSize; j++) { + if (data[j].compareTo(data[minIndex]) < 0) { + minIndex = j; + } + } + if (minIndex != i) { + swap(minIndex, i); + } + } + } + + @Override + public void sortInsert() { + for (int i = 1; i < currentSize; i++) { + E temp = data[i]; + + int in = i; + while (in > 0 && data[in - 1].compareTo(temp) >= 0) { + data[in] = data[in -1]; + in--; + } + + data[in] = temp; + } + } + + private void swap(int index1, int index2) { + E temp = data[index1]; + data[index1] = data[index2]; + data[index2] = temp; + } + + @Override + public String toString() { + return Stream.of(data) + .limit(currentSize) +// .map(element -> String.valueOf(element)) + .map(String::valueOf) + .collect(Collectors.joining(", ", "[", "]")); + +// StringBuilder sb = new StringBuilder("["); +// for (int i = 0; i < currentSize; i++) { +// sb.append(data[i]); +// if (i < currentSize - 1) { +// sb.append(", "); +// } +// } +// +// sb.append("]"); +// +// return sb.toString(); + } +} diff --git a/hw3/src/stack/Stack.java b/hw3/src/stack/Stack.java new file mode 100644 index 0000000..055557b --- /dev/null +++ b/hw3/src/stack/Stack.java @@ -0,0 +1,29 @@ +package stack; + +import resources.ArrayImpl; + +public class Stack> extends ArrayImpl { + + public Stack(int capacity) { + super(capacity); + } + + public void push(E value) { + super.add(value); + } + + public E pop() { + if (isEmpty()) { + throw new RuntimeException("Stack is Empty!"); + } + int index = getSize() - 1; + E value = super.get(index); + remove(index); + return value; + } + + private void remove(int index) { + super.data[index] = null; + super.currentSize--; + } +} diff --git a/lesson3/src/ru/geekbrains/datastructure/Main3.java b/lesson3/src/ru/geekbrains/datastructure/Main3.java new file mode 100644 index 0000000..a4cd3fe --- /dev/null +++ b/lesson3/src/ru/geekbrains/datastructure/Main3.java @@ -0,0 +1,59 @@ +package ru.geekbrains.datastructure; + +import ru.geekbrains.datastructure.queue.PriorityQueue; +import ru.geekbrains.datastructure.queue.Queue; +import ru.geekbrains.datastructure.queue.QueueImpl; +import ru.geekbrains.datastructure.stack.Stack; +import ru.geekbrains.datastructure.stack.StackImpl; + +public class Main3 { + public static void main(String[] args) { +// testStack(); + testQueue(); + } + + private static void testQueue() { +// Queue queue = new QueueImpl<>(5); + Queue queue = new PriorityQueue<>(5); + addToQueue(queue, 2); + addToQueue(queue, 4); + addToQueue(queue, 1); + addToQueue(queue, 5); + addToQueue(queue, 3); +// addToQueue(queue, 6); + System.out.println("Top of queue is " + queue.peek()); + + while (!queue.isEmpty()) { + System.out.println(queue.remove()); + } + } + + private static void addToQueue(Queue queue, int value) { + if (!queue.isFull()) { + queue.insert(value); + } + } + + private static void testStack() { + Stack stack = new StackImpl<>(5); + addToStack(stack, 1); + addToStack(stack, 2); + addToStack(stack, 3); + addToStack(stack, 4); + addToStack(stack, 5); + addToStack(stack, 6); + + System.out.println("Top of stack is " + stack.peek()); + stack.clear(); + while (!stack.isEmpty()) { + System.out.println(stack.pop()); + } + stack.pop(); + } + + private static void addToStack(Stack stack, int value) { + if (!stack.isFull()) { + stack.push(value); + } + } +} diff --git a/lesson3/src/ru/geekbrains/datastructure/queue/PriorityQueue.java b/lesson3/src/ru/geekbrains/datastructure/queue/PriorityQueue.java new file mode 100644 index 0000000..1157efa --- /dev/null +++ b/lesson3/src/ru/geekbrains/datastructure/queue/PriorityQueue.java @@ -0,0 +1,37 @@ +package ru.geekbrains.datastructure.queue; + +public class PriorityQueue> extends QueueImpl { + + @SuppressWarnings("unchecked") + public PriorityQueue(int maxSize) { + super((E[]) new Object[maxSize]); + } + + @Override + public void insert(E value) { + if (isEmpty()) { + data[size++] = value; + return; + } + int index; + for (index = size - 1; index >= 0; index--) { + if (value.compareTo(data[index]) > 0) { + data[index + 1] = data[index]; + } else { + break; + } + } + data[index + 1] = value; + size++; + } + + @Override + public E remove() { + return data[--size]; + } + + @Override + public E peek() { + return data[size - 1]; + } +} diff --git a/lesson3/src/ru/geekbrains/datastructure/queue/Queue.java b/lesson3/src/ru/geekbrains/datastructure/queue/Queue.java new file mode 100644 index 0000000..d123e32 --- /dev/null +++ b/lesson3/src/ru/geekbrains/datastructure/queue/Queue.java @@ -0,0 +1,15 @@ +package ru.geekbrains.datastructure.queue; + +public interface Queue { + void insert(E value); + + E remove(); + + E peek(); + + boolean isEmpty(); + + boolean isFull(); + + int getSize(); +} diff --git a/lesson3/src/ru/geekbrains/datastructure/queue/QueueImpl.java b/lesson3/src/ru/geekbrains/datastructure/queue/QueueImpl.java new file mode 100644 index 0000000..2037372 --- /dev/null +++ b/lesson3/src/ru/geekbrains/datastructure/queue/QueueImpl.java @@ -0,0 +1,66 @@ +package ru.geekbrains.datastructure.queue; + +public class QueueImpl implements Queue { + private static final int DEFAULT_REAR = -1; + private static final int DEFAULT_FRONT = 0; + protected E[] data; + protected int size; + private int front; + private int rear; + + @SuppressWarnings("unchecked") + public QueueImpl(int maxSize) { + this((E[]) new Object[maxSize]); + } + + protected QueueImpl(E[] data) { + this.data = data; + this.front = DEFAULT_FRONT; + this.rear = DEFAULT_REAR; + } + + @Override//O(1) + public void insert(E value) { + if (isFull()) { + throw new RuntimeException("Queue is full"); + } + if (rear == data.length - 1) { + rear = DEFAULT_REAR; + } + data[++rear] = value; + size++; + } + + @Override//O(1) + public E remove() { + if (isEmpty()) { + throw new RuntimeException("Queue is empty"); + } + if (front == data.length) { + front = DEFAULT_FRONT; + } + E value = data[front++]; + size--; + return value; + } + + @Override + public E peek() { + return data[front]; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean isFull() { + return size == data.length; + } + + @Override + public int getSize() { + return size; + } +} diff --git a/lesson3/src/ru/geekbrains/datastructure/stack/Brackets.java b/lesson3/src/ru/geekbrains/datastructure/stack/Brackets.java new file mode 100644 index 0000000..8abc962 --- /dev/null +++ b/lesson3/src/ru/geekbrains/datastructure/stack/Brackets.java @@ -0,0 +1,51 @@ +package ru.geekbrains.datastructure.stack; + +public class Brackets { + private final String text; + + public Brackets(String text) { + this.text = text; + } + + public void check() { + Stack stack = new StackImpl<>(text.length()); + for (int i = 0; i < text.length(); i++) { + char chr = text.charAt(i); + switch (chr) { + case '(': + case '{': + case '[': + case '<': + stack.push(chr); + break; + case ')': + case '}': + case ']': + case '>': + if (stack.isEmpty()) { + System.out.println("Error: " + chr + " at " + i); + break; + } + Character lastChar = stack.pop(); + if (lastChar == '(' && chr != ')' || + lastChar == '{' && chr != '}' || + lastChar == '<' && chr != '>' || + lastChar == '[' && chr != ']') { + System.out.println("Error: " + chr + " at " + i); + } + break; + default: + break; + } + } + if (!stack.isEmpty()) { + System.out.println("Error: missing right delimeter"); + } + } + + public static void main(String[] args) { +// String text = " public E peek() { return data[size - 1]; }"; + String text = "<()()>"; + new Brackets(text).check(); + } +} diff --git a/lesson3/src/ru/geekbrains/datastructure/stack/Stack.java b/lesson3/src/ru/geekbrains/datastructure/stack/Stack.java new file mode 100644 index 0000000..906051e --- /dev/null +++ b/lesson3/src/ru/geekbrains/datastructure/stack/Stack.java @@ -0,0 +1,17 @@ +package ru.geekbrains.datastructure.stack; + +public interface Stack { + void push(E value); + + E pop(); + + E peek(); + + boolean isEmpty(); + + boolean isFull(); + + int getSize(); + + void clear(); +} diff --git a/lesson3/src/ru/geekbrains/datastructure/stack/StackImpl.java b/lesson3/src/ru/geekbrains/datastructure/stack/StackImpl.java new file mode 100644 index 0000000..16981c2 --- /dev/null +++ b/lesson3/src/ru/geekbrains/datastructure/stack/StackImpl.java @@ -0,0 +1,64 @@ +package ru.geekbrains.datastructure.stack; + +public class StackImpl implements Stack { + private E[] data; + private int maxSize; + private int size; + + public StackImpl(int maxSize) { + this.data = createArray(maxSize); + this.maxSize = maxSize; + } + + @SuppressWarnings("unchecked") + private E[] createArray(int maxSize) { + return (E[]) new Object[maxSize]; + } + + @Override// O(1) + public void push(E value) { + if (isFull()) { + throw new StackOverloadException(); + } + data[size++] = value; + } + + @Override// O(1) + public E pop() { + if (isEmpty()) { + throw new RuntimeException("ru.geekbrains.datastructure.stack.Stack is Empty!"); + } + E value = data[--size]; + data[size] = null; + return value; + } + + @Override// O(1) + public E peek() { + return data[size - 1]; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean isFull() { + return size == data.length; + } + + @Override + public int getSize() { + return size; + } + + @Override// O(1) + public void clear() { + this.data = createArray(maxSize); + this.size = 0; +// while (!isEmpty()) { +// pop(); +// } + } +} diff --git a/lesson3/src/ru/geekbrains/datastructure/stack/StackOverloadException.java b/lesson3/src/ru/geekbrains/datastructure/stack/StackOverloadException.java new file mode 100644 index 0000000..87d8736 --- /dev/null +++ b/lesson3/src/ru/geekbrains/datastructure/stack/StackOverloadException.java @@ -0,0 +1,7 @@ +package ru.geekbrains.datastructure.stack; + +public class StackOverloadException extends RuntimeException { + public StackOverloadException() { + super("ru.geekbrains.datastructure.stack.Stack is full"); + } +}