[英]Search in Doubly Linked List c#
I'm writing an application which is using a doubly linked list. 我正在编写一个使用双向链表的应用程序。 I need to implement search in this list.
我需要在此列表中实施搜索。 There is a list of employees with the name, id, department.
这里有一个雇员的名字,名字,部门。 What I need to do is to find the specified employee by her/his last name.
我需要做的是根据其姓氏查找指定的员工。 Any ideas, how it's possible to implement?
有什么想法,如何实现?
Please do not reinvent the wheel. 请不要重新发明轮子。 All classes for this are already in .Net - just initialize
LinkedList
and use Enumerable.FirstOrDefault
to search: .Net的所有此类均已在-Net中-只需初始化
LinkedList
并使用Enumerable.FirstOrDefault
搜索:
LinkedList<Employee> employees = ....
var firstWithIdSeven = employees.FirstOrDefault(e => e.Id == 7); // assuming id is int
Note that if you need frequent search by some subset of parameters using Dictionary
with custom comparer would be more efficient. 请注意,如果您需要使用带有自定义比较器的
Dictionary
来按参数的某些子集进行频繁搜索,效率会更高。
If this is for actual C# code, you should be using the collections that come with the system (such as LinkedList
). 如果这是用于实际的C#代码,则应使用系统随附的集合(例如
LinkedList
)。
For educational purposes, the algorithm is as follows: 出于教育目的,该算法如下:
def find(lastName):
set curr to head
while curr <> null:
if curr.lastName == lastName:
return curr
curr = curr.next
return null
Translating that into C# (or any other procedural language) should be relatively easy. 将其转换为C#(或任何其他过程语言)应该相对容易。
If you are looking for exotic implemetations - check out High Level Function Confusion with Lambda/LINQ expressions in C# that discusses iteration with recursive lambda expression. 如果您正在寻找奇异的实现,请在C#中查看有关Lambda / LINQ表达式的高级函数混淆,其中讨论了递归Lambda表达式的迭代。
So let's say your node is 假设您的节点是
class Employee
{
// code to initialize omitted
public Employee Next { get;}
public int Id { get;}
}
Now we can use following recursive lambda (converting to anonymous recursive lambda is exercise for readers) 现在我们可以使用以下递归lambda(转换为匿名递归lambda是读者的练习)
Func<
Func<Employee, bool>, // condition to continue iteration
Func<Employee, Employee>, // "next" function
Func<Employee, Employee> // resulting iterator method
> Loop = null;
Loop = (c , f ) => n => c(n) ? Loop(c , f ) ( f (n)): n;
Func<Employee, Employee> findIdSevenStartingFrom = Loop(
n => !(n == null || n.Id == 7), // condition to continue search
n => n.Next ); // iterator function
Now with findIdSevenStartingFrom
we can pass any element of the list and search to the end to find it by Id. 现在,通过
findIdSevenStartingFrom
我们可以传递列表的任何元素并搜索到末尾以通过ID进行查找。
var listHead = new Employee{ Id = 6, Next = new Employee { Id = 7 } };
var item = findIdSevenStartingFrom(listHead);
I think you are looking for something like this: 我认为您正在寻找这样的东西:
Employee: 雇员:
public class Employee
{
public string FirstName {get; set;}
public string LastName {get; set;}
}
Double linked node: 双链接节点:
public class DoubleLinkedList
{
public Employee Employee {get; set;}
public DoubleLinkedList Next {get; set;}
public DoubleLinkedList Prev {get; set;}
}
Function to find employee by name in the list: 在列表中按姓名查找员工的功能:
public Employee FindByName(DoubleLinkedList startNode, string name)
{
var currentNode = startNode;
while(currentNode != null)
{
if(currentNode.Employee.LastName == name)
{
return currentNode.Employee; // Found
}
currentNode = currentNode.Next;
}
return null; // Not found
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.