2nd Ex Finished

This commit is contained in:
2022-10-10 13:02:47 +03:30
parent bf2ab28205
commit 448fefd692
8 changed files with 63 additions and 17 deletions

9
.idea/ds_exercises.iml generated Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
sll/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

@@ -4,16 +4,24 @@ package com.tabrizu.ds;
* Hello world! * Hello world!
*/ */
public final class App { public final class App {
private App() {
}
/**
* Says hello to the world.
* @param args The arguments of the program.
*/
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Hello World!"); System.out.println("Hello World!");
SingleLinkedList<Integer> list = new SingleLinkedList<Integer>();
System.out.println("Step 0: " + list);
list.addLast(55);
list.addLast(76);
list.addLast(35);
list.addFirst(22);
System.out.println("Step 1: " + list);
System.out.println("[Remove]: " + list.removeLast());
System.out.println("Step 2: " + list);
list.addFirst(78);
list.addLast(88);
System.out.println("Step 3: " + list);
System.out.println("[Remove]: " + list.removeFirst());
System.out.println("Step 4: " + list);
System.out.println("First: " + list.getFirst());
System.out.println("Last: " + list.getLast());
SingleLinkedList<Integer> myList = new SingleLinkedList<Integer>();
} }
} }

View File

@@ -1,5 +1,4 @@
package com.tabrizu.ds; package com.tabrizu.ds;
public class SingleLinkedList<T> implements ISingleLinkedList<T> { public class SingleLinkedList<T> implements ISingleLinkedList<T> {
private class Node { private class Node {
@@ -46,17 +45,21 @@ public class SingleLinkedList<T> implements ISingleLinkedList<T> {
m_tail = m_head; m_tail = m_head;
} }
m_size += 1; m_size += 1;
System.out.println("First Node added with value: " + value);
} }
@Override @Override
public void addLast(T value) { public void addLast(T value) {
if (m_size == 0) { if (m_size == 0) {
addFirst(value); addFirst(value);
return;
} }
Node newNode = new Node(value, null); Node newNode = new Node(value, null);
m_tail.setNext(newNode); m_tail.setNext(newNode);
m_tail = newNode; m_tail = newNode;
m_size++; m_size++;
System.out.println("Last Node added with value: " + value);
} }
@@ -77,30 +80,50 @@ public class SingleLinkedList<T> implements ISingleLinkedList<T> {
if (m_size == 0) { if (m_size == 0) {
return null; return null;
} }
if (m_size == 1) {
removeFirst();
}
T lastTail = m_tail.getValue(); T lastTail = m_tail.getValue();
Node currentNode = m_head; Node currentNode = m_head;
while (currentNode.getNext() != null){ while (currentNode.getNext().getNext() != null){
currentNode = currentNode.getNext(); currentNode = currentNode.getNext();
} }
m_tail = currentNode;
m_tail.setNext(null);
m_size--;
return lastTail;
} }
@Override @Override
public T getFirst() { public T getFirst() {
// TODO Auto-generated method stub if (m_size == 0){
return null; return null;
}
return m_head.getValue();
} }
@Override @Override
public T getLast() { public T getLast() {
// TODO Auto-generated method stub if (m_size == 0) {
return null; return null;
}
return m_tail.getValue();
} }
@Override @Override
public String toString() { public String toString() {
if (m_size == 0){
return "[NULL]";
}
Node currentNode = m_head;
StringBuilder output = new StringBuilder();
while (currentNode != null){
output.append(currentNode.getValue());
if (currentNode.getNext() != null)
output.append(" => ");
currentNode = currentNode.getNext();
}
return output.toString();
} }
} }