Question: Add a ToArray method to OurList class below that returns an array containing all of the elements in this list in proper sequence (from first
Add a ToArray method to OurList class below that returns an array containing all of the elements in this list in proper sequence (from first to last element). Test your method in a main routine in a different class (e..g. don't put a main routine in the list class).
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LinkListExample { public class OurList { private class OurListNode { public T Data { get; set; } public OurListNode Next { get; set; } public OurListNode(T paramData, OurListNode paramNext) { this.Data = paramData; this.Next = paramNext; } } private OurListNode first; public OurList() { first = null; } public void Clear() // shown in class notes { first = null; } public void AddFirst(T data) // shown in class notes { this.first = new OurListNode(data, this.first); } public void RemoveFirst() // shown in class notes { if (first != null) first = first.Next; } public void AddLast(T data) // shown in class notes { if (first == null) AddFirst(data); else { OurListNode mTmp = first; while (mTmp.Next != null) mTmp = mTmp.Next; mTmp.Next = new OurListNode(data, null); } } public void RemoveLast() // shown in class notes { if (first == null) return; else if (first.Next == null) RemoveFirst(); else { OurListNode pTmp = first; while (pTmp.Next != null && pTmp.Next.Next != null) pTmp = pTmp.Next; pTmp.Next = null; } } public void Display() // shown in class notes { OurListNode pTmp = first; while (pTmp != null) { Console.Write("{0}, ", pTmp.Data); pTmp = pTmp.Next; } Console.WriteLine(); } public bool IsEmpty() // shown in class notes { if (first == null) return true; else return false; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
