December 2007
Intermediate to advanced
896 pages
19h 57m
English
You need a linked data structure that allows you to easily add and remove elements.
Use the generic LinkedList<T> class. The following method creates a LinkedList<T> class, adds nodes to this linked list object, and then uses several methods to obtain information from nodes within the linked list:
public static void UseLinkedList() { Console.WriteLine("\r\n\r\n"); // Create TodoItem objects to add to the linked list TodoItem i1 = new TodoItem() { Name = "paint door", Comment = "Should be done third" }; TodoItem i2 = new TodoItem() { Name = "buy door", Comment = "Should be done first" }; TodoItem i3 = new TodoItem() { Name = "assemble door", Comment = "Should be done second" }; TodoItem i4 = new TodoItem() { Name = "hang door", Comment = "Should be done last" }; // Create a new LinkedList object LinkedList<TodoItem> todoList = new LinkedList<TodoItem>(); // Add the items todoList.AddFirst(i1); todoList.AddFirst(i2); todoList.AddBefore(todoList.Find(i1), i3); todoList.AddAfter(todoList.Find(i1), i4); // Display all items foreach (TodoItem tdi in todoList) { Console.WriteLine(tdi.Name + " : " + tdi.Comment); } // Display information from the first node in the linked list Console.WriteLine("todoList.First.Value.Name == " + todoList.First.Value.Name); // Display information from the second node in the linked list Console.WriteLine("todoList.First.Next.Value.Name == " + todoList.First.Next.Value.Name); // Display information from the next to last ...Read now
Unlock full access