-
Notifications
You must be signed in to change notification settings - Fork 0
LinkedList and Merge Sort #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
60964c3
9193f07
87865a9
c86713a
5da5a51
e88ec73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package az.edu.turing.module02.tasks; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| public class MergeSortedArrayApp { | ||
|
|
||
| public static void main(String[] args) { | ||
| int[] array1 = {1, 4, 5, 8, 15, 20}; | ||
| int[] array2 = {2, 3, 6, 7, 13}; | ||
| int[] result = mergeSortedArrays(array1, array2); | ||
| System.out.println("Result Array: " + Arrays.toString(result)); | ||
| } | ||
|
|
||
| public static int[] mergeSortedArrays(int[] a, int[] b) { | ||
| int[] merged = new int[a.length + b.length]; | ||
| int i = 0, j = 0, k = 0; | ||
| while (i < a.length && j < b.length) { | ||
| if (a[i] <= b[j]) { | ||
| merged[k++] = a[i++]; | ||
| } else { | ||
| merged[k++] = b[j++]; | ||
| } | ||
| } | ||
| while (i < a.length) { | ||
| merged[k++] = a[i++]; | ||
| } | ||
| while (j < b.length) { | ||
| merged[k++] = b[j++]; | ||
| } | ||
| return merged; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package az.edu.turing.module02.tasks.linkedListOwn; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| import az.edu.turing.module02.tasks.linkedListOwn.service.impl.LinkedListImpl; | ||
|
|
||
| public class MainApp { | ||
|
|
||
| public static void main(String[] args) { | ||
Turqut7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| LinkedListImpl<Integer> list = new LinkedListImpl<>(); | ||
|
|
||
| list.addHead(1); | ||
| list.addTail(2); | ||
| list.addTail(3); | ||
| list.addTail(4); | ||
|
|
||
| System.out.println("Linked List after adding elements: " + list); | ||
|
|
||
| System.out.println("Array: " + Arrays.toString(list.toArray())); | ||
|
|
||
| list.removeHead(); | ||
| System.out.println("After removing head: " + list); | ||
|
|
||
| list.removeTail(); | ||
| System.out.println("After removing tail: " + list); | ||
|
|
||
| list.insert(1, 5); | ||
| System.out.println("After inserting at index 1: " + list); | ||
|
|
||
| list.update(1, 10); | ||
| System.out.println("After updating index 1: " + list); | ||
|
|
||
| list.delete(1); | ||
| System.out.println("After deleting index 1: " + list); | ||
|
|
||
| list.delete(Integer.valueOf(2)); | ||
| System.out.println("After deleting value 2: " + list); | ||
|
|
||
| list.deleteAll(); | ||
| System.out.println("After deleting all elements: " + list); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package az.edu.turing.module02.tasks.linkedListOwn.model; | ||
|
|
||
| public class Node<T> { | ||
|
|
||
| public T data; | ||
Turqut7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public Node<T> next; | ||
|
|
||
| public Node(T data) { | ||
| this.data = data; | ||
| this.next = null; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package az.edu.turing.module02.tasks.linkedListOwn.service; | ||
|
|
||
| public interface LinkedListInterface<T> { | ||
|
|
||
| T addHead(T item); | ||
|
|
||
| void addTail(T item); | ||
|
|
||
| T removeHead(); | ||
|
|
||
| T removeTail(); | ||
|
|
||
| void insert(int index, T item); | ||
|
|
||
| void update(int index, T item); | ||
|
|
||
| void delete(int index); | ||
|
|
||
| boolean delete(T item); | ||
|
|
||
| void deleteAll(); | ||
|
|
||
| Object[] toArray(); | ||
|
|
||
| String toString(); | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| package az.edu.turing.module02.tasks.linkedListOwn.service.impl; | ||
|
|
||
| import az.edu.turing.module02.tasks.linkedListOwn.model.Node; | ||
| import az.edu.turing.module02.tasks.linkedListOwn.service.LinkedListInterface; | ||
|
|
||
| public class LinkedListImpl<T> implements LinkedListInterface<T> { | ||
|
|
||
| public Node<T> head; | ||
Turqut7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public Node<T> tail; | ||
| private int size; | ||
|
|
||
| @Override | ||
| public T addHead(T item) { | ||
| Node<T> newNode = new Node<>(item); | ||
| if (head == null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use size for checking this condition instead of node |
||
| head = newNode; | ||
| tail = newNode; | ||
| } else { | ||
| newNode.next = head; | ||
| head = newNode; | ||
| } | ||
| size++; | ||
| return item; | ||
| } | ||
|
|
||
| @Override | ||
| public void addTail(T item) { | ||
| Node<T> newNode = new Node<>(item); | ||
| if (tail == null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use size for checking this condition instead of node |
||
| head = newNode; | ||
| tail = newNode; | ||
| } else { | ||
| tail.next = newNode; | ||
| tail = newNode; | ||
| } | ||
| size++; | ||
| } | ||
|
|
||
| @Override | ||
| public T removeHead() { | ||
| if (head == null) { | ||
| return null; | ||
| } | ||
| T data = head.data; | ||
| head = head.next; | ||
| size--; | ||
| if (head == null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use size for checking this condition instead of nodes |
||
| tail = null; | ||
| } | ||
| return data; | ||
| } | ||
|
|
||
| @Override | ||
| public T removeTail() { | ||
| if (tail == null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use size for checking this condition instead of node |
||
| return null; | ||
| } | ||
| if (head == tail) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use size for checking this condition instead of node |
||
| T data = tail.data; | ||
| head = null; | ||
| tail = null; | ||
| size--; | ||
| return data; | ||
| } | ||
| Node<T> current = head; | ||
| while (current.next != tail) { | ||
| current = current.next; | ||
| } | ||
| T data = tail.data; | ||
| tail = current; | ||
| tail.next = null; | ||
| size--; | ||
| return data; | ||
| } | ||
|
|
||
| @Override | ||
| public void insert(int index, T item) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return inserted element,and make arguments final pls |
||
| if (index < 0 || index > size) { | ||
| throw new IndexOutOfBoundsException("Invalid index"); | ||
| } | ||
| if (index == 0) { | ||
| addHead(item); | ||
| } else if (index == size) { | ||
| addTail(item); | ||
| } else { | ||
| Node<T> newNode = new Node<>(item); | ||
| Node<T> current = head; | ||
| for (int i = 0; i < index - 1; i++) { | ||
| current = current.next; | ||
| } | ||
| newNode.next = current.next; | ||
| current.next = newNode; | ||
| size++; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void update(int index, T item) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return updated element,and make arguments final pls |
||
| if (index < 0 || index >= size) { | ||
| throw new IndexOutOfBoundsException("Invalid index"); | ||
| } | ||
| Node<T> current = head; | ||
| for (int i = 0; i < index; i++) { | ||
| current = current.next; | ||
| } | ||
| current.data = item; | ||
| } | ||
|
|
||
| @Override | ||
| public void delete(int index) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return deleted element,and make arguments final pls |
||
| if (index < 0 || index >= size) { | ||
| throw new IndexOutOfBoundsException("Invalid index"); | ||
| } | ||
| if (index == 0) { | ||
| removeHead(); | ||
| } else if (index == size - 1) { | ||
| removeTail(); | ||
| } else { | ||
| Node<T> current = head; | ||
| for (int i = 0; i < index - 1; i++) { | ||
| current = current.next; | ||
| } | ||
| current.next = current.next.next; | ||
| size--; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean delete(T item) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return deleted element,and make arguments final pls |
||
| if (item == null || head == null) { | ||
| return false; | ||
| } | ||
| if (head.data.equals(item)) { | ||
| removeHead(); | ||
| return true; | ||
| } | ||
| Node<T> current = head; | ||
| while (current.next != null && !current.next.data.equals(item)) { | ||
| current = current.next; | ||
| } | ||
| if (current.next != null) { | ||
| current.next = current.next.next; | ||
| size--; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteAll() { | ||
| head = null; | ||
| tail = null; | ||
| size = 0; | ||
| } | ||
|
|
||
| @Override | ||
| public Object[] toArray() { | ||
| Object[] array = new Object[size]; | ||
| Node<T> current = head; | ||
| for (int i = 0; i < size; i++) { | ||
| array[i] = current.data; | ||
| current = current.next; | ||
| } | ||
| return array; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| Node<T> current = head; | ||
| while (current != null) { | ||
| sb.append(current.data).append(" -> "); | ||
| current = current.next; | ||
| } | ||
| return sb.append("null").toString(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.