Missing data imputation with KNN
There are various approaches for dealing with missing values. Suppose we've got 4 instances in a dataset:

x1 = [1 2 3]
x2 = [1 ? 3]
x3 = [2 4 2]
x4 = [1 3 3.5]



one simple approach (specially popular in medical datasets) is finding values with regard to most similar instance; in the above case, missing value of x2 would be then: 2 (since x1 is the most similar)
a more sophisticated approach is weighted averaging through k most similar instances (of course only applicable if the missing value is numeric or at least ordinal) in this case, you should calculate: x2(2) = (2*(1-(0/3)) + 3*(1-(0.5/3)) / (2-(0/3)-(0.5/3)) (x4 has also been counted as a similar case
another approach, is voting among k most similar instances (applicable both for categorical and ordinal values).


cases 1 and 3 are what you have implemented (case1 = 1 Nearest Neighbor / case2 = k-nearest neighbors)

there are other approaches for handling a missing value, but it depends on what you're going to do with your dataset. For example in very large datasets, sometimes it is efficient to simply ignore every instance that contains at least one missing value, or ignore only the missing value (not the entire instance) in further processes (e.g. in VFI algorithm)