From 68d6a5aafe84bf9e12f2e4eae27d3e427a841956 Mon Sep 17 00:00:00 2001 From: wheelman1995 Date: Mon, 3 Sep 2018 22:55:15 +0300 Subject: [PATCH] add lesson3 homework --- lesson3/src/Array.java | 2 - lesson3/src/Bracket.java | 54 ++++++++++++++++++ lesson3/src/Deque.java | 85 +++++++++++++++++++++++++++ lesson3/src/PriorityQueue.java | 101 +++++++++++++++++++++++++++++++++ lesson3/src/Queue.java | 2 - lesson3/src/RTL.java | 28 +++++++++ lesson3/src/Second.java | 55 +++++++++++++++--- lesson3/src/Stack.java | 4 -- lesson3/src/Third.java | 2 - 9 files changed, 315 insertions(+), 18 deletions(-) create mode 100644 lesson3/src/Bracket.java create mode 100644 lesson3/src/Deque.java create mode 100644 lesson3/src/PriorityQueue.java create mode 100644 lesson3/src/RTL.java diff --git a/lesson3/src/Array.java b/lesson3/src/Array.java index 5298a62..952efae 100644 --- a/lesson3/src/Array.java +++ b/lesson3/src/Array.java @@ -1,5 +1,3 @@ -package jads.two.home; - public class Array { private int arr[]; private int size; diff --git a/lesson3/src/Bracket.java b/lesson3/src/Bracket.java new file mode 100644 index 0000000..f82b83d --- /dev/null +++ b/lesson3/src/Bracket.java @@ -0,0 +1,54 @@ +public class Bracket { + + public static void main(String[] args) { + check("(()<(<){}<>>>)"); + } + + private static void check(CharSequence sequence) { + int size = sequence.length(); + + Stack bracket = new Stack(size); + Stack squareBracket = new Stack(size); + Stack curlyBracket = new Stack(size); + Stack angleBracket = new Stack(size); + + for (int i = 0; i < sequence.length(); i++) { + + char c = sequence.charAt(i); + + switch (c) { + case '(': + bracket.push(1); + break; + case '[': + squareBracket.push(1); + break; + case '{': + curlyBracket.push(1); + break; + case '<': + angleBracket.push(1); + break; + + case ')': + if (bracket.isEmpty()) throw new RuntimeException("wrong syntax"); + bracket.pop(); + break; + case ']': + if (squareBracket.isEmpty()) throw new RuntimeException("wrong syntax"); + squareBracket.pop(); + break; + case '}': + if (curlyBracket.isEmpty()) throw new RuntimeException("wrong syntax"); + curlyBracket.pop(); + break; + case '>': + if (angleBracket.isEmpty()) throw new RuntimeException("wrong syntax"); + angleBracket.pop(); + break; + } + } + if (!(bracket.isEmpty() || squareBracket.isEmpty() || curlyBracket.isEmpty() || angleBracket.isEmpty())) + throw new RuntimeException("wrong syntax"); + } +} diff --git a/lesson3/src/Deque.java b/lesson3/src/Deque.java new file mode 100644 index 0000000..31a96ca --- /dev/null +++ b/lesson3/src/Deque.java @@ -0,0 +1,85 @@ +public class Deque { + private int size; + private int items; + private int[] deque; + private int left; + private int right; + + public Deque(int size) { + this.size = size; + items = 0; + deque = new int[size]; + left = -1; + right = size; + } + + public void attachLeft(int value) { + if (isFull()) { + size *= 2; + int[] temp = new int[size]; + if (left > -1) { + System.arraycopy(deque, 0, temp, 0, left + 1); + } + if (right < deque.length) { + System.arraycopy(deque, right, temp, size - (deque.length - right), deque.length - right); + } + right = size - (deque.length - right); + deque = temp; + } + deque[++left] = value; + items++; + } + + public void attachRight(int value) { + if (isFull()) { + size *= 2; + int[] temp = new int[size]; + + if (left > -1) { + System.arraycopy(deque, 0, temp, 0, left + 1); + } + if (right < deque.length) { + System.arraycopy(deque, right, temp, size - (deque.length - right), deque.length - right); + } + right = size - (deque.length - right); + deque = temp; + } + deque[--right] = value; + items++; + } + + public int removeLeft() { + if (isEmpty() || left == -1) + throw new RuntimeException(); + items--; + return deque[left--]; + } + + public int removeRight() { + if (isEmpty() || right == size) + throw new RuntimeException(); + items--; + return deque[right++]; + } + + public boolean isFull() { + return items == size; + } + + public boolean isEmpty() { + return items == 0; + } + + public int peekLeft() { + if (isEmpty() || left == -1) + throw new RuntimeException(); + return deque[left]; + } + + public int peekRight() { + if (isEmpty() || right == size) + throw new RuntimeException(); + return deque[right]; + } +} + diff --git a/lesson3/src/PriorityQueue.java b/lesson3/src/PriorityQueue.java new file mode 100644 index 0000000..6d24517 --- /dev/null +++ b/lesson3/src/PriorityQueue.java @@ -0,0 +1,101 @@ +import java.util.Arrays; + +public class PriorityQueue { + private int size; + private int head; + private int tail; + private int items; + private int[] priorityQueue; + + public PriorityQueue(int size) { + this.size = size; + head = 0; + tail = -1; + items = 0; + priorityQueue = new int[size]; + } + + public boolean isFull() { + return items == size; + } + + public boolean isEmpty() { + return items == 0; + } + + public int size() { + return items; + } + + public void insert(int value) { + + if (isFull()) { + size *=2; + int[] temp = new int[size]; + + if (head <= tail) { + System.arraycopy(priorityQueue, 0, temp, 0, priorityQueue.length); + } else if (head > tail) { + System.arraycopy(priorityQueue, head, temp, size - (priorityQueue.length - head), priorityQueue.length - head); + System.arraycopy(priorityQueue, 0, temp, 0, tail + 1); + head = size - (priorityQueue.length - head); + } + priorityQueue = temp; + } + + int index = head; + if (head <= tail) { + int n = tail - head + 1; + while (value > priorityQueue[index] && n > 0) { + index++; + n--; + } + } else if(head > tail) { + int n = size - head + tail + 1; + while (value > priorityQueue[index] && n > 0) { + index++; + index %= size; + n--; + } + } + + int elementsToBeShifted = 0; + + if (index >= head) { + elementsToBeShifted = items - (index - head); + } else if (index < head) { + if (index <= tail) { + elementsToBeShifted = tail - index + 1; + } else { + elementsToBeShifted = 0; + } + } + for ( ; elementsToBeShifted > 0 ; elementsToBeShifted--) { + priorityQueue[(index + elementsToBeShifted) % size] = priorityQueue[(index + elementsToBeShifted - 1) % size]; + } + + priorityQueue[index] = value; + tail++; + tail %= size; + + items++; + + System.out.println(Arrays.toString(priorityQueue) + "head " + head + " tail " + tail); + } + + public int remove() { + if (isEmpty()) + throw new RuntimeException(); + int temp = priorityQueue[head++]; + head %= size; + items--; + System.out.println(Arrays.toString(priorityQueue) + "head " + head + " tail " + tail); + return temp; + } + + public int peek() { + if (isEmpty()) + throw new RuntimeException(); + return priorityQueue[head]; + } +} diff --git a/lesson3/src/Queue.java b/lesson3/src/Queue.java index bd2b040..6252b45 100644 --- a/lesson3/src/Queue.java +++ b/lesson3/src/Queue.java @@ -1,5 +1,3 @@ -package jads.three.online; - public class Queue { private int size; private int[] queue; diff --git a/lesson3/src/RTL.java b/lesson3/src/RTL.java new file mode 100644 index 0000000..8633141 --- /dev/null +++ b/lesson3/src/RTL.java @@ -0,0 +1,28 @@ +import java.util.Scanner; +import java.util.Stack; + +public class RTL { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + while (true) { + CharSequence sequence = scanner.nextLine(); + rtl(sequence); + } + + } + + private static void rtl(CharSequence sequence) { + Stack stack = new Stack<>(); + + for (int i = 0; i < sequence.length(); i++) { + stack.push(sequence.charAt(i)); + } + + while (stack.size() > 0) { + System.out.print(stack.pop()); + } + System.out.println(); + } +} + diff --git a/lesson3/src/Second.java b/lesson3/src/Second.java index d34aff3..34687f5 100644 --- a/lesson3/src/Second.java +++ b/lesson3/src/Second.java @@ -1,15 +1,54 @@ -package jads.two.home; - public class Second { public static void main(String[] args) { Array arr = new Array(20); arr.append(1); arr.append(7); - arr.append(-5); arr.append(2); - arr.append(7); arr.append(3); - arr.append(7); arr.append(0); - System.out.println(arr); - arr.deleteAll(7); - arr.pigeon(); +// arr.append(-5); arr.append(2); +// arr.append(7); arr.append(3); +// arr.append(7); arr.append(0); +// System.out.println(arr); +// arr.deleteAll(7); +// arr.pigeon(); +// System.out.println(arr); + arr.delete(2); //в вашем методе delete вываливается exception, если удалять последний элемент массива System.out.println(arr); + + + Deque deque = new Deque(2); + deque.attachLeft(1); + deque.attachLeft(5); + + deque.attachRight(2); + deque.attachRight(11); + deque.attachRight(12); + deque.attachRight(13); + deque.attachRight(14); + deque.attachRight(15); + deque.attachRight(16); + deque.attachRight(17); + deque.attachRight(18); + System.out.println("removeRigh t" + deque.removeRight()); + System.out.println("removeLeft " + deque.removeLeft()); + System.out.println(deque.peekLeft()); + System.out.println(deque.peekRight()); + + PriorityQueue priorityQueue = new PriorityQueue(1); + priorityQueue.insert(4); + priorityQueue.insert(5); + priorityQueue.insert(0); + priorityQueue.insert(-1); + priorityQueue.insert(-1); + + priorityQueue.remove(); + priorityQueue.remove(); + priorityQueue.remove(); + priorityQueue.remove(); + + priorityQueue.insert(100); + priorityQueue.insert(101); + priorityQueue.insert(103); + priorityQueue.insert(88); + priorityQueue.insert(10670); + priorityQueue.insert(44); + priorityQueue.insert(-44); } } diff --git a/lesson3/src/Stack.java b/lesson3/src/Stack.java index e693876..09afba2 100644 --- a/lesson3/src/Stack.java +++ b/lesson3/src/Stack.java @@ -1,7 +1,3 @@ -package jads.three.online; - -import jads.two.home.Array; - public class Stack { private Array stack; private int head; diff --git a/lesson3/src/Third.java b/lesson3/src/Third.java index 13048a6..787eec6 100644 --- a/lesson3/src/Third.java +++ b/lesson3/src/Third.java @@ -1,5 +1,3 @@ -package jads.three.online; - public class Third { public static void main(String[] args) { Stack s = new Stack(5);