public MyLinkedListItem <T> Modify(MyLinkedListItem <T> newItem) { MyLinkedListItem <T> temp = _next; _next = newItem; return(_next); }
/// <summary> /// FindItemDescending - Finds the the nth number item from the start of the list /// /// </summary> /// <param name="nth"> This parameter is assumed to be index 1 instead of index 0 based.</param> /// <returns></returns> private MyLinkedListItem <T> FindItemAscending(ulong nth) { if (_start == null) { return(null); } //No need to search the list if item is index 1 since that is the start item. if (nth == 1) { return(_start); } MyLinkedListItem <T> temp = _start; ulong index = 0; while (temp != null) { //Once item index found return that item and terminate search. if (++index == nth) { return(temp); } temp = temp.Next; } //If itemNumber index is not reached then there were not enough items in the list. return(null); }
/// <summary> /// FindItemDescending - Finds the the nth number item from the end of the list /// The algorithm requires at least one full pass on the entire list so that we can find the end of the list /// Because we can only traverse the list once, we have to save each reference for each item access into /// a revolving collection stored in arrayOfReference which is the size of the item trying to be found /// /// </summary> /// <param name="nth"> This parameter is assumed to be index 1 instead of index 0 based.</param> /// <returns></returns> private MyLinkedListItem <T> FindItemDescending(ulong nth) { MyLinkedListItem <T>[] arrayOfReferences = new MyLinkedListItem <T> [nth]; MyLinkedListItem <T> temp = _start; MyLinkedListItem <T> retItem = null; //If there is a null linked list then return null; if (_start == null || nth == 0) { return(null); } ulong index = 0; while (temp.Next != null) { //Continue writing items into array. If we exceed nth then //the mod operation will begin overwriting entries at the beginning of //the array. arrayOfReferences[index++ % nth] = temp; temp = temp.Next; } if (index >= nth) { retItem = arrayOfReferences[index % nth]; } return(retItem); }
public void Push(T itemVal) { if (_start == null) { _start = new MyLinkedListItem <T>(itemVal); _end = _start; } else { _end = _end.Add(itemVal); } }
public MyLinkedListItem <T> FindItem(ulong itemNumber, MyLinkListSearchDirection direction) { MyLinkedListItem <T> retItem = null; switch (direction) { case MyLinkListSearchDirection.Ascending: retItem = FindItemAscending(itemNumber); break; case MyLinkListSearchDirection.Descending: retItem = FindItemDescending(itemNumber); break; } return(retItem); }
public void Clear() { if (_start == null) { return; } MyLinkedListItem <T> current = _start; MyLinkedListItem <T> next = _start.Next; while (next != null) { next = current.Next; current.Unlink(); } _start = null; }
private void btnSearch_Click(object sender, EventArgs e) { ulong itemNumber = 0; if (!ulong.TryParse(txtItemNumber.Text, out itemNumber)) { MessageBox.Show("Please enter a valid non-negative number into the Item Number field.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtItemNumber.Focus(); txtItemNumber.SelectAll(); return; } MyLinkListSearchDirection searchType = (cmbSearchType.Text == "ASC") ? MyLinkListSearchDirection.Ascending : MyLinkListSearchDirection.Descending; string searchDataResult = "Unable to find entry. Item Number is out of range."; if (bIntegerOrString) { if (IntegerLinkedList.IsEmpty) { MessageBox.Show("Please load a file with integer data.", "Integer Data Empty", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } MyLinkedListItem <long> item = IntegerLinkedList.FindItem(itemNumber, searchType); if (item != null) { searchDataResult = string.Format("Integer data found at position {0} from {1} of linked list.{2}Value: {3}", itemNumber, (searchType == MyLinkListSearchDirection.Ascending) ? "front" : "back", Environment.NewLine, item.Value); } } else { if (StringLinkedList.IsEmpty) { MessageBox.Show("Please load a file with string data.", "String Data Empty", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } MyLinkedListItem <string> item = StringLinkedList.FindItem(itemNumber, searchType); if (item != null) { searchDataResult = string.Format("String data found at position {0} from {1} of linked list.{2}Value: {3}", itemNumber, (searchType == MyLinkListSearchDirection.Ascending) ? "front" : "back", Environment.NewLine, item.Value); } } txtFoundData.Text = searchDataResult; }
public void Unlink() { _next = null; }
public void Add(MyLinkedListItem <T> item) { _next = item; }
public MyLinkedListItem <T> Add(T item) { this._next = new MyLinkedListItem <T>(item); return(this._next); }
public MyLinkedListItem(T item) { Value = item; _next = null; }