Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
target/
/target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
/.idea/
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
Expand Down
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) {
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;
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;
public Node<T> tail;
private int size;

@Override
public T addHead(T item) {
Node<T> newNode = new Node<>(item);
if (head == null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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();
}
}