Skip to content

Commit

Permalink
feat: Add insert function and print the output
Browse files Browse the repository at this point in the history
  • Loading branch information
VishwajeetVT committed Dec 2, 2024
1 parent 66d002b commit 0915a8d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ public void insertLast(int val) {
node.prev = last;
}

public void insert(int val, int after) {
Node p = find(after);
if (p == null) {
System.out.println("does not exists");
return;
}

Node node = new Node(val);
node.next = p.next;
p.next = node;
node.prev = p;
if (node.next != null) {
node.next.prev = node;
}
}

public Node find(int value) {
Node node = head;
while (node != null) {
if (node.val == value) {
return node;
}
node = node.next;
}
return null;
}

public void display() {
Node node = head;
Node last = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public static void main(String[] args) {
dd.insertFirst(8);
dd.insertFirst(7);
dd.insertLast(11);

dd.insert(10, 8);
dd.display();
}
}

0 comments on commit 0915a8d

Please sign in to comment.