From cc534d6485864b8d5e7a06f78727a55a31629d6d Mon Sep 17 00:00:00 2001 From: Sobottasgithub Date: Wed, 14 Jan 2026 09:10:22 +0100 Subject: [PATCH] Implement search --- LinkedList.java | 35 +++++++++++++++++++++++++---------- Main.java | 3 +++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/LinkedList.java b/LinkedList.java index 8dff57c..b452ad1 100644 --- a/LinkedList.java +++ b/LinkedList.java @@ -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; + } } diff --git a/Main.java b/Main.java index 7b5e800..205c286 100644 --- a/Main.java +++ b/Main.java @@ -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()); } }