How it works
Find the first node holding a target value by walking the list from the head.
Implementation
function sllSearchOp(state, target) { const valueArr = state.value; const next = state.next; let current = state.head; while (current !== -1) { visitNode(current); compareKeys(current, target); if (valueArr[current] === target) { reportFound(current); return; } current = next[current]; } reportNotFound(); }
def sllSearchOp(state, target): valueArr = state.value next = state.next current = state.head while (current != -1): visitNode(current) compareKeys(current, target) if (valueArr[current] == target): reportFound(current) return current = next[current] reportNotFound()
#include <vector> #include <algorithm> void sllSearchOp(int state, int target) { auto valueArr = state.value; auto next = state.next; auto current = state.head; while((current != -1)) { visitNode(current); compareKeys(current, target); if((valueArr[current] == target)) { reportFound(current); return; } current = next[current]; } reportNotFound(); }
public void sllSearchOp(int state, int target) { var valueArr = state.value; var next = state.next; var current = state.head; while((current != -1)) { visitNode(current); compareKeys(current, target); if((valueArr[current] == target)) { reportFound(current); return; } current = next[current]; } reportNotFound(); }
#include <stdio.h> void sllSearchOp(int state, int target) { var valueArr = state.value; var next = state.next; var current = state.head; while((current != -1)) { visitNode(current); compareKeys(current, target); if((valueArr[current] == target)) { reportFound(current); return; } current = next[current]; } reportNotFound(); }