Implement search
This commit is contained in:
@@ -60,18 +60,20 @@ public class LinkedList {
|
||||
}
|
||||
|
||||
public void insert(Object content, Object prev) {
|
||||
toFirst();
|
||||
if (search(prev)) {
|
||||
toFirst();
|
||||
|
||||
while(current.getContent() != prev && hasAccess()) {
|
||||
toNext();
|
||||
}
|
||||
while(current.getContent() != prev && hasAccess()) {
|
||||
toNext();
|
||||
}
|
||||
|
||||
if (hasAccess()) {
|
||||
Node newNode = new Node(content);
|
||||
|
||||
newNode.setNextNode(current.getNextNode());
|
||||
current.setNextNode(newNode);
|
||||
size++;
|
||||
if (hasAccess()) {
|
||||
Node newNode = new Node(content);
|
||||
|
||||
newNode.setNextNode(current.getNextNode());
|
||||
current.setNextNode(newNode);
|
||||
size++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,4 +113,17 @@ public class LinkedList {
|
||||
public Object getCurrentContent() {
|
||||
return current.getContent();
|
||||
}
|
||||
|
||||
public boolean search(Object content) {
|
||||
toFirst();
|
||||
|
||||
while(current.getContent() != content && hasAccess()) {
|
||||
toNext();
|
||||
}
|
||||
|
||||
if(hasAccess()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,9 +20,12 @@ public class Main {
|
||||
linkedList.toFirst();
|
||||
linkedList.toLast();
|
||||
linkedList.toNext();
|
||||
System.out.println("size" + linkedList.getSize());
|
||||
System.out.println("First: " + linkedList.getFirst());
|
||||
System.out.println("Last: " + linkedList.getLast());
|
||||
|
||||
linkedList.remove();
|
||||
|
||||
System.out.println("size" + linkedList.getSize());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user