From 2b084ec150d3a767992bf412dcb603b1fda19fe4 Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Mon, 5 Jul 2021 20:27:55 +0300 Subject: [PATCH 1/4] Created LinkedList and created Stack on basis an LinkedList; --- src/algorithmsAndDataStructures/Main.java | 38 ++- .../MyLinkedList.java | 227 ++++++++++++++++++ .../MyLinkedListOneLink.java | 133 ++++++++++ .../MyLinkedQueue.java | 37 +++ .../MyLinkedStack.java | 33 +++ 5 files changed, 467 insertions(+), 1 deletion(-) create mode 100644 src/algorithmsAndDataStructures/MyLinkedList.java create mode 100644 src/algorithmsAndDataStructures/MyLinkedListOneLink.java create mode 100644 src/algorithmsAndDataStructures/MyLinkedQueue.java create mode 100644 src/algorithmsAndDataStructures/MyLinkedStack.java diff --git a/src/algorithmsAndDataStructures/Main.java b/src/algorithmsAndDataStructures/Main.java index ba454e2..5bce250 100644 --- a/src/algorithmsAndDataStructures/Main.java +++ b/src/algorithmsAndDataStructures/Main.java @@ -1,8 +1,44 @@ package algorithmsAndDataStructures; + public class Main { public static void main(String[] args) { - // write your code here +// stack(); +// queue(); + list(); + + } + + public static void stack() { + MyLinkedStack stack = new MyLinkedStack<>(); + stack.push(2); + stack.push(3); + stack.push(4); + stack.push(5); + System.out.println(stack); + System.out.println(stack.pick()); + } + + public static void queue() { + MyLinkedQueue queue = new MyLinkedQueue<>(); + queue.insert(11); + queue.insert(22); + queue.insert(33); + queue.insert(44); + System.out.println(queue.toString()); + System.out.println(queue.contains(22)); + System.out.println(queue.getLast()); + } + + public static void list() { + MyLinkedList list = new MyLinkedList<>(); + list.insertFirst(1); + list.insertFirst(2); + list.insertFirst(3); + list.insertFirst(4); + list.insertFirst(5); + list.insertFirst(6); + System.out.println(list); } } diff --git a/src/algorithmsAndDataStructures/MyLinkedList.java b/src/algorithmsAndDataStructures/MyLinkedList.java new file mode 100644 index 0000000..1766192 --- /dev/null +++ b/src/algorithmsAndDataStructures/MyLinkedList.java @@ -0,0 +1,227 @@ +package algorithmsAndDataStructures; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class MyLinkedList implements Iterable { + + private Node first; + private int size; + private Node last; + + public void insertFirst(E item) { + Node newNode = new Node(first, item); + if (isEmpty()) { + last = newNode; + } else { + first.setPrev(newNode); + } + first = newNode; + size++; + } + + public void insertLast(E item) { + Node newNode = new Node(last, item, null); + if (isEmpty()) { + first = newNode; + } else last.setNext(newNode); + last = newNode; + size++; + } + + public void insert(E item, int index) { + if (index < 0 || index > size) { + throw new IllegalArgumentException("index: " + index); + } + if (index == 0) { + insertFirst(item); + return; + } + + if (index == size) { + insertLast(item); + return; + } + Node current = first; + for (int i = 0; i < index - 1; i++) { + current = current.getNext(); + } + Node newNode = new Node(current, item, current.getNext()); + current.setNext(newNode); + newNode.getNext().setPrev(newNode); + size++; + } + + public boolean remove(E item) { + if (isEmpty()) { + throw new NoSuchElementException("List is Empty"); + } + if (item.equals(first.value)) { + removeFirst(); + return true; + } + Node current = first; + while (current != null && !current.value.equals(item)) { + current = current.getNext(); + } + if (current == null) { + return false; + } + if (current == last) { + removeLast(); + return true; + } + current.getNext().setPrev(current.getPrev()); + current.getPrev().setNext(current.getNext()); + size--; + return true; + + } + + public E removeFirst() { + if (isEmpty()) { + throw new NoSuchElementException("List is Empty"); + } + E temp = first.getValue(); + first = first.getNext(); + if (isEmpty()) { + last = null; + } else { + first.setPrev(null); + } + size--; + return temp; + } + + public E removeLast() { + if (isEmpty()) { + throw new NoSuchElementException("List is Empty"); + } + E temp = last.getValue(); + if (last.getPrev() == null) { + first = null; + } else { + last.getPrev().setNext(null); + } + last = last.getPrev(); + size--; + return temp; + } + + public boolean isEmpty() { + return first == null; + } + + public E getFirst() { + if (isEmpty()) { + throw new NoSuchElementException("List is Empty"); + } + return first.getValue(); + } + + public E getLast() { + if (isEmpty()) { + throw new NoSuchElementException("List is Empty"); + } + return last.getValue(); + } + + public int size() { + return size; + } + + public boolean contains(E item) { + return index(item) > -1; + } + + public int indexOf(E item) { + return index(item); + } + + private int index(E item) { + Node current = first; + int index = 0; + while (current != null) { + if (current.value.equals(item)) { + return index; + } + current = current.getNext(); + index++; + } + return -1; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + Node current = first; + while (current != null) { + sb.append(current.value).append(" "); + current = current.getNext(); + } + return sb.toString(); + } + + @Override + public Iterator iterator() { + return new Iterate(); + } + + private class Iterate implements Iterator { + + private Node current = first; + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public E next() { + E e = current.value; + current = current.getNext(); + return e; + } + } + + private class Node { + Node prev; + E value; + Node next; + + public Node(Node prev, E value, Node next) { + this.prev = prev; + this.value = value; + this.next = next; + } + + public Node(Node next, E value) { + this.next = next; + this.value = value; + } + + public Node getPrev() { + return prev; + } + + public void setPrev(Node prev) { + this.prev = prev; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + + public E getValue() { + return value; + } + + public void setValue(E value) { + this.value = value; + } + } +} \ No newline at end of file diff --git a/src/algorithmsAndDataStructures/MyLinkedListOneLink.java b/src/algorithmsAndDataStructures/MyLinkedListOneLink.java new file mode 100644 index 0000000..b9e06f6 --- /dev/null +++ b/src/algorithmsAndDataStructures/MyLinkedListOneLink.java @@ -0,0 +1,133 @@ +package algorithmsAndDataStructures; + +import java.util.NoSuchElementException; + +public class MyLinkedListOneLink { + + private Node first; + private int size; + + public void insertFirst(E item) { + Node node = new Node(first, item); + first = node; + size++; + } + + public void insert(E item, int index) { + if (index < 0 || index > size) { + throw new IllegalArgumentException("index: " + index); + } + if (index == 0) { + insertFirst(item); + return; + } + Node current = first; + for (int i = 0; i < index - 1; i++) { + current = current.getNext(); + } + Node node = new Node(current.getNext(), item); + current.setNext(node); + size++; + } + + public boolean remove(E item) { + if (isEmpty()) { + throw new NoSuchElementException("List is Empty"); + } + if (item.equals(first.value)) { + removeFirst(); + return true; + } + Node current = first; + while (current.getNext() != null && !current.getNext().value.equals(item)) { + current = current.getNext(); + } + if (current.getNext() == null) { + return false; + } + current.setNext(current.getNext().getNext()); + size--; + return true; + + } + + public E removeFirst() { + if (isEmpty()) { + throw new NoSuchElementException("List is Empty"); + } + E temp = first.getValue(); + first = first.getNext(); + size--; + return temp; + } + + public boolean isEmpty() { + return first == null; + } + + public Node getFirst() { + return first; + } + + public int size() { + return size; + } + + public boolean contains(E item) { + return index(item) > -1; + } + + public int indexOf(E item) { + return index(item); + } + + private int index(E item) { + Node current = first; + int index = 0; + while (current != null) { + if (current.value.equals(item)) { + return index; + } + current = current.getNext(); + index++; + } + return -1; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + Node current = first; + while (current != null) { + sb.append(current.value).append(" "); + current = current.getNext(); + } + return sb.toString(); + } + + private class Node { + Node next; + E value; + + public Node(Node next, E value) { + this.next = next; + this.value = value; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + + public E getValue() { + return value; + } + + public void setValue(E value) { + this.value = value; + } + } +} diff --git a/src/algorithmsAndDataStructures/MyLinkedQueue.java b/src/algorithmsAndDataStructures/MyLinkedQueue.java new file mode 100644 index 0000000..7126557 --- /dev/null +++ b/src/algorithmsAndDataStructures/MyLinkedQueue.java @@ -0,0 +1,37 @@ +package algorithmsAndDataStructures; + +public class MyLinkedQueue { + private MyLinkedList list = new MyLinkedList<>(); + + public void insert(E item) { + list.insertFirst(item); + } + + public E remove() { + return list.removeLast(); + } + + public boolean isEmpty() { + return list.isEmpty(); + } + + public E getFirst() { + return list.getFirst(); + } + + public E getLast() { + return list.getLast(); + } + + public int size() { + return list.size(); + } + + public boolean contains(E item) { + return list.contains(item); + } + + public String toString() { + return list.toString(); + } +} diff --git a/src/algorithmsAndDataStructures/MyLinkedStack.java b/src/algorithmsAndDataStructures/MyLinkedStack.java new file mode 100644 index 0000000..634d412 --- /dev/null +++ b/src/algorithmsAndDataStructures/MyLinkedStack.java @@ -0,0 +1,33 @@ +package algorithmsAndDataStructures; + +public class MyLinkedStack { + private MyLinkedList list = new MyLinkedList<>(); + + public void push(E item) { + list.insertFirst(item); + } + + public E pop() { + return list.removeFirst(); + } + + public boolean isEmpty() { + return list.isEmpty(); + } + + public E pick() { + return list.getFirst(); + } + + public int size() { + return list.size(); + } + + public boolean contains(E item) { + return list.contains(item); + } + + public String toString() { + return list.toString(); + } +} From d480dae2b94439c0e87bd1f63c74d89cc2b87aef Mon Sep 17 00:00:00 2001 From: SerjNikitin <76908941+SerjNikitin@users.noreply.github.com> Date: Mon, 5 Jul 2021 20:29:45 +0300 Subject: [PATCH 2/4] 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..78d9c7b --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# AlgorithmsAndDataStructures +Data structures - LinkedList and Stack on basis an LinkedList; From 5d4eca01f8423ff2b57dfb63af6b46ec7b1627ae Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Tue, 6 Jul 2021 10:15:45 +0300 Subject: [PATCH 3/4] Created LinkedList and created Stack on basis an LinkedList; --- src/algorithmsAndDataStructures/Main.java | 8 ++++++++ src/algorithmsAndDataStructures/MyLinkedList.java | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/algorithmsAndDataStructures/Main.java b/src/algorithmsAndDataStructures/Main.java index 5bce250..c5e527b 100644 --- a/src/algorithmsAndDataStructures/Main.java +++ b/src/algorithmsAndDataStructures/Main.java @@ -1,6 +1,8 @@ package algorithmsAndDataStructures; +import java.util.Iterator; + public class Main { public static void main(String[] args) { @@ -39,6 +41,12 @@ public static void list() { list.insertFirst(4); list.insertFirst(5); list.insertFirst(6); + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + + System.out.println(list); } } diff --git a/src/algorithmsAndDataStructures/MyLinkedList.java b/src/algorithmsAndDataStructures/MyLinkedList.java index 1766192..28c2192 100644 --- a/src/algorithmsAndDataStructures/MyLinkedList.java +++ b/src/algorithmsAndDataStructures/MyLinkedList.java @@ -173,7 +173,7 @@ private class Iterate implements Iterator { @Override public boolean hasNext() { - return current != null; + return current.next != null; } @Override From 545ec6014c3cb557c7d35c9ee39f28cee37d01cc Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Wed, 21 Jul 2021 17:24:05 +0300 Subject: [PATCH 4/4] Rename branch --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 78d9c7b..670cf77 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # AlgorithmsAndDataStructures Data structures - LinkedList and Stack on basis an LinkedList; +1. Реализовать класс Stack используя класс MyLinkedList +2. Добавить итератор для класса MyLinkedList \ No newline at end of file