static void Main( ) { // Initialize a list of 'Drug' objects from a file. LinkedList <Drug> drugList = new LinkedList <Drug>( ); foreach (Drug d in Drug.ArrayFromFile("RXQT1503-10.txt")) { drugList.AddLast(d); } // Display the list of 'Drug' objects in the order they appear on the list. WriteLine( ); WriteLine("drugList as read from the file"); foreach (Drug d in drugList) { WriteLine(d); } WriteLine( ); // Search for a 'Drug' object by the start of the drug name. // Redisplay the list of 'Drug' objects in the order they appear on the list. Write("Enter the start of a drug name to find: "); string start = ReadLine( ) !; Write("Enter the self-organizing move [None, Back, Head]: "); SelfOrgMove selfOrgMove = ( SelfOrgMove )Enum.Parse(typeof(SelfOrgMove), ReadLine( ) !, ignoreCase: true); bool DrugNameMatch(Drug d) { return(d.Name.StartsWith(start, StringComparison.OrdinalIgnoreCase)); } bool found = drugList.Contains(DrugNameMatch, selfOrgMove); WriteLine( ); WriteLine("Drug name starting with \"{0}\" found: {1}", start, found); WriteLine( ); WriteLine("drugList after self-organizing move"); foreach (Drug d in drugList) { WriteLine(d); } WriteLine( ); }
public bool Contains(Predicate <TData> IsTarget, SelfOrgMove move) { Node?currentNode = Head; Node?previousNode = null; Node?backTwo = null; while (currentNode != null) // while valid { if (IsTarget(currentNode.Data)) // when the target is found and it is the current node { if (move == SelfOrgMove.Back) // when user enters back { // previous node is null so it returns the list unchanged if (currentNode == Head) { return(true); } // back2 node is null so your new head is currentNode // back2.next doesnt work else if (previousNode == Head) { previousNode !.Next = currentNode !.Next; //backTwo!.Next = currentNode; Head = currentNode; currentNode !.Next = previousNode; } // swapping the current node and previous node in the middle of the list else { previousNode !.Next = currentNode !.Next; backTwo !.Next = currentNode; // this was the old previous node currentNode !.Next = previousNode; } } // moves the target to the front of the list else if (move == SelfOrgMove.Head) // when user enters head { // nothing to move - current node is already head if (currentNode == Head) { return(true); } // moves it from the middle or end of the list to the beginning // current node is now your head previousNode !.Next = currentNode !.Next; currentNode !.Next = Head; Head = currentNode; } return(true); } // advances through the list backTwo = previousNode; previousNode = currentNode; currentNode = currentNode !.Next; } return(false); }