2nd Ex Finished
This commit is contained in:
9
.idea/ds_exercises.iml
generated
Normal file
9
.idea/ds_exercises.iml
generated
Normal 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
6
sll/.idea/vcs.xml
generated
Normal 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>
|
||||
@@ -4,16 +4,24 @@ package com.tabrizu.ds;
|
||||
* Hello world!
|
||||
*/
|
||||
public final class App {
|
||||
private App() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Says hello to the world.
|
||||
* @param args The arguments of the program.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
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>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
package com.tabrizu.ds;
|
||||
|
||||
public class SingleLinkedList<T> implements ISingleLinkedList<T> {
|
||||
|
||||
private class Node {
|
||||
@@ -46,17 +45,21 @@ public class SingleLinkedList<T> implements ISingleLinkedList<T> {
|
||||
m_tail = m_head;
|
||||
}
|
||||
m_size += 1;
|
||||
System.out.println("First Node added with value: " + value);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLast(T value) {
|
||||
if (m_size == 0) {
|
||||
addFirst(value);
|
||||
return;
|
||||
}
|
||||
Node newNode = new Node(value, null);
|
||||
m_tail.setNext(newNode);
|
||||
m_tail = newNode;
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
if (m_size == 1) {
|
||||
removeFirst();
|
||||
}
|
||||
T lastTail = m_tail.getValue();
|
||||
Node currentNode = m_head;
|
||||
while (currentNode.getNext() != null){
|
||||
|
||||
while (currentNode.getNext().getNext() != null){
|
||||
currentNode = currentNode.getNext();
|
||||
|
||||
}
|
||||
m_tail = currentNode;
|
||||
m_tail.setNext(null);
|
||||
m_size--;
|
||||
return lastTail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getFirst() {
|
||||
// TODO Auto-generated method stub
|
||||
if (m_size == 0){
|
||||
return null;
|
||||
}
|
||||
return m_head.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getLast() {
|
||||
// TODO Auto-generated method stub
|
||||
if (m_size == 0) {
|
||||
return null;
|
||||
}
|
||||
return m_tail.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user