Question: Add a GetMiddle method to the linked list class (OurList). It returns the middle Data item from the list. If there are 0 or 1

Add a GetMiddle method to the linked list class (OurList). It returns the middle Data item from the list. If there are 0 or 1 items on the list, throw something.

public class OurList { private class OurListNode { public T Data { get; set; } public OurListNode Next { get; set; } public OurListNode(T d = default(T), OurListNode ln = null) { Data = d; Next = ln; } } private OurListNode mFirst; public OurList() { mFirst = null; } public void Clear() { mFirst = null; } public void AddFirst(T data) { mFirst = new OurListNode(data, mFirst); } public void RemoveFirst() { if (mFirst != null) mFirst = mFirst.Next; } public void AddLast(T data) { if (mFirst == null) AddFirst(data); else { OurListNode mTmp = mFirst; while (mTmp.Next != null) mTmp = mTmp.Next; mTmp.Next = new OurListNode(data, null); } } public void RemoveLast() { if (mFirst == null) return; else if (mFirst.Next == null) RemoveFirst(); else { OurListNode pTmp = mFirst; while (pTmp.Next != null && pTmp.Next.Next != null) pTmp = pTmp.Next; pTmp.Next = null; } } public void Display() { OurListNode pTmp = mFirst; while (pTmp != null) { Console.Write("{0}, ", pTmp.Data); pTmp = pTmp.Next; } Console.WriteLine(); } public bool isEmpty() { if (mFirst == null) return true; else return false; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!