// Make a list of random items. private void makeItemsButton_Click(object sender, EventArgs e) { int numItems = int.Parse(numItemsTextBox.Text); int minimum = int.Parse(minimumTextBox.Text); int maximum = int.Parse(maximumTextBox.Text); Random rand = new Random(); // Delete the previous list if there is one. //(This isn't necessary in C#.) // Add cells. ValueCell newTop = null; for (int i = 0; i < numItems; i++) { ValueCell newCell = new ValueCell(); newCell.Value = rand.Next(minimum, maximum); newCell.Next = newTop; newTop = newCell; } // Make a new sentinel. TopCell = new ValueCell() { Value = 0, Next = newTop }; // Make a copy. CopyTopCell = TopCell.CopyList(); // Display the list. DisplayList(); }
// Use insertionsort to sort the list after this cell. // (This assumes we are the sentinel.) public void Insertionsort() { // Make a sentinel for the new list. ValueCell newTop = new ValueCell() { Next = null }; // Repeat until the list is empty. while (this.Next != null) { // Remove the next cell from the original list. ValueCell cell = this.Next; int cellValue = cell.Value; this.Next = cell.Next; // Insert it in the new list. for (ValueCell before = newTop; ; before = before.Next) { // If before.Next is null or the next // cell's value >= this one, insert the item here. if ((before.Next == null) || (before.Next.Value >= cellValue)) { // Insert the new value after before. cell.Next = before.Next; before.Next = cell; break; } } } // Make this cell be the sentinel for the new list. this.Next = newTop.Next; }
// Make a copy of the list (non-recursively). public ValueCell CopyList() { // Make a new top for the list. ValueCell newTop = new ValueCell(); newTop.Value = this.Value; // Make a cell to hold the last cell in the new list. ValueCell lastCell = newTop; // Make the other cells. for (ValueCell cell = this.Next; cell != null; cell = cell.Next) { // Make the new cell. ValueCell newCell = new ValueCell(); newCell.Value = cell.Value; // Add it to the list. lastCell.Next = newCell; lastCell = newCell; } // End the list. lastCell.Next = null; // Return the copy. return(newTop); }
// Display the items. private void DisplayList() { valueListBox.Items.Clear(); int i = 0; for (ValueCell cell = TopCell.Next; cell != null; cell = cell.Next) { valueListBox.Items.Add(cell.Value); // Only list up to 1000 items. if (++i > 1000) { break; } } }
// Verify that the list below this item is sorted (non-recursively). public bool IsSorted() { // An empty list is sorted. if (this.Next == null) { return(true); } for (ValueCell cell = this.Next; cell.Next != null; cell = cell.Next) { if (cell.Value > cell.Next.Value) { return(false); } } // If we get this far, the list is sorted. return(true); }
// Use selectionsort to sort the list after this cell. // (This assumes we are the sentinel.) public void Selectionsort() { // Make a new top cell reference. // Note that the new list doesn't have a sentinel. ValueCell newTop = null; // Repeat until the list is empty. while (this.Next != null) { // Find the next biggest value in the list. ValueCell bestBefore = this; int bestValue = bestBefore.Next.Value; // Search the other cells to find a bigger value. for (ValueCell cell = bestBefore.Next; cell.Next != null; cell = cell.Next) { // See if the next cell's value is bigger than // the biggest value we've found so far. if (cell.Next.Value > bestValue) { // Replace the biggest value found. bestValue = cell.Next.Value; bestBefore = cell; } } // Remove the best cell from the old list. ValueCell bestCell = bestBefore.Next; bestBefore.Next = bestCell.Next; // Add the cell to the top of the new list. bestCell.Next = newTop; newTop = bestCell; } // Make this cell be the sentinel for the new list. this.Next = newTop; }
// Reset the list to the original insorted values. private void resetButton_Click(object sender, EventArgs e) { TopCell = CopyTopCell.CopyList(); DisplayList(); }