// Sort this list by insertion sorts public void InsertSort() { //this method places the Drug object in a new Node of the linked list //just before the first element greater than the new element under //the comparison method. DrugList sortedList = new DrugList(); Drug tmp = RemoveFirst(); while (tmp != null) { sortedList.InsertInOrder(tmp); tmp = RemoveFirst(); } head = sortedList.head; tail = sortedList.tail; count = sortedList.count; }
// Methods which sort the list: // Sort this list by selection sort. public void SelectSort() { //this methods keeps on calling the removemin method //it always places the new min in the beginning of the list //in order to have a sorted list DrugList sortedList = new DrugList(); Drug tmp = RemoveMin(); while (tmp != null) { Console.WriteLine(count); sortedList.Append(tmp); tmp = RemoveMin(); } head = sortedList.head; tail = sortedList.tail; count = sortedList.count; }
// Add a new Drug item to the end of this linked list. public void Append(Drug data) { //this method places the DRug object in a new Node of the linked list //after its last element //if there is no data, then don't return anything if (data == null) { return; } //Creating a new Node name that would be the equivalent of the data Node newDrug = new Node(data); //if the head is null then the tail should be null just like //the variable we created if (head == null) { head = newDrug; tail = newDrug; } //If head is not null, the "case" (data) after the tail should now //equal to newDrug //then this new DRug becomes the new tail of the list else if (head == tail) { head.Next = newDrug; tail = newDrug; } else if (head != tail) { tail.Next = newDrug; tail = newDrug; } //add one index to the list count++; }
public Node(Drug data) { next = null; this.data = data; }