From 0ef7af53dcb5d87c2b734d59a72d9fcbf332bbc1 Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Thu, 1 Jul 2021 14:04:36 +0300 Subject: [PATCH 1/8] Create MyStack --- src/algorithmsAndDataStructures/MyStack.java | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/algorithmsAndDataStructures/MyStack.java diff --git a/src/algorithmsAndDataStructures/MyStack.java b/src/algorithmsAndDataStructures/MyStack.java new file mode 100644 index 0000000..44c1f65 --- /dev/null +++ b/src/algorithmsAndDataStructures/MyStack.java @@ -0,0 +1,45 @@ +package algorithmsAndDataStructures; + +import java.util.EmptyStackException; +import java.util.Objects; + +public class MyStack { + private int size; + private E[] list; + private final int DEFAULT_CAPACITY = 10; + + public MyStack(int capacity) { + if (capacity <= 0) { + throw new IllegalArgumentException("capacity: " + capacity); + } else list = (E[]) new Objects[capacity]; + } + + public MyStack() { + list = (E[]) new Objects[DEFAULT_CAPACITY]; + } + + public E pick() { + if (isEmpty()) { + throw new EmptyStackException(); + } + return list[size - 1]; + } + public void push(E item){ + if (isFull()) { + + } list[size]=item; + size++; + } + + public boolean isEmpty() { + return size == 0; + } + + public int getSize() { + return size; + } + + public boolean isFull() { + return size == list.length; + } +} From fb719ddcadd7a6dc8a61946a67bcb68011028d48 Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Thu, 1 Jul 2021 15:20:05 +0300 Subject: [PATCH 2/8] Create MyStack --- src/algorithmsAndDataStructures/MyStack.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/algorithmsAndDataStructures/MyStack.java b/src/algorithmsAndDataStructures/MyStack.java index 44c1f65..db63da0 100644 --- a/src/algorithmsAndDataStructures/MyStack.java +++ b/src/algorithmsAndDataStructures/MyStack.java @@ -7,8 +7,10 @@ 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 Objects[capacity]; @@ -16,6 +18,7 @@ public MyStack(int capacity) { public MyStack() { list = (E[]) new Objects[DEFAULT_CAPACITY]; + capacity = DEFAULT_CAPACITY; } public E pick() { @@ -24,10 +27,15 @@ public E pick() { } return list[size - 1]; } - public void push(E item){ + + public void push(E item) { if (isFull()) { + capacity = capacity + DEFAULT_CAPACITY / 2 + 1; + E[]newList = (E[]) new Objects[capacity]; + System.arraycopy(list,0,newList,0,size); - } list[size]=item; + } + list[size] = item; size++; } From 0edff209872b6836cd9c14e2bb8c136592746c06 Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Fri, 2 Jul 2021 11:45:12 +0300 Subject: [PATCH 3/8] Create MyStack --- src/algorithmsAndDataStructures/Main.java | 77 ++++++++++++- src/algorithmsAndDataStructures/MyQueue.java | 110 +++++++++++++++++++ src/algorithmsAndDataStructures/MyStack.java | 27 +++-- 3 files changed, 206 insertions(+), 8 deletions(-) create mode 100644 src/algorithmsAndDataStructures/MyQueue.java diff --git a/src/algorithmsAndDataStructures/Main.java b/src/algorithmsAndDataStructures/Main.java index ba454e2..2334b30 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(); + queue(); + + } + + public static void queue() { + MyQueue queue = new MyQueue<>(); + 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); + +// +// System.out.println(queue); +// +// +// System.out.println(queue.remove()); +// +// System.out.println(queue); +// +// queue.insertRate(5); +// +// System.out.println(queue); +// queue.insertLeft(12); +// System.out.println(queue); + + } + + public static void stack() { + MyStack stack = new MyStack<>(); + stack.push(2); + stack.push(2); + stack.push(2); + stack.push(2); + stack.push(2); + stack.push(2); + stack.push(2); + stack.push(2); + stack.push(2); + stack.push(2); + stack.push(2); + stack.push(2); + stack.push(2); + 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()); + } + } } } diff --git a/src/algorithmsAndDataStructures/MyQueue.java b/src/algorithmsAndDataStructures/MyQueue.java new file mode 100644 index 0000000..3923d20 --- /dev/null +++ b/src/algorithmsAndDataStructures/MyQueue.java @@ -0,0 +1,110 @@ +package algorithmsAndDataStructures; + +import java.util.Arrays; + + +public class MyQueue { + private int size; + private E[] list; + 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); + } else list = (E[]) new Object[capacity]; + } + + public MyQueue() { + 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.copyOf(list, size)) + " b = " + begin + " e = " + end; +// } + @Override + public String toString() { + return Arrays.toString(list) + " b = " + begin + " e = " + end; + } + +} \ No newline at end of file diff --git a/src/algorithmsAndDataStructures/MyStack.java b/src/algorithmsAndDataStructures/MyStack.java index db63da0..234a6ce 100644 --- a/src/algorithmsAndDataStructures/MyStack.java +++ b/src/algorithmsAndDataStructures/MyStack.java @@ -1,7 +1,8 @@ package algorithmsAndDataStructures; +import java.util.Arrays; import java.util.EmptyStackException; -import java.util.Objects; + public class MyStack { private int size; @@ -13,14 +14,21 @@ public MyStack(int capacity) { this.capacity = capacity; if (capacity <= 0) { throw new IllegalArgumentException("capacity: " + capacity); - } else list = (E[]) new Objects[capacity]; + } else list = (E[]) new Object[capacity]; } public MyStack() { - list = (E[]) new Objects[DEFAULT_CAPACITY]; + 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(); @@ -31,9 +39,9 @@ public E pick() { public void push(E item) { if (isFull()) { capacity = capacity + DEFAULT_CAPACITY / 2 + 1; - E[]newList = (E[]) new Objects[capacity]; - System.arraycopy(list,0,newList,0,size); - + E[] newList = (E[]) new Object[capacity]; + System.arraycopy(list, 0, newList, 0, size); + list = newList; } list[size] = item; size++; @@ -43,11 +51,16 @@ public boolean isEmpty() { return size == 0; } - public int getSize() { + public int size() { return size; } public boolean isFull() { return size == list.length; } + + @Override + public String toString() { + return Arrays.toString(Arrays.copyOf(list, size)); + } } From 5f82d8a95cb0d8cac5858e05d4f2948e1f8a0238 Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Fri, 2 Jul 2021 13:10:54 +0300 Subject: [PATCH 4/8] new commit --- src/algorithmsAndDataStructures/Main.java | 24 +++- .../MyDequeue.java | 110 ++++++++++++++++++ .../MyPriorityQueue.java | 77 ++++++++++++ src/algorithmsAndDataStructures/MyQueue.java | 74 +++--------- 4 files changed, 225 insertions(+), 60 deletions(-) create mode 100644 src/algorithmsAndDataStructures/MyDequeue.java create mode 100644 src/algorithmsAndDataStructures/MyPriorityQueue.java diff --git a/src/algorithmsAndDataStructures/Main.java b/src/algorithmsAndDataStructures/Main.java index 2334b30..25d9bc8 100644 --- a/src/algorithmsAndDataStructures/Main.java +++ b/src/algorithmsAndDataStructures/Main.java @@ -6,12 +6,32 @@ public class Main { public static void main(String[] args) { // doTask(); - queue(); +// queue(); + priorityQueue(); + } + + public static void priorityQueue() { + MyPriorityQueue queue = new MyPriorityQueue<>(); + queue.insert(10); + queue.insert(1); + queue.insert(5); + queue.insert(4); + queue.insert(0); + queue.insert(-1); + queue.insert(-1); + queue.insert(-1); + queue.insert(-1); + queue.insert(-1); + queue.insert(-1); + queue.insert(-1); + + System.out.println(queue); + System.out.println(queue.remove()); } public static void queue() { - MyQueue queue = new MyQueue<>(); + MyDequeue queue = new MyDequeue<>(); queue.insertRight(1); queue.insertRight(2); queue.insertRight(3); diff --git a/src/algorithmsAndDataStructures/MyDequeue.java b/src/algorithmsAndDataStructures/MyDequeue.java new file mode 100644 index 0000000..fc53816 --- /dev/null +++ b/src/algorithmsAndDataStructures/MyDequeue.java @@ -0,0 +1,110 @@ +package algorithmsAndDataStructures; + +import java.util.Arrays; + + +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.copyOf(list, size)) + " b = " + begin + " e = " + end; +// } + @Override + public String toString() { + return Arrays.toString(list) + " b = " + begin + " e = " + end; + } + +} \ No newline at end of file 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 index 3923d20..72f7977 100644 --- a/src/algorithmsAndDataStructures/MyQueue.java +++ b/src/algorithmsAndDataStructures/MyQueue.java @@ -2,35 +2,26 @@ import java.util.Arrays; +public class MyQueue { -public class MyQueue { + private T[] list; private int size; - private E[] list; 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); - } else list = (E[]) new Object[capacity]; + } + list = (T[]) new Object[capacity]; } public MyQueue() { - list = (E[]) new Object[DEFAULT_CAPACITY]; - capacity = DEFAULT_CAPACITY; + list = (T[]) new Object[DEFAULT_CAPACITY]; } - public E peekLeft() { - if (isEmpty()) { - throw new RuntimeException("Queue is empty"); - } - return list[begin]; - } - - public void insertRight(E item) { + public void insert(T item) { if (isFull()) { throw new RuntimeException("queue is full"); } @@ -39,52 +30,24 @@ public void insertRight(E 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(); + public T remove() { + T temp = peek(); 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 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; @@ -98,13 +61,8 @@ public int size() { return size; } - // @Override -// public String toString() { -// return Arrays.toString(Arrays.copyOf(list, size)) + " b = " + begin + " e = " + end; -// } @Override public String toString() { return Arrays.toString(list) + " b = " + begin + " e = " + end; } - } \ No newline at end of file From 7345ad5c1c5cc4ac8d49721622a1d0827efc1c8a Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Sat, 3 Jul 2021 13:36:30 +0300 Subject: [PATCH 5/8] new commit --- src/algorithmsAndDataStructures/Main.java | 67 ++++++------------- .../MyDequeue.java | 23 +++---- src/algorithmsAndDataStructures/MyQueue.java | 10 ++- 3 files changed, 39 insertions(+), 61 deletions(-) diff --git a/src/algorithmsAndDataStructures/Main.java b/src/algorithmsAndDataStructures/Main.java index 25d9bc8..53cf6e7 100644 --- a/src/algorithmsAndDataStructures/Main.java +++ b/src/algorithmsAndDataStructures/Main.java @@ -6,31 +6,32 @@ public class Main { public static void main(String[] args) { // doTask(); -// queue(); - priorityQueue(); +// 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); + } public static void priorityQueue() { MyPriorityQueue queue = new MyPriorityQueue<>(); - queue.insert(10); - queue.insert(1); - queue.insert(5); - queue.insert(4); - queue.insert(0); - queue.insert(-1); - queue.insert(-1); - queue.insert(-1); - queue.insert(-1); - queue.insert(-1); - queue.insert(-1); - queue.insert(-1); - + for (int i = 1; i < 17; i++) { + queue.insert(i); + } System.out.println(queue); System.out.println(queue.remove()); } - public static void queue() { + public static void dequeue() { MyDequeue queue = new MyDequeue<>(); queue.insertRight(1); queue.insertRight(2); @@ -51,38 +52,14 @@ public static void queue() { System.out.println(queue); System.out.println(queue.removeRight()); System.out.println(queue); - -// -// System.out.println(queue); -// -// -// System.out.println(queue.remove()); -// -// System.out.println(queue); -// -// queue.insertRate(5); -// -// System.out.println(queue); -// queue.insertLeft(12); -// System.out.println(queue); - } public static void stack() { MyStack stack = new MyStack<>(); - stack.push(2); - stack.push(2); - stack.push(2); - stack.push(2); - stack.push(2); - stack.push(2); - stack.push(2); - stack.push(2); - stack.push(2); - stack.push(2); - stack.push(2); - stack.push(2); - stack.push(2); + for (int i = 1; i < 17; i++) { + stack.push(i); + } + System.out.println(stack.pop()); System.out.println(stack); } @@ -100,4 +77,4 @@ public static void doTask() { } } } -} +} \ No newline at end of file diff --git a/src/algorithmsAndDataStructures/MyDequeue.java b/src/algorithmsAndDataStructures/MyDequeue.java index fc53816..027da54 100644 --- a/src/algorithmsAndDataStructures/MyDequeue.java +++ b/src/algorithmsAndDataStructures/MyDequeue.java @@ -43,30 +43,28 @@ 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 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); - + begin = prevIndex(begin); } public E peekRight() { if (isEmpty()) { throw new RuntimeException("Queue is empty"); } - return list[end-1]; + return list[end - 1]; } public E removeLeft() { @@ -80,12 +78,11 @@ public E removeLeft() { public E removeRight() { E temp = peekRight(); size--; - list[end-1] = null; + list[end - 1] = null; end = nextIndex(begin); return temp; } - public boolean isFull() { return size == list.length; } @@ -98,10 +95,6 @@ public int size() { return size; } - // @Override -// public String toString() { -// return Arrays.toString(Arrays.copyOf(list, size)) + " b = " + begin + " e = " + end; -// } @Override public String toString() { return Arrays.toString(list) + " b = " + begin + " e = " + end; diff --git a/src/algorithmsAndDataStructures/MyQueue.java b/src/algorithmsAndDataStructures/MyQueue.java index 72f7977..64020ca 100644 --- a/src/algorithmsAndDataStructures/MyQueue.java +++ b/src/algorithmsAndDataStructures/MyQueue.java @@ -1,5 +1,6 @@ package algorithmsAndDataStructures; +import java.lang.reflect.Array; import java.util.Arrays; public class MyQueue { @@ -9,8 +10,10 @@ public class MyQueue { 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); } @@ -19,11 +22,16 @@ public MyQueue(int capacity) { public MyQueue() { list = (T[]) new Object[DEFAULT_CAPACITY]; + capacity = DEFAULT_CAPACITY; } public void insert(T item) { if (isFull()) { - throw new RuntimeException("queue is full"); + 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; From 9a8762489cef2fc4ef953a624489f260bfb4c1d4 Mon Sep 17 00:00:00 2001 From: SerjNikitin <76908941+SerjNikitin@users.noreply.github.com> Date: Sat, 3 Jul 2021 13:41:56 +0300 Subject: [PATCH 6/8] Create README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..7de488c --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# AlgorithmsAndDataStructures +Created next structures, based on array: Stack, Queue, PriorityQueue, Dequeue; From 3898a967639c4bc3bdaa529e6baa5e5c5dd806da Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Mon, 5 Jul 2021 11:21:40 +0300 Subject: [PATCH 7/8] new commit --- src/algorithmsAndDataStructures/Main.java | 5 ++++- src/algorithmsAndDataStructures/MyDequeue.java | 12 ++++++++++-- src/algorithmsAndDataStructures/MyQueue.java | 15 ++++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/algorithmsAndDataStructures/Main.java b/src/algorithmsAndDataStructures/Main.java index 53cf6e7..375c5ff 100644 --- a/src/algorithmsAndDataStructures/Main.java +++ b/src/algorithmsAndDataStructures/Main.java @@ -9,7 +9,7 @@ public static void main(String[] args) { // dequeue(); // priorityQueue(); // stack(); - queue(); +// queue(); } public static void queue() { @@ -18,6 +18,9 @@ public static void queue() { queue.insert(i); } System.out.println(queue); + queue.remove(); + System.out.println(queue); + System.out.println(queue.size()); } diff --git a/src/algorithmsAndDataStructures/MyDequeue.java b/src/algorithmsAndDataStructures/MyDequeue.java index 027da54..3756671 100644 --- a/src/algorithmsAndDataStructures/MyDequeue.java +++ b/src/algorithmsAndDataStructures/MyDequeue.java @@ -1,6 +1,7 @@ package algorithmsAndDataStructures; import java.util.Arrays; +import java.util.Objects; public class MyDequeue { @@ -97,7 +98,14 @@ public int size() { @Override public String toString() { - return Arrays.toString(list) + " b = " + begin + " e = " + end; + 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();} -} \ No newline at end of file diff --git a/src/algorithmsAndDataStructures/MyQueue.java b/src/algorithmsAndDataStructures/MyQueue.java index 64020ca..53ef0b2 100644 --- a/src/algorithmsAndDataStructures/MyQueue.java +++ b/src/algorithmsAndDataStructures/MyQueue.java @@ -2,6 +2,7 @@ import java.lang.reflect.Array; import java.util.Arrays; +import java.util.Objects; public class MyQueue { @@ -71,6 +72,18 @@ public int size() { @Override public String toString() { - return Arrays.toString(list) + " b = " + begin + " e = " + end; + 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 From e3e94064254e88a502349aa7bde4e063053fbc82 Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Wed, 21 Jul 2021 17:19:45 +0300 Subject: [PATCH 8/8] Rename branch --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 7de488c..e1f112b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # AlgorithmsAndDataStructures Created next structures, based on array: Stack, Queue, PriorityQueue, Dequeue; +1. Создать программу, которая переворачивает вводимые строки (читает справа налево). +2. Создать класс для реализации дека. +3. Реализовать расширение массива в стеке при заполнении стека. \ No newline at end of file