From 7b132fcc068eeb2758cb125a4dd8cc8311db4adf Mon Sep 17 00:00:00 2001 From: ksilerman Date: Sat, 22 Dec 2018 17:48:47 +0300 Subject: [PATCH 1/9] =?UTF-8?q?=D0=BA=D0=BE=D0=B4=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82=20=D0=B8=20=D1=81=D0=BE=D1=80?= =?UTF-8?q?=D1=82=D0=B8=D1=80=D1=83=D0=B5=D1=82.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lesson_2/Array.java | 4 +++- lesson_2/ArrayImpl.java | 14 +++++++++++++- lesson_2/Main2.java | 43 ++++++++++++++++++++++++++++++----------- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/lesson_2/Array.java b/lesson_2/Array.java index f3da3ff..f680178 100644 --- a/lesson_2/Array.java +++ b/lesson_2/Array.java @@ -1,6 +1,8 @@ package lesson_2; -ublic interface Array { +public interface Array { + + int[] rand(); void add(int value); diff --git a/lesson_2/ArrayImpl.java b/lesson_2/ArrayImpl.java index af90d9a..362cbe4 100644 --- a/lesson_2/ArrayImpl.java +++ b/lesson_2/ArrayImpl.java @@ -1,6 +1,7 @@ package lesson_2; import java.util.Arrays; +import java.util.Random; public class ArrayImpl implements Array { @@ -12,6 +13,16 @@ public ArrayImpl(int maxSize) { // this.currentSize = 0; } + @Override + public int[] rand() { + + Random r = new Random(); + for (int i = 0; i < data.length; i++) { + data[currentSize++] = r.nextInt(10); + } + return data; + } + @Override public void add(int value) { if (currentSize == data.length) { @@ -97,8 +108,9 @@ public int getSize() { public void display() { System.out.println("Display array:"); for (int i = 0; i < currentSize; i++) { - System.out.println(data[i]); + System.out.print(data[i] + " "); } + System.out.println(""); System.out.println("------------"); } diff --git a/lesson_2/Main2.java b/lesson_2/Main2.java index 8a321b4..9487f7c 100644 --- a/lesson_2/Main2.java +++ b/lesson_2/Main2.java @@ -19,10 +19,41 @@ public class Main2 { public static void main(String[] args) { // testArrayList(); // testArray(); - testArrayImpl(); +// testArrayImpl(); + testArrayRandom(); } + private static void testArrayRandom() { + Array array1 = new ArrayImpl(100000); + array1.rand(); + Array array2 = array1; + Array array3 = array1; + System.out.println("Размер массива = " + array1.getSize()); + System.out.println("Произвожу сортировку пузырьками подождите..."); + long start1 = System.nanoTime(); + array1.sortBubble(); + long finish1 = System.nanoTime(); + System.out.println("Время потраченное на сортировку: " + TimeUnit.NANOSECONDS.toMillis(finish1 - start1) + " мсек"); + System.out.println("---------"); + + System.out.println("Произвожу сортировку выборкой подождите..."); + long start2 = System.nanoTime(); + array2.sortSelect(); + long finish2 = System.nanoTime(); + System.out.println("Время потраченное на сортировку: " + TimeUnit.NANOSECONDS.toMillis(finish2 - start2) + " мсек"); + System.out.println("---------"); + + System.out.println("Произвожу сортировку вставкой подождите..."); + long start3 = System.nanoTime(); + array3.sortInsert(); + long finish3 = System.nanoTime(); + System.out.println("Время потраченное на сортировку: " + TimeUnit.NANOSECONDS.toMillis(finish3 - start3) + " мсек"); + System.out.println("---------"); + } + + + private static void testArrayImpl() { // Array array = new SortedArrayImpl(5); Array array = new ArrayImpl(5); @@ -36,18 +67,8 @@ private static void testArrayImpl() { array.add(6); array.add(3); -// array.sortBubble(); -// array.sortSelect(); - long start = System.nanoTime(); - array.sortInsert(); - long finish = System.nanoTime(); - System.out.println(TimeUnit.NANOSECONDS.toMillis(finish - start)); - array.display(); -// Random r = new Random(); -// r.nextInt(); - System.out.println("contains 3: " + array.contains(3)); System.out.println("a[2] = " + array.get(2)); From e601a6e591cabece2b87eae7c28d02d37b9e36f5 Mon Sep 17 00:00:00 2001 From: ksilerman Date: Tue, 8 Jan 2019 17:19:15 +0300 Subject: [PATCH 2/9] =?UTF-8?q?=D0=BA=D0=BE=D0=B4=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82=20=D0=B8=20=D1=81=D0=BE=D1=80?= =?UTF-8?q?=D1=82=D0=B8=D1=80=D1=83=D0=B5=D1=82.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lesson_3/Deque.java | 12 +++++++ lesson_3/DequeImpl.java | 39 +++++++++++++++++++++ lesson_3/main.java | 75 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 lesson_3/Deque.java create mode 100644 lesson_3/DequeImpl.java create mode 100644 lesson_3/main.java diff --git a/lesson_3/Deque.java b/lesson_3/Deque.java new file mode 100644 index 0000000..ae3f008 --- /dev/null +++ b/lesson_3/Deque.java @@ -0,0 +1,12 @@ +package lesson_3; + +import lesson_3.queue.Queue; + +public interface Deque extends Queue { + + void insertLeft(int value); + void insertRight(int value); + + int removeLeft(); + int removeRight(); +} \ No newline at end of file diff --git a/lesson_3/DequeImpl.java b/lesson_3/DequeImpl.java new file mode 100644 index 0000000..1c57d9f --- /dev/null +++ b/lesson_3/DequeImpl.java @@ -0,0 +1,39 @@ +package lesson_3; + +import lesson_3.queue.QueueImpl; + +public class DequeImpl extends QueueImpl implements Deque { + + public DequeImpl(int maxSize) { + super(maxSize); + } + + @Override + public int removeLeft() { + return super.remove(); + } + + @Override + public void insertRight(int value) { + super.insert(value); + } + + @Override + public void insertLeft(int value) { + if (front - 1 < 0) + front = data.length; + + data[--front] = value; + size++; + } + + @Override + public int removeRight() { + if (rear < 0) + rear = data.length - 1; + + size--; + return data[rear--]; + } + +} diff --git a/lesson_3/main.java b/lesson_3/main.java new file mode 100644 index 0000000..67c9194 --- /dev/null +++ b/lesson_3/main.java @@ -0,0 +1,75 @@ +package lesson_3; + +import java.util.Stack; + +public class main { + + public static void main(String[] args) { + //TASK 1 + String str = "abcd"; + + Stack st = new Stack<>(); + for (int i = 0; i < str.length(); i++) { + st.push(str.charAt(i)); + } + + while ( !st.isEmpty() ) { + System.out.print(st.pop()); + } + System.out.println(); + + + //TASK 2 + Deque deq = new DequeImpl(6); + + insertRight(deq, 1); + insertRight(deq, 2); + insertRight(deq, 3); + insertRight(deq, 4);// 1 2 3 4 + insertLeft(deq, 5); // 5 1 2 3 4 + insertLeft(deq, 6); // 6 5 1 2 3 4 + + removeLeft(deq); //5 1 2 3 4 + removeRight(deq); // 5 1 2 3 + +// displayLeftToRight(deq); // Queue + displayRightToLeft(deq); //Stack + } + + private static void insertRight(Deque deq, int value) { + if ( !deq.isFull() ) { + deq.insertRight(value); + } + } + + private static void insertLeft(Deque deq, int value) { + if ( !deq.isFull() ) { + deq.insertLeft(value); + } + } + + private static void removeRight(Deque deq) { + if ( !deq.isEmpty() ) { + deq.removeRight(); + } + } + + private static void removeLeft(Deque deq) { + if ( !deq.isEmpty() ) { + deq.removeLeft(); + } + } + + private static void displayRightToLeft(Deque deq) { + while ( !deq.isEmpty() ) { + System.out.println(deq.removeRight()); + } + } + + private static void displayLeftToRight(Deque deq) { + while ( !deq.isEmpty() ) { + System.out.println(deq.removeLeft()); + } + } + } + From df5bd420602c8978b76929ec6b946bef519e239a Mon Sep 17 00:00:00 2001 From: ksilerman Date: Tue, 8 Jan 2019 17:45:42 +0300 Subject: [PATCH 3/9] =?UTF-8?q?=D0=BA=D0=BE=D0=B4=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lesson_4/main.java | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 lesson_4/main.java diff --git a/lesson_4/main.java b/lesson_4/main.java new file mode 100644 index 0000000..f7fc1be --- /dev/null +++ b/lesson_4/main.java @@ -0,0 +1,72 @@ +package lesson_4; + +import java.util.Collection; +import java.util.Iterator; + +public class main { + + public static void main(String[] args) { +// testSimpleLinkedList(); +// testTwoSideLinkedList(); + testIterator(); + } + + private static void testIterator() { + java.util.LinkedList linkedList = new java.util.LinkedList(); + linkedList.addFirst(1); + linkedList.addFirst(2); + linkedList.addFirst(3); + + Collection collection = linkedList; + Iterator iterator = collection.iterator(); + + + for (Integer o : collection) { + System.out.println(o); + } + + while (iterator.hasNext()) { + int element = iterator.next(); + System.out.println(element); + } + } + + private static void testSimpleLinkedList() { + LinkedList linkedList = new SimpleLinkedListImpl(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(6); + + System.out.println("Size = " + linkedList.getSize()); + + linkedList.display(); + + linkedList.remove(); + linkedList.remove(3); + + linkedList.display(); + +// for (int value : linkedList) { +// System.out.println(value); +// } + } + private static void testTwoSideLinkedList() { + TwoSideLinkedList linkedList = new TwoSideLinkedListImpl(); + linkedList.addFirst(1); + linkedList.addFirst(2); + linkedList.addFirst(3); + linkedList.addLast(4); + linkedList.addLast(6); + + System.out.println("Size = " + linkedList.getSize()); + + linkedList.display(); + + linkedList.remove(); + linkedList.remove(6); + + linkedList.display(); + } +} \ No newline at end of file From a56063dbd0db64eac491ab3ea27826cf66dd1674 Mon Sep 17 00:00:00 2001 From: ksilerman Date: Tue, 8 Jan 2019 19:09:22 +0300 Subject: [PATCH 4/9] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D1=81=D0=BE=D0=B1?= =?UTF-8?q?=D1=80=D0=B0=D0=BB=20=D0=BF=D0=BE=D0=B4=20=D0=94=D0=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lesson_4/LinkIterator/Link.java | 17 +++++ lesson_4/LinkIterator/LinkInterator.java | 75 +++++++++++++++++++ lesson_4/LinkIterator/LinkedList.java | 35 +++++++++ lesson_4/LinkIterator/main.java | 17 +++++ lesson_4/LinkedList/Link.java | 25 +++++++ lesson_4/LinkedList/LinkedList.java | 47 ++++++++++++ lesson_4/LinkedList/People.java | 56 +++++++++++++++ lesson_4/LinkedList/main.java | 19 +++++ lesson_4/Queue/Link.java | 18 +++++ lesson_4/Queue/LinkedList.java | 41 +++++++++++ lesson_4/Queue/Queue.java | 26 +++++++ lesson_4/Queue/main.java | 17 +++++ lesson_4/StackList/Link.java | 18 +++++ lesson_4/StackList/LinkedList.java | 35 +++++++++ lesson_4/StackList/StackList.java | 25 +++++++ lesson_4/StackList/main.java | 16 +++++ lesson_4/TwoSideLinkedList/Link.java | 20 ++++++ lesson_4/TwoSideLinkedList/LinkedList.java | 83 ++++++++++++++++++++++ lesson_4/TwoSideLinkedList/People.java | 56 +++++++++++++++ lesson_4/TwoSideLinkedList/main.java | 22 ++++++ lesson_4/main.java | 72 ------------------- 21 files changed, 668 insertions(+), 72 deletions(-) create mode 100644 lesson_4/LinkIterator/Link.java create mode 100644 lesson_4/LinkIterator/LinkInterator.java create mode 100644 lesson_4/LinkIterator/LinkedList.java create mode 100644 lesson_4/LinkIterator/main.java create mode 100644 lesson_4/LinkedList/Link.java create mode 100644 lesson_4/LinkedList/LinkedList.java create mode 100644 lesson_4/LinkedList/People.java create mode 100644 lesson_4/LinkedList/main.java create mode 100644 lesson_4/Queue/Link.java create mode 100644 lesson_4/Queue/LinkedList.java create mode 100644 lesson_4/Queue/Queue.java create mode 100644 lesson_4/Queue/main.java create mode 100644 lesson_4/StackList/Link.java create mode 100644 lesson_4/StackList/LinkedList.java create mode 100644 lesson_4/StackList/StackList.java create mode 100644 lesson_4/StackList/main.java create mode 100644 lesson_4/TwoSideLinkedList/Link.java create mode 100644 lesson_4/TwoSideLinkedList/LinkedList.java create mode 100644 lesson_4/TwoSideLinkedList/People.java create mode 100644 lesson_4/TwoSideLinkedList/main.java delete mode 100644 lesson_4/main.java diff --git a/lesson_4/LinkIterator/Link.java b/lesson_4/LinkIterator/Link.java new file mode 100644 index 0000000..6c86cbb --- /dev/null +++ b/lesson_4/LinkIterator/Link.java @@ -0,0 +1,17 @@ +package lesson_4.LinkIterator; + +class Link{ + public String name; + public int age; + + public Link next; + + public Link(String name, int age){ + this.name = name; + this.age = age; + } + + public void display(){ + System.out.println("Name: "+this.name+", age: "+this.age); + } +} diff --git a/lesson_4/LinkIterator/LinkInterator.java b/lesson_4/LinkIterator/LinkInterator.java new file mode 100644 index 0000000..1ae4529 --- /dev/null +++ b/lesson_4/LinkIterator/LinkInterator.java @@ -0,0 +1,75 @@ +package lesson_4.LinkIterator; + +class LinkInterator{ + private Link current; + private Link previous; + private LinkedList list; + + public LinkInterator(LinkedList list){ + this.list = list; + this.reset(); + } + + public void reset(){ + current = list.getFirst(); + previous = null; + } + + public boolean atEnd(){ + return (current.next == null); + } + + public void nextLink(){ + previous = current; + current = current.next; + } + + public Link getCurrent(){ + return current; + } + + public void insertAfter(String name, int age){ + Link newLink = new Link(name, age); + if (list.isEmpty()){ + list.setFirst(newLink); + current = newLink; + } else { + newLink.next = current.next; + current.next = newLink; + nextLink(); + } + } + + public void insertBefore(String name, int age){ + Link newLink = new Link(name, age); + if(previous == null){ + newLink.next = list.getFirst(); + list.setFirst(newLink); + reset(); + } + else{ + newLink.next = previous.next; + previous.next = newLink; + current = newLink; + } + } + + public String deleteCurrent(){ + String name = current.name; + if (previous == null){ + list.setFirst(current.next); + reset(); + } else { + previous.next = current.next; + if (atEnd()){ + reset(); + } else { + current = current.next; + } + } + + return name; + } + +} + diff --git a/lesson_4/LinkIterator/LinkedList.java b/lesson_4/LinkIterator/LinkedList.java new file mode 100644 index 0000000..97d4e09 --- /dev/null +++ b/lesson_4/LinkIterator/LinkedList.java @@ -0,0 +1,35 @@ +package lesson_4.LinkIterator; + +class LinkedList{ + private Link first; + + + public LinkedList(){ + first = null; + + } + + public Link getFirst() { + return first; + } + + public void setFirst(Link first) { + this.first = first; + } + + public LinkInterator getIterator(){ + return new LinkInterator(this); + } + + public boolean isEmpty(){ + return (first == null); + } + + public void display(){ + Link current = first; + while(current != null){ + current.display(); + current = current.next; + } + } +} diff --git a/lesson_4/LinkIterator/main.java b/lesson_4/LinkIterator/main.java new file mode 100644 index 0000000..bbf6ce2 --- /dev/null +++ b/lesson_4/LinkIterator/main.java @@ -0,0 +1,17 @@ +package lesson_4.LinkIterator; + +public class main { + public static void main(String[] args) { + LinkedList list = new LinkedList(); + + LinkInterator itr = new LinkInterator(list); + + itr.insertAfter("Artem", 20); + itr.insertBefore("Sergey", 10); + + list.display(); + } + +} + + diff --git a/lesson_4/LinkedList/Link.java b/lesson_4/LinkedList/Link.java new file mode 100644 index 0000000..8f298f9 --- /dev/null +++ b/lesson_4/LinkedList/Link.java @@ -0,0 +1,25 @@ +package lesson_4.LinkedList; + +class Link { + private T link; + private Link next; + + public Link(T link){ + this.link = link; + } + + public Link getNext() { + return next; + } + + public void setNext(Link next) { + this.next = next; + } + + public T getValue(){ + return link; + } +} + + + diff --git a/lesson_4/LinkedList/LinkedList.java b/lesson_4/LinkedList/LinkedList.java new file mode 100644 index 0000000..d095720 --- /dev/null +++ b/lesson_4/LinkedList/LinkedList.java @@ -0,0 +1,47 @@ +package lesson_4.LinkedList; + +class LinkedList { + private Link first; + + public LinkedList(){ + first = null; + } + + public boolean isEmpty(){ + return (first == null); + } + + public void insert(T link){ + Link l = new Link<>(link); + l.setNext(first); + this.first = l; + } + + public Link delete(){ + Link temp = first; + first = first.getNext(); + return temp; + } + + public void display(){ + Link current = first; + while (current != null) { + System.out.println(current.getValue()); + current = current.getNext(); + } + } + + public T find(T searchNode){ + Link findNode = new Link<>(searchNode); + Link current = first; + while (current != null) { + if (current.getValue().equals(findNode.getValue())){ + return findNode.getValue(); + } + current = current.getNext(); + } + return null; + } +} + + diff --git a/lesson_4/LinkedList/People.java b/lesson_4/LinkedList/People.java new file mode 100644 index 0000000..0eed6ee --- /dev/null +++ b/lesson_4/LinkedList/People.java @@ -0,0 +1,56 @@ +package lesson_4.LinkedList; + +import java.util.Objects; + +class People { + private String name; + private int age; + + public People(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + @Override + public int hashCode() { + int hash = 5; + hash = 53 * hash + Objects.hashCode(this.name); + hash = 53 * hash + this.age; + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final People other = (People) obj; + if (this.age != other.age) { + return false; + } + if (!Objects.equals(this.name, other.name)) { + return false; + } + return true; + } + + @Override + public String toString() { + return "Name: "+this.name+", age: "+this.age; + } +} + diff --git a/lesson_4/LinkedList/main.java b/lesson_4/LinkedList/main.java new file mode 100644 index 0000000..80affea --- /dev/null +++ b/lesson_4/LinkedList/main.java @@ -0,0 +1,19 @@ +package lesson_4.LinkedList; + +public class main { + + public static void main(String[] args) { + lesson_4.LinkedList.LinkedList list = new lesson_4.LinkedList.LinkedList(); + list.insert("Artem"); + list.insert("Roman"); + + System.out.println(list.find("Artem")); + + LinkedList peopleList = new LinkedList<>(); + peopleList.insert(new People("Artem", 22)); + peopleList.insert(new People("Roman", 18)); + + System.out.println(peopleList.find(new People("Artem", 22)).toString()); + } +} + diff --git a/lesson_4/Queue/Link.java b/lesson_4/Queue/Link.java new file mode 100644 index 0000000..5ef4ac2 --- /dev/null +++ b/lesson_4/Queue/Link.java @@ -0,0 +1,18 @@ +package lesson_4.Queue; + +class Link{ + public String name; + public int age; + + public Link next; + + public Link(String name, int age){ + this.name = name; + this.age = age; + } + + public void display(){ + System.out.println("Name: "+this.name+", age: "+this.age); + } +} + diff --git a/lesson_4/Queue/LinkedList.java b/lesson_4/Queue/LinkedList.java new file mode 100644 index 0000000..acfb5ed --- /dev/null +++ b/lesson_4/Queue/LinkedList.java @@ -0,0 +1,41 @@ +package lesson_4.Queue; + +class LinkedList{ + public Link first; + public Link last; + + public LinkedList(){ + first = null; + last = null; + } + + public boolean isEmpty(){ + return (first == null); + } + + public void insert(String name, int age){ + Link newLink = new Link(name, age); + if (this.isEmpty()) + first = newLink; + else + last.next = newLink; + last = newLink; + } + + public String delete(){ + Link temp = first; + if (first.next == null) + last = null; + first = first.next; + return temp.name; + + } + + public void display(){ + Link current = first; + while(current != null){ + current.display(); + current = current.next; + } + } +} diff --git a/lesson_4/Queue/Queue.java b/lesson_4/Queue/Queue.java new file mode 100644 index 0000000..45a4020 --- /dev/null +++ b/lesson_4/Queue/Queue.java @@ -0,0 +1,26 @@ +package lesson_4.Queue; + +class Queue{ + private LinkedList queue; + + public Queue(){ + queue = new LinkedList(); + } + + public boolean isEmpty(){ + return queue.isEmpty(); + } + + public void insert(String name, int age){ + queue.insert(name, age); + } + + public String delete(){ + return queue.delete(); + } + + public void display(){ + queue.display(); + } + +} diff --git a/lesson_4/Queue/main.java b/lesson_4/Queue/main.java new file mode 100644 index 0000000..ca7ba74 --- /dev/null +++ b/lesson_4/Queue/main.java @@ -0,0 +1,17 @@ +package lesson_4.Queue; + +public class main { + public static void main(String[] args) { + Queue q = new Queue(); + q.insert("Artem", 30); + q.insert("Viktor", 20); + q.insert("Sergey", 10); + q.display(); + while (!q.isEmpty()) { + System.out.println("Элемент "+ q.delete()+" удален из стека"); + } + } + +} + + diff --git a/lesson_4/StackList/Link.java b/lesson_4/StackList/Link.java new file mode 100644 index 0000000..ad4faa3 --- /dev/null +++ b/lesson_4/StackList/Link.java @@ -0,0 +1,18 @@ +package lesson_4.StackList; + +class Link{ + public String name; + public int age; + + public Link next; + + public Link(String name, int age){ + this.name = name; + this.age = age; + } + + public void display(){ + System.out.println("Name: "+this.name+", age: "+this.age); + } +} + diff --git a/lesson_4/StackList/LinkedList.java b/lesson_4/StackList/LinkedList.java new file mode 100644 index 0000000..846e2d4 --- /dev/null +++ b/lesson_4/StackList/LinkedList.java @@ -0,0 +1,35 @@ +package lesson_4.StackList; + +class LinkedList{ + public Link first; + + public LinkedList(){ + first = null; + } + + public boolean isEmpty(){ + return (first == null); + } + + public void insert(String name, int age){ + Link newLink = new Link(name, age); + newLink.next = first; + first = newLink; + } + + public Link delete(){ + + Link temp = first; + first = first.next; + return temp; + + } + + public void display(){ + Link current = first; + while(current != null){ + current.display(); + current = current.next; + } + } +} diff --git a/lesson_4/StackList/StackList.java b/lesson_4/StackList/StackList.java new file mode 100644 index 0000000..f45ead0 --- /dev/null +++ b/lesson_4/StackList/StackList.java @@ -0,0 +1,25 @@ +package lesson_4.StackList; + +class StackList{ + private LinkedList list; + public StackList(){ + list = new LinkedList(); + } + + public void push(String name, int age){ + list.insert(name, age); + } + + public String pop(){ + return list.delete().name; + } + + public boolean isEmpty(){ + return list.isEmpty(); + } + + public void display(){ + list.display(); + } + +} diff --git a/lesson_4/StackList/main.java b/lesson_4/StackList/main.java new file mode 100644 index 0000000..d490918 --- /dev/null +++ b/lesson_4/StackList/main.java @@ -0,0 +1,16 @@ +package lesson_4.StackList; + +public class main { + + public static void main(String[] args) { + StackList sl = new StackList(); + sl.push("Artem", 30); + sl.push("Viktor", 20); + sl.push("Sergey", 10); + sl.display(); + while (!sl.isEmpty()) { + System.out.println("Элемент "+ sl.pop()+" удален из стека"); + } + } +} + diff --git a/lesson_4/TwoSideLinkedList/Link.java b/lesson_4/TwoSideLinkedList/Link.java new file mode 100644 index 0000000..3b177fa --- /dev/null +++ b/lesson_4/TwoSideLinkedList/Link.java @@ -0,0 +1,20 @@ +package lesson_4.TwoSideLinkedList; + +class Link { + public String name; + public int age; + + public Link next; + + public Link(String name, int age){ + this.name = name; + this.age = age; + } + + public void display(){ + System.out.println("Name: "+this.name+", age: "+this.age); + } +} + + + diff --git a/lesson_4/TwoSideLinkedList/LinkedList.java b/lesson_4/TwoSideLinkedList/LinkedList.java new file mode 100644 index 0000000..c956a51 --- /dev/null +++ b/lesson_4/TwoSideLinkedList/LinkedList.java @@ -0,0 +1,83 @@ +package lesson_4.TwoSideLinkedList; + +import lesson_4.TwoSideLinkedList.Link; + +class LinkedList { + public Link first; + public Link last; + + public LinkedList(){ + first = null; + last = null; + } + + public boolean isEmpty(){ + return (first == null); + } + + public void insert(String name, int age){ + Link newLink = new Link(name, age); + if (this.isEmpty()) + last = newLink; + newLink.next = first; + first = newLink; + } + + public void insertLast(String name, int age){ + Link newLink = new Link(name, age); + if (this.isEmpty()){ + first = newLink; + } else { + last.next = newLink; + } + last = newLink; + } + public Link delete(){ + + Link temp = first; + if (first.next == null) + last = null; + first = first.next; + return temp; + + } + + public void display(){ + Link current = first; + while(current != null){ + current.display(); + current = current.next; + } + } + public Link find(String name){ + Link current = first; + while(current.name != name){ + if(current.next == null) + return null; + else + current = current.next; + } + return current; + } + + public Link delete(String name){ + Link current = first; + Link previous = first; + while(current.name != name){ + if(current.next == null) + return null; + else{ + previous = current; + current = current.next; + } + } + if(current == first) + first = first.next; + else + previous.next = current.next; + return current; + + } +} + + diff --git a/lesson_4/TwoSideLinkedList/People.java b/lesson_4/TwoSideLinkedList/People.java new file mode 100644 index 0000000..f972630 --- /dev/null +++ b/lesson_4/TwoSideLinkedList/People.java @@ -0,0 +1,56 @@ +package lesson_4.TwoSideLinkedList; + +import java.util.Objects; + +class People { + private String name; + private int age; + + public People(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + @Override + public int hashCode() { + int hash = 5; + hash = 53 * hash + Objects.hashCode(this.name); + hash = 53 * hash + this.age; + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final People other = (People) obj; + if (this.age != other.age) { + return false; + } + if (!Objects.equals(this.name, other.name)) { + return false; + } + return true; + } + + @Override + public String toString() { + return "Name: "+this.name+", age: "+this.age; + } +} + diff --git a/lesson_4/TwoSideLinkedList/main.java b/lesson_4/TwoSideLinkedList/main.java new file mode 100644 index 0000000..42668a3 --- /dev/null +++ b/lesson_4/TwoSideLinkedList/main.java @@ -0,0 +1,22 @@ +package lesson_4.TwoSideLinkedList; + + +public class main { + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.insert("Artem", 30); + list.insert("Misha", 10); + list.insert("Vova", 5); + list.insertLast("Petya", 25); + + list.display(); + System.out.println("Удаление элементов списка"); + + + list.delete("Vova"); + list.display(); + + } +} + diff --git a/lesson_4/main.java b/lesson_4/main.java deleted file mode 100644 index f7fc1be..0000000 --- a/lesson_4/main.java +++ /dev/null @@ -1,72 +0,0 @@ -package lesson_4; - -import java.util.Collection; -import java.util.Iterator; - -public class main { - - public static void main(String[] args) { -// testSimpleLinkedList(); -// testTwoSideLinkedList(); - testIterator(); - } - - private static void testIterator() { - java.util.LinkedList linkedList = new java.util.LinkedList(); - linkedList.addFirst(1); - linkedList.addFirst(2); - linkedList.addFirst(3); - - Collection collection = linkedList; - Iterator iterator = collection.iterator(); - - - for (Integer o : collection) { - System.out.println(o); - } - - while (iterator.hasNext()) { - int element = iterator.next(); - System.out.println(element); - } - } - - private static void testSimpleLinkedList() { - LinkedList linkedList = new SimpleLinkedListImpl(); - linkedList.add(1); - linkedList.add(2); - linkedList.add(3); - linkedList.add(4); - linkedList.add(6); - - System.out.println("Size = " + linkedList.getSize()); - - linkedList.display(); - - linkedList.remove(); - linkedList.remove(3); - - linkedList.display(); - -// for (int value : linkedList) { -// System.out.println(value); -// } - } - private static void testTwoSideLinkedList() { - TwoSideLinkedList linkedList = new TwoSideLinkedListImpl(); - linkedList.addFirst(1); - linkedList.addFirst(2); - linkedList.addFirst(3); - linkedList.addLast(4); - linkedList.addLast(6); - - System.out.println("Size = " + linkedList.getSize()); - - linkedList.display(); - - linkedList.remove(); - linkedList.remove(6); - - linkedList.display(); - } -} \ No newline at end of file From 928c5177f04f0003b0c73e759408f62a60529819 Mon Sep 17 00:00:00 2001 From: ksilerman Date: Mon, 14 Jan 2019 21:00:32 +0300 Subject: [PATCH 5/9] initial --- lesson_4/LinkIterator/main.java | 3 +++ lesson_5/main.java | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 lesson_5/main.java diff --git a/lesson_4/LinkIterator/main.java b/lesson_4/LinkIterator/main.java index bbf6ce2..f52ae75 100644 --- a/lesson_4/LinkIterator/main.java +++ b/lesson_4/LinkIterator/main.java @@ -10,6 +10,9 @@ public static void main(String[] args) { itr.insertBefore("Sergey", 10); list.display(); + + + } } diff --git a/lesson_5/main.java b/lesson_5/main.java new file mode 100644 index 0000000..577cfd7 --- /dev/null +++ b/lesson_5/main.java @@ -0,0 +1,24 @@ +package lesson_5; + +public class main { + public static void main(String[] args) { + + int chislo = 0; + int step = 2; + System.out.println( power2(chislo,step) ); + + + } + + public static int power2(int x, int n) { + if (n == 0) + return 1; + if (n == 1) + return x; + else + return x * (power2(x, n-1)); + + } + + +} From 01f7a472c55bca4c74f69f61116d1b79b2dc4b18 Mon Sep 17 00:00:00 2001 From: ksilerman Date: Mon, 14 Jan 2019 21:26:53 +0300 Subject: [PATCH 6/9] =?UTF-8?q?=D0=94=D0=97=20=D0=B2=D1=8B=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D0=BD=D0=B5=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lesson_5/main.java | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/lesson_5/main.java b/lesson_5/main.java index 577cfd7..fa78389 100644 --- a/lesson_5/main.java +++ b/lesson_5/main.java @@ -2,14 +2,21 @@ public class main { public static void main(String[] args) { - - int chislo = 0; +// 1 задание + int chislo = 3; int step = 2; System.out.println( power2(chislo,step) ); - +//--------------- +// 2 задание + int[] values = new int[] {894, 260, 392, 281, 27}; + int[] weights = new int[] {8, 6, 4, 0, 21}; + int W = 30; + int[] tab = new int[W+1]; + System.out.println(knapsack(values, weights, W, tab, 0)); +//------------------- } - +//--------------------- public static int power2(int x, int n) { if (n == 0) return 1; @@ -19,6 +26,17 @@ public static int power2(int x, int n) { return x * (power2(x, n-1)); } +//------------------------------------------------ +static int knapsack(int[] values, int[] weights, int W, int[] tab, int i) { + if(i>=values.length) return 0; + if(tab[W] != 0) + return tab[W]; + int value1 = knapsack(values, weights, W, tab, i+1); + int value2 = 0; + if(W >= weights[i]) value2 = knapsack(values, weights, W-weights[i], tab, i+1) + values[i]; + return tab[W] = (value1>value2) ? value1 : value2; +} +//--------------------------- } From d0248af532060f765bb2e246377ad5256451e97f Mon Sep 17 00:00:00 2001 From: ksilerman Date: Fri, 18 Jan 2019 19:57:46 +0300 Subject: [PATCH 7/9] =?UTF-8?q?=D0=94=D0=97=20=D0=B2=D1=8B=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D0=BD=D0=B5=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lesson_6/Node.java | 71 ++++++++++++ lesson_6/TraverseMode.java | 7 ++ lesson_6/Tree.java | 18 +++ lesson_6/TreeImpl.java | 228 +++++++++++++++++++++++++++++++++++++ lesson_6/main.java | 102 +++++++++++++++++ 5 files changed, 426 insertions(+) create mode 100644 lesson_6/Node.java create mode 100644 lesson_6/TraverseMode.java create mode 100644 lesson_6/Tree.java create mode 100644 lesson_6/TreeImpl.java create mode 100644 lesson_6/main.java diff --git a/lesson_6/Node.java b/lesson_6/Node.java new file mode 100644 index 0000000..892105d --- /dev/null +++ b/lesson_6/Node.java @@ -0,0 +1,71 @@ +package lesson_6; + +public class Node { + + private final int value; + + private Node leftChild; + private Node rightChild; + + public Node(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public Node getLeftChild() { + return leftChild; + } + + public void setLeftChild(Node leftChild) { + this.leftChild = leftChild; + } + + public Node getRightChild() { + return rightChild; + } + + public void setRightChild(Node rightChild) { + this.rightChild = rightChild; + } + + public boolean isShouldLeft(int value) { + return value < this.value; + } + + public Node getChild(int value) { + return isShouldLeft(value) ? getLeftChild() : getRightChild(); + } + + void addChild(Node child) { + if (isShouldLeft(child.getValue())) { + setLeftChild(child); + } + else { + setRightChild(child); + } + } + + public void display() { + System.out.println(value); + } + + boolean isLeaf() { + return getLeftChild() == null && getRightChild() == null; + } + boolean hasOnlyOneChild() { + return getLeftChild() == null ^ getRightChild() == null; +// return !isLeaf() && (getLeftChild() == null || getRightChild() == null); + } + + void removeChild(Node child) { + if (leftChild == child) { + leftChild = null; + } + else { + rightChild = null; + } + } +} diff --git a/lesson_6/TraverseMode.java b/lesson_6/TraverseMode.java new file mode 100644 index 0000000..fb13759 --- /dev/null +++ b/lesson_6/TraverseMode.java @@ -0,0 +1,7 @@ +package lesson_6; + +public enum TraverseMode { + PRE_ORDER, + POST_ORDER, + IN_ORDER, +} diff --git a/lesson_6/Tree.java b/lesson_6/Tree.java new file mode 100644 index 0000000..7abaa6d --- /dev/null +++ b/lesson_6/Tree.java @@ -0,0 +1,18 @@ +package lesson_6; + +public interface Tree { + + void add(int value); + + boolean remove(int value); + + boolean find(int value); + + public int getSize(); + + boolean isEmpty(); + + void traverse(TraverseMode mode); + + void displayTree(); +} diff --git a/lesson_6/TreeImpl.java b/lesson_6/TreeImpl.java new file mode 100644 index 0000000..2ae0a21 --- /dev/null +++ b/lesson_6/TreeImpl.java @@ -0,0 +1,228 @@ +package lesson_6; + +import java.util.Stack; + +public class TreeImpl implements Tree { + + private Node root; + private int size; + + private static class NodeAndParent { + Node current; + Node parent; + + public static NodeAndParent create(Node parent, Node current) { + NodeAndParent nodeAndParent = new NodeAndParent(); + nodeAndParent.parent = parent; + nodeAndParent.current = current; + return nodeAndParent; + } + } + + + @Override + public void add(int value) { + Node newNode = new Node(value); + + if (isEmpty()) { + root = newNode; + size++; + return; + } + + NodeAndParent nodeAndParent = doFind(value); + nodeAndParent.parent.addChild(newNode); + size++; + } + + @Override + public boolean remove(int value) { + NodeAndParent nodeAndParent = doFind(value); + Node current = nodeAndParent.current; + Node parent = nodeAndParent.parent; + + if (current == null) { + return false; + } + + if (current.isLeaf()) { + if (root == current) { + root = null; + } + else { + parent.removeChild(current); + } + } + else if (current.hasOnlyOneChild()) { + Node child = current.getLeftChild() != null ? current.getLeftChild() : current.getRightChild(); + if (current == root) { + root = child; + } + else if (parent.getLeftChild() == current) { + parent.setLeftChild(child); + } + else { + parent.setRightChild(child); + } + } + else { + Node successor = getSuccessor(current); + Node successorParent = doFind(successor.getValue()).parent; + + if (successor != current.getRightChild()) { + successorParent.setLeftChild(successor.getRightChild()); + successor.setRightChild(current.getRightChild()); + } + + if (current == root) { + root = successor; + } + else if (parent.getLeftChild() == current) { + successor.setLeftChild(current.getLeftChild()); + parent.setLeftChild(successor); + } + else { + successor.setLeftChild(current.getLeftChild()); + parent.setRightChild(successor); + } + } + size--; + + return true; + } + + private Node getSuccessor(Node node) { + Node successorParent = null; + Node successor = null; + Node current = node.getRightChild(); + + while (current != null) { + successorParent = successor; + successor = current; + current = current.getLeftChild(); + } + + return successor; + } + + @Override + public boolean find(int value) { + NodeAndParent nodeAndParent = doFind(value); + return nodeAndParent.current != null; + } + + private NodeAndParent doFind(int value) { + Node current = root; + Node previous = null; + + while (current != null) { + if (current.getValue() == value) { + break; + } + + previous = current; + current = current.getChild(value); + } + + return NodeAndParent.create(previous, current); + } + + @Override + public int getSize() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public void traverse(TraverseMode mode) { + switch (mode) { + case IN_ORDER: + inOrder(root); + break; + case PRE_ORDER: + preOrder(root); + break; + case POST_ORDER: + postOrder(root); + break; + default: + throw new IllegalArgumentException("Unknown mode: " + mode); + } + } + + private void postOrder(Node root) { + if (root != null) { + preOrder(root.getLeftChild()); + preOrder(root.getRightChild()); + root.display(); + } + } + + private void preOrder(Node root) { + if (root != null) { + root.display(); + preOrder(root.getLeftChild()); + preOrder(root.getRightChild()); + } + } + + private void inOrder(Node root) { + if (root != null) { + inOrder(root.getLeftChild()); + root.display(); + inOrder(root.getRightChild()); + } + } + + public void displayTree() { + Stack globalStack = new Stack(); + globalStack.push(root); + int nBlanks = 64; + + boolean isRowEmpty = false; + System.out.println("................................................................"); + + while (!isRowEmpty) { + Stack localStack = new Stack<>(); + + isRowEmpty = true; + for (int i = 0; i < nBlanks; i++) { + System.out.print(" "); + } + + while (!globalStack.isEmpty()) { + Node tempNode = globalStack.pop(); + if (tempNode != null) { + System.out.print(tempNode.getValue()); + localStack.push(tempNode.getLeftChild()); + localStack.push(tempNode.getRightChild()); + if (tempNode.getLeftChild() != null || tempNode.getRightChild() != null) { + isRowEmpty = false; + } + } else { + System.out.print("--"); + localStack.push(null); + localStack.push(null); + } + for (int j = 0; j < nBlanks * 2 - 2; j++) { + System.out.print(" "); + } + } + + System.out.println(); + + while (!localStack.isEmpty()) { + globalStack.push(localStack.pop()); + } + + nBlanks /= 2; + } + System.out.println("................................................................"); + } + + +} diff --git a/lesson_6/main.java b/lesson_6/main.java new file mode 100644 index 0000000..f70f16a --- /dev/null +++ b/lesson_6/main.java @@ -0,0 +1,102 @@ +package lesson_6; + +import java.lang.reflect.Array; +import java.util.List; +import java.util.Objects; +import java.util.Random; +import java.util.Arrays; + +public class main { + + public static void main(String[] args) { + + int[] dia = new int[41]; + dia[0] = -20; + for(int i = 1; i < dia.length; i++){ + dia[i] = dia[i-1]+1; + } + + System.out.print(Arrays.toString(dia)); // выводим массив для наглядности от -20 до 20 +// пошло заполнение деревьев, если попадается уже выпашее значение ловим ошибку и продолжаем + Tree tree1 = new TreeImpl(); + for (int i = 0; i < dia.length; i++) { + try { + tree1.add(getRandom(dia)); + } catch (NullPointerException e) { + tree1.add(getRandom(dia)); + } + } + Tree tree2 = new TreeImpl(); + for (int i = 0; i < dia.length; i++) { + try { + tree2.add(getRandom(dia)); + } catch (NullPointerException e) { + tree2.add(getRandom(dia)); + } + } + Tree tree3 = new TreeImpl(); + for (int i = 0; i < dia.length; i++) { + try { + tree3.add(getRandom(dia)); + } catch (NullPointerException e) { + tree3.add(getRandom(dia)); + } + } + Tree tree4 = new TreeImpl(); + for (int i = 0; i < dia.length; i++) { + try { + tree4.add(getRandom(dia)); + } catch (NullPointerException e) { + tree4.add(getRandom(dia)); + } + } + Tree tree5 = new TreeImpl(); + for (int i = 0; i < dia.length; i++) { + try { + tree5.add(getRandom(dia)); + } catch (NullPointerException e) { + tree5.add(getRandom(dia)); + } + } + Tree tree6 = new TreeImpl(); + + for (int i = 0; i < dia.length; i++) { + try { + tree6.add(getRandom(dia)); + } catch (NullPointerException e) { + tree6.add(getRandom(dia)); + } + } + System.out.println(""); + System.out.println("Дерево 1"); + tree1.displayTree(); + System.out.println("Дерево 2"); + tree2.displayTree(); + System.out.println("Дерево 3"); + tree3.displayTree(); + System.out.println("Дерево 4"); + tree4.displayTree(); + System.out.println("Дерево 5"); + tree5.displayTree(); + System.out.println("Дерево 6"); + tree6.displayTree(); + +// tree.add(5); +// tree.add(2); +// tree.add(7); +// +// System.out.println("Size = " + tree.getSize()); +// System.out.println("Find 5: " + tree.find(5)); +// System.out.println("Find 2: " + tree.find(2)); +// System.out.println("Find 7: " + tree.find(7)); +// System.out.println("Find 777: " + tree.find(777)); +// +// tree.traverse(TraverseMode.IN_ORDER); + + } + + public static int getRandom(int[] array) { + int rnd = new Random().nextInt(array.length); + return array[rnd]; + } +} From 9b78c8c2f557d04bad0694f91727a7b09703cdd6 Mon Sep 17 00:00:00 2001 From: ksilerman Date: Sun, 20 Jan 2019 22:49:02 +0300 Subject: [PATCH 8/9] =?UTF-8?q?=D0=94=D0=97=20=D0=B2=D1=8B=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D0=BD=D0=B5=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lesson_7/Graph.java | 22 ++++++ lesson_7/GraphImpl.java | 161 ++++++++++++++++++++++++++++++++++++++++ lesson_7/Vertex.java | 45 +++++++++++ lesson_7/main.java | 41 ++++++++++ 4 files changed, 269 insertions(+) create mode 100644 lesson_7/Graph.java create mode 100644 lesson_7/GraphImpl.java create mode 100644 lesson_7/Vertex.java create mode 100644 lesson_7/main.java diff --git a/lesson_7/Graph.java b/lesson_7/Graph.java new file mode 100644 index 0000000..045f179 --- /dev/null +++ b/lesson_7/Graph.java @@ -0,0 +1,22 @@ +package lesson_7; + +public interface Graph { + + void addVertex(String label); + + boolean addEdge(String startLabel, String endLabel); + + void display(); + + int getSize(); + + /** + * Depth-first search, DFS //в глубину + */ + void dfs(String startLabel); + + /** + * breadth-first search, BFS //в ширину + */ + void bfs(String startLabel); +} diff --git a/lesson_7/GraphImpl.java b/lesson_7/GraphImpl.java new file mode 100644 index 0000000..6dc0707 --- /dev/null +++ b/lesson_7/GraphImpl.java @@ -0,0 +1,161 @@ +package lesson_7; + +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class GraphImpl implements Graph { + + private boolean[][] adjMat; + private List vertexes; + + + public GraphImpl(int maxVertexes) { + this.adjMat = new boolean[maxVertexes][maxVertexes]; + this.vertexes = new LinkedList<>(); + } + + @Override + public void addVertex(String label) { + Vertex vertex = new Vertex(label); + vertexes.add(vertex); + } + + @Override + public boolean addEdge(String startLabel, String endLabel) { + int startIndex = indexOf(startLabel); + int endIndex = indexOf(endLabel); + + if ( startIndex == -1 || endIndex == -1 ) { + return false; + } + + adjMat[startIndex][endIndex] = true; + adjMat[endIndex][startIndex] = true; + + return true; + } + + private int indexOf(String label) { + for (int i = 0; i < vertexes.size(); i++) { + if (vertexes.get(i).getLabel().equals(label)) { + return i; + } + } + + return -1; + } + + @Override + public void display() { + System.out.println("-------------"); + for (int i = 0; i < vertexes.size(); i++) { + Vertex vertex = vertexes.get(i); + + StringBuilder sb = new StringBuilder(vertex.getLabel()); + + for (int j = 0; j < vertexes.size(); j++) { + if (adjMat[i][j]) { + sb.append(" -> " + vertexes.get(j).getLabel()); + } + } + System.out.println(sb.toString()); + } + System.out.println("-------------"); + } + + @Override + public int getSize() { + return vertexes.size(); + } + + @Override + public void dfs(String startLabel) { + int startIndex = indexOf(startLabel); + if (startIndex == -1) { + throw new IllegalArgumentException("Unknown vertex: " + startLabel); + } + + System.out.println("DFS:"); + System.out.println("-------------"); + + Stack stack = new Stack<>(); + + Vertex vertex = vertexes.get(startIndex); + + visitVertex(vertex); + stack.push(vertex); + + while ( !stack.isEmpty() ) { + vertex = getAdjUnvisitedVertex(stack.peek()); + if (vertex != null) { + visitVertex(vertex); + stack.push(vertex); + } + else { + stack.pop(); + } + } + + System.out.println("-------------"); + + resetVertexState(); + } + + @Override + public void bfs(String startLabel) { + int startIndex = indexOf(startLabel); + if (startIndex == -1) { + throw new IllegalArgumentException("Unknown vertex: " + startLabel); + } + + System.out.println("BFS:"); + System.out.println("-------------"); + + Queue queue = new LinkedList<>(); + + Vertex vertex = vertexes.get(startIndex); + + visitVertex(vertex); + queue.add(vertex); + + while ( !queue.isEmpty() ) { + vertex = getAdjUnvisitedVertex(queue.peek()); + if (vertex != null) { + visitVertex(vertex); + queue.add(vertex); + } + else { + queue.remove(); + } + } + + System.out.println("-------------"); + + resetVertexState(); + } + + private void resetVertexState() { + for (Vertex vertex : vertexes) { + vertex.setWasVisited(false); + } + } + + private Vertex getAdjUnvisitedVertex(Vertex vertex) { + int index = vertexes.indexOf(vertex); + for (int i = 0; i < vertexes.size(); i++) { + if ( adjMat[index][i] && !vertexes.get(i).isWasVisited() ) { + return vertexes.get(i); + } + } + + return null; + } + + private void visitVertex(Vertex vertex) { + System.out.println(vertex); + vertex.setWasVisited(true); + } + +} diff --git a/lesson_7/Vertex.java b/lesson_7/Vertex.java new file mode 100644 index 0000000..edb88ca --- /dev/null +++ b/lesson_7/Vertex.java @@ -0,0 +1,45 @@ +package lesson_7; + +import java.util.Objects; + +public class Vertex { + + private final String label; + private boolean wasVisited; + + public Vertex(String label) { + this.label = label; + } + + public String getLabel() { + return label; + } + + public boolean isWasVisited() { + return wasVisited; + } + + public void setWasVisited(boolean wasVisited) { + this.wasVisited = wasVisited; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Vertex vertex = (Vertex) o; + return Objects.equals(label, vertex.label); + } + + @Override + public int hashCode() { + return Objects.hash(label); + } + + @Override + public String toString() { + return "Vertex{" + + "label='" + label + '\'' + + '}'; + } +} diff --git a/lesson_7/main.java b/lesson_7/main.java new file mode 100644 index 0000000..ca84dd9 --- /dev/null +++ b/lesson_7/main.java @@ -0,0 +1,41 @@ +package lesson_7; + +public class main { + public static void main(String[] args) { + bfs(); + } + + private static void bfs(){ + Graph graph = new GraphImpl(15); + graph.addVertex("Москва"); + graph.addVertex("Тула"); + graph.addVertex("Рязань"); + graph.addVertex("Калуга"); + graph.addVertex("Липецк"); + graph.addVertex("Тамбов"); + graph.addVertex("Орел"); + graph.addVertex("Воронеж"); + graph.addVertex("Саратов"); + graph.addVertex("Курск"); + + graph.addEdge("Москва", "Тула"); + graph.addEdge("Москва", "Рязань"); + graph.addEdge("Москва", "Калуга"); + + graph.addEdge("Тула", "Липецк"); + graph.addEdge("Рязань", "Тамбов"); + graph.addEdge("Калуга", "Орел"); + + graph.addEdge("Липецк", "Воронеж"); + graph.addEdge("Тамбов", "Саратов"); + graph.addEdge("Орел", "Курск"); + + graph.addEdge("Саратов", "Воронеж"); + graph.addEdge("Курск", "Воронеж"); + + System.out.println("Graph size = " + graph.getSize()); + graph.display(); + graph.bfs("Москва"); + + } +} From f0a8b4f3797a488d618519dae224d27cf6242cd0 Mon Sep 17 00:00:00 2001 From: ksilerman Date: Sat, 26 Jan 2019 19:07:22 +0300 Subject: [PATCH 9/9] =?UTF-8?q?=D0=94=D0=97=20=D0=B2=D1=8B=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D0=BD=D0=B5=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lesson_8/DoubleHashTableImpl.java | 19 +++++ lesson_8/HashTable.java | 17 ++++ lesson_8/HashTableImpl.java | 127 ++++++++++++++++++++++++++++++ lesson_8/Item.java | 42 ++++++++++ lesson_8/Main8.java | 33 ++++++++ 5 files changed, 238 insertions(+) create mode 100644 lesson_8/DoubleHashTableImpl.java create mode 100644 lesson_8/HashTable.java create mode 100644 lesson_8/HashTableImpl.java create mode 100644 lesson_8/Item.java create mode 100644 lesson_8/Main8.java diff --git a/lesson_8/DoubleHashTableImpl.java b/lesson_8/DoubleHashTableImpl.java new file mode 100644 index 0000000..5819bf4 --- /dev/null +++ b/lesson_8/DoubleHashTableImpl.java @@ -0,0 +1,19 @@ +package lesson_8; + +public class DoubleHashTableImpl extends HashTableImpl { + + private static int DOUBLE_HASH_CONST = 5; + + public DoubleHashTableImpl(int maxSize) { + super(maxSize); + } + + @Override + protected int getStep(int key) { + return hashFuncDouble(key); + } + + private int hashFuncDouble(int key) { + return DOUBLE_HASH_CONST - (key % DOUBLE_HASH_CONST); + } +} diff --git a/lesson_8/HashTable.java b/lesson_8/HashTable.java new file mode 100644 index 0000000..0e05ade --- /dev/null +++ b/lesson_8/HashTable.java @@ -0,0 +1,17 @@ +package lesson_8; + +public interface HashTable { + + boolean put(Item item, int cost); + + int get(Item item); + + boolean remove(Item item); + + int getSize(); + + boolean isEmpty(); + + void display(); + +} diff --git a/lesson_8/HashTableImpl.java b/lesson_8/HashTableImpl.java new file mode 100644 index 0000000..8551b6f --- /dev/null +++ b/lesson_8/HashTableImpl.java @@ -0,0 +1,127 @@ +package lesson_8; + +public class HashTableImpl implements HashTable { + + private class Entry { + + private Item key; + private int value; + @Override + public String toString() { + return "Entry{" + + "key=" + key + + ", value=" + value + + '}'; + } + + } + + + private static final int DEFAULT_LINE_STEP = 1; + + private static final int INVALID_INDEX = -1; + private static final int INVALID_COST = -1; + + protected Entry[] data; + + protected int maxSize; + protected int size; + + public HashTableImpl(int maxSize) { + this.data = new Entry[maxSize * 2]; + this.maxSize = maxSize; + } + + private int hashFunc(int key) { + return key % data.length; + } + + + @Override + public boolean put(Item item, int cost) { + if (size == maxSize) { + return false; + } + + Entry entry = new Entry(); + entry.key = item; + entry.value = cost; + + int index = hashFunc(item.hashCode()); + + while ( data[index] != null ) { + index += getStep(item.hashCode()); + index %= data.length; + } + + data[index] = entry; + + size++; + return true; + } + + protected int getStep(int key) { + return DEFAULT_LINE_STEP; + } + + @Override + public int get(Item item) { + int index = indexOf(item); + return index != INVALID_INDEX ? data[index].value : INVALID_COST; + + } + + @Override + public boolean remove(Item item) { + int index = indexOf(item); + if (index != INVALID_INDEX) { + data[index] = null; + size--; + return true; + } + + return false; + } + + private int indexOf(Item item) { + int index = hashFunc(item.hashCode()); + + int count = 0; + while (data[index] != null) { + Entry currentEntry = data[index]; + if ( currentEntry.key.equals(item) ) { + return index; + } + + if (count > data.length) { + return INVALID_INDEX; + } + + index += getStep(item.hashCode()); + index %= data.length; + + count++; + } + + return INVALID_INDEX; + } + + @Override + public int getSize() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public void display() { + System.out.println("-----------"); + for (int i = 0; i < data.length; i++) { + System.out.println(String.format("%d = [%s]", i, data[i])); + } + System.out.println("-----------"); + } +} diff --git a/lesson_8/Item.java b/lesson_8/Item.java new file mode 100644 index 0000000..abfa000 --- /dev/null +++ b/lesson_8/Item.java @@ -0,0 +1,42 @@ +package lesson_8; + +public class Item { + + private final String title; + private final int id; + + public Item(int id, String title) { + this.title = title; + this.id = id; + } + + public String getTitle() { + return title; + } + + public int getId() { + return id; + } + + @Override + public String toString() { + return "Item{" + + "title='" + title + '\'' + + ", id=" + id + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Item item = (Item) o; + return id == item.id; + } + + @Override + public int hashCode() { + return id; +// return Objects.hash(id); + } +} diff --git a/lesson_8/Main8.java b/lesson_8/Main8.java new file mode 100644 index 0000000..5173202 --- /dev/null +++ b/lesson_8/Main8.java @@ -0,0 +1,33 @@ +package lesson_8; + +import java.util.HashMap; +import java.util.Map; + +public class Main8 { + + public static void main(String[] args) { +// HashTable hashTable = new HashTableImpl(5); + HashTable hashTable = new DoubleHashTableImpl(5); + hashTable.put(new Item(1, "Orange"), 150); + hashTable.put(new Item(77, "Banana"), 100); + hashTable.put(new Item(62, "Lemon"), 250); + hashTable.put(new Item(21, "Potato"), 67); + hashTable.put(new Item(55, "Milk"), 120); + + System.out.println("Size is " + hashTable.getSize()); + hashTable.display(); + + + Map costByTitle = new HashMap<>(); + costByTitle.put("Banana", 75); + costByTitle.put("Potato", 175); + costByTitle.put("Orange", 275); + + for (String key : costByTitle.keySet()) { + Integer cost = costByTitle.get(key); + System.out.println(key + " = " + cost); + } + + + } +}