diff --git a/README.md b/README.md new file mode 100644 index 0000000..670cf77 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# AlgorithmsAndDataStructures +Data structures - LinkedList and Stack on basis an LinkedList; +1. Реализовать класс Stack используя класс MyLinkedList +2. Добавить итератор для класса MyLinkedList \ No newline at end of file diff --git a/src/algorithmsAndDataStructures/Main.java b/src/algorithmsAndDataStructures/Main.java index ba454e2..c5e527b 100644 --- a/src/algorithmsAndDataStructures/Main.java +++ b/src/algorithmsAndDataStructures/Main.java @@ -1,8 +1,52 @@ package algorithmsAndDataStructures; + +import java.util.Iterator; + 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); + 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 new file mode 100644 index 0000000..28c2192 --- /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.next != 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(); + } +}