diff --git a/README.md b/README.md new file mode 100644 index 0000000..e1f112b --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# AlgorithmsAndDataStructures +Created next structures, based on array: Stack, Queue, PriorityQueue, Dequeue; +1. Создать программу, которая переворачивает вводимые строки (читает справа налево). +2. Создать класс для реализации дека. +3. Реализовать расширение массива в стеке при заполнении стека. \ No newline at end of file diff --git a/src/algorithmsAndDataStructures/Main.java b/src/algorithmsAndDataStructures/Main.java index ba454e2..375c5ff 100644 --- a/src/algorithmsAndDataStructures/Main.java +++ b/src/algorithmsAndDataStructures/Main.java @@ -1,8 +1,83 @@ package algorithmsAndDataStructures; +import java.util.Scanner; + public class Main { public static void main(String[] args) { - // write your code here +// doTask(); +// dequeue(); +// priorityQueue(); +// stack(); +// queue(); + } + + public static void queue() { + MyQueue queue = new MyQueue<>(); + for (int i = 1; i < 17; i++) { + queue.insert(i); + } + System.out.println(queue); + queue.remove(); + System.out.println(queue); + System.out.println(queue.size()); + + } + + public static void priorityQueue() { + MyPriorityQueue queue = new MyPriorityQueue<>(); + for (int i = 1; i < 17; i++) { + queue.insert(i); + } + System.out.println(queue); + System.out.println(queue.remove()); + + } + + public static void dequeue() { + MyDequeue queue = new MyDequeue<>(); + queue.insertRight(1); + queue.insertRight(2); + queue.insertRight(3); + queue.insertRight(4); + queue.insertRight(5); + + queue.insertLeft(6); + queue.insertLeft(7); + queue.insertLeft(8); + queue.insertLeft(9); + queue.insertLeft(10); + + System.out.println("left " + queue.peekLeft()); + System.out.println("right " + queue.peekRight()); + System.out.println(queue); + System.out.println(queue.removeLeft()); + System.out.println(queue); + System.out.println(queue.removeRight()); + System.out.println(queue); + } + + public static void stack() { + MyStack stack = new MyStack<>(); + for (int i = 1; i < 17; i++) { + stack.push(i); + } + System.out.println(stack.pop()); + System.out.println(stack); + } + + public static void doTask() { + Scanner scanner = new Scanner(System.in); + MyStack stack = new MyStack<>(); + while (true) { + String s = scanner.nextLine(); + String[] split = s.split(""); + for (int i = 0; i < split.length; i++) { + stack.push(split[i]); + } + for (int i = stack.size(); i > 0; i--) { + System.out.print(stack.pop()); + } + } } -} +} \ No newline at end of file diff --git a/src/algorithmsAndDataStructures/MyDequeue.java b/src/algorithmsAndDataStructures/MyDequeue.java new file mode 100644 index 0000000..3756671 --- /dev/null +++ b/src/algorithmsAndDataStructures/MyDequeue.java @@ -0,0 +1,111 @@ +package algorithmsAndDataStructures; + +import java.util.Arrays; +import java.util.Objects; + + +public class MyDequeue { + private int size; + private E[] list; + private final int DEFAULT_CAPACITY = 10; + private int begin; + private int end; + private int capacity; + + public MyDequeue(int capacity) { + this.capacity = capacity; + if (capacity <= 0) { + throw new IllegalArgumentException("capacity: " + capacity); + } else list = (E[]) new Object[capacity]; + } + + public MyDequeue() { + list = (E[]) new Object[DEFAULT_CAPACITY]; + capacity = DEFAULT_CAPACITY; + } + + public E peekLeft() { + if (isEmpty()) { + throw new RuntimeException("Queue is empty"); + } + return list[begin]; + } + + public void insertRight(E item) { + if (isFull()) { + throw new RuntimeException("queue is full"); + } + size++; + list[end] = item; + end = nextIndex(end); + } + + public int nextIndex(int index) { + return (index + 1) % list.length; + + } + + public int prevIndex(int index) { + if (index == 0) { + return list.length - 1; + } + return index - 1; + } + + public void insertLeft(E item) { + if (isFull()) { + throw new RuntimeException("queue is full"); + } + size++; + list[prevIndex(begin)] = item; + begin = prevIndex(begin); + } + + public E peekRight() { + if (isEmpty()) { + throw new RuntimeException("Queue is empty"); + } + return list[end - 1]; + } + + public E removeLeft() { + E temp = peekLeft(); + size--; + list[begin] = null; + begin = nextIndex(begin); + return temp; + } + + public E removeRight() { + E temp = peekRight(); + size--; + list[end - 1] = null; + end = nextIndex(begin); + return temp; + } + + public boolean isFull() { + return size == list.length; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } + + @Override + public String toString() { + return Arrays.toString(Arrays.stream(list).filter(Objects::nonNull).toArray()); + } +} + +// public String toString() { +// StringBuilder sb = new StringBuilder(); +// for (int i = begin; i != end; i=nextIndex(i)) { +// sb.append(list[i]).append(", "); +// } +// return sb.toString();} + diff --git a/src/algorithmsAndDataStructures/MyPriorityQueue.java b/src/algorithmsAndDataStructures/MyPriorityQueue.java new file mode 100644 index 0000000..a93786d --- /dev/null +++ b/src/algorithmsAndDataStructures/MyPriorityQueue.java @@ -0,0 +1,77 @@ +package algorithmsAndDataStructures; + +import java.util.Arrays; +import java.util.EmptyStackException; + +public class MyPriorityQueue { + private int size; + private E[] list; + private final int DEFAULT_CAPACITY = 10; + private int capacity; + + public MyPriorityQueue(int capacity) { + this.capacity = capacity; + if (capacity <= 0) { + throw new IllegalArgumentException("capacity: " + capacity); + } else list = (E[]) new Comparable[capacity]; + } + + public MyPriorityQueue() { + list = (E[]) new Comparable[DEFAULT_CAPACITY]; + capacity = DEFAULT_CAPACITY; + } + + public void insert(E item) { + if (isFull()) { + capacity = capacity + DEFAULT_CAPACITY / 2 + 1; + E[] newArray = (E[]) new Comparable[capacity]; + System.arraycopy(list, 0, newArray, 0, size); + list=newArray; + } + list[size] = item; + size++; + int i = size - 1; + while (i > 0 && list[i].compareTo(list[i - 1]) > 0) { + swap(i, i - 1); + i--; + } + } + + private void swap(int index1, int index2) { + E temp = list[index1]; + list[index1] = list[index2]; + list[index2] = temp; + } + + public E remove() { + E temp = peek(); + size--; + list[size] = null; + return temp; + } + + public E peek() { + if (isEmpty()) { + throw new EmptyStackException(); + } + return list[size - 1]; + } + + @Override + public String toString() { + return Arrays.toString(Arrays.copyOf(list, size)); + } + + + public boolean isFull() { + return size == list.length; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/src/algorithmsAndDataStructures/MyQueue.java b/src/algorithmsAndDataStructures/MyQueue.java new file mode 100644 index 0000000..53ef0b2 --- /dev/null +++ b/src/algorithmsAndDataStructures/MyQueue.java @@ -0,0 +1,89 @@ +package algorithmsAndDataStructures; + +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.Objects; + +public class MyQueue { + + private T[] list; + private int size; + private final int DEFAULT_CAPACITY = 10; + private int begin; + private int end; + private int capacity; + + public MyQueue(int capacity) { + this.capacity = capacity; + if (capacity <= 0) { + throw new IllegalArgumentException("capacity: " + capacity); + } + list = (T[]) new Object[capacity]; + } + + public MyQueue() { + list = (T[]) new Object[DEFAULT_CAPACITY]; + capacity = DEFAULT_CAPACITY; + } + + public void insert(T item) { + if (isFull()) { + end = size; + capacity = capacity + DEFAULT_CAPACITY / 2 + 1; + T[] newArray1 = (T[]) new Object[capacity]; + System.arraycopy(list, 0, newArray1, 0, size); + list = newArray1; + } + size++; + list[end] = item; + end = nextIndex(end); + } + + public T remove() { + T temp = peek(); + size--; + list[begin] = null; + begin = nextIndex(begin); + return temp; + } + + public T peek() { + if (isEmpty()) { + throw new RuntimeException("queue is empty"); + } + return list[begin]; + } + + private int nextIndex(int index) { + return (index + 1) % list.length; + } + + public boolean isFull() { + return size == list.length; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (end == begin) { + for (int i = begin; i < size; i++) { + sb.append(list[i]).append(","); + } + } else { + for (int i = begin; i != end; i = nextIndex(i)) { + sb.append(list[i]).append(", "); + } + } + return sb.toString(); + +// return Arrays.toString(Arrays.stream(list).filter(Objects::nonNull).toArray()); + } +} \ No newline at end of file diff --git a/src/algorithmsAndDataStructures/MyStack.java b/src/algorithmsAndDataStructures/MyStack.java new file mode 100644 index 0000000..234a6ce --- /dev/null +++ b/src/algorithmsAndDataStructures/MyStack.java @@ -0,0 +1,66 @@ +package algorithmsAndDataStructures; + +import java.util.Arrays; +import java.util.EmptyStackException; + + +public class MyStack { + private int size; + private E[] list; + private final int DEFAULT_CAPACITY = 10; + private int capacity; + + public MyStack(int capacity) { + this.capacity = capacity; + if (capacity <= 0) { + throw new IllegalArgumentException("capacity: " + capacity); + } else list = (E[]) new Object[capacity]; + } + + public MyStack() { + list = (E[]) new Object[DEFAULT_CAPACITY]; + capacity = DEFAULT_CAPACITY; + } + + public E pop() { + E temp = pick(); + size--; + list[size] = null; + return temp; + } + + public E pick() { + if (isEmpty()) { + throw new EmptyStackException(); + } + return list[size - 1]; + } + + public void push(E item) { + if (isFull()) { + capacity = capacity + DEFAULT_CAPACITY / 2 + 1; + E[] newList = (E[]) new Object[capacity]; + System.arraycopy(list, 0, newList, 0, size); + list = newList; + } + list[size] = item; + size++; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } + + public boolean isFull() { + return size == list.length; + } + + @Override + public String toString() { + return Arrays.toString(Arrays.copyOf(list, size)); + } +}