Implement search

This commit is contained in:
Sobottasgithub
2026-01-14 09:10:22 +01:00
parent 8a0c6785c5
commit cc534d6485
2 changed files with 28 additions and 10 deletions

View File

@@ -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;
}
}

View File

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