Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions