Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Linked List Add a method to the OurList class that returns a new list containing

ID: 3744825 • Letter: L

Question

Linked List

Add a method to the OurList class that returns a new list containing every 10th item on this list (e.g. the internal list pointed to by this.first). The order of the nodes in the resulting list doesn’t matter.

Example :

This List: 0, 1, 2, 3, ..., 97, 98, 99

List returned by method: 0,10,20,30,40,50,60,70,80,90

**Given Code**

Main:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace MyPrograms
{
public class TestLinkedList
{
public static void Main()
{
OurList aList = new OurList();
foreach (int x in Enumerable.Range(1, 10).Select(x => x * x))
aList.AddFront(x);
Console.WriteLine(aList);


Console.WriteLine();
Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
}
}

OurList Class:

public class OurList : ICollection, IEnumerable
{
private class OurListNode
{
public T Data { get; set; }
public OurListNode Next { get; set; }
public OurListNode(T d = default(T), OurListNode node = null)
{
Data = d;
Next = node;
}
}

private OurListNode mFirst;

public OurList() // shown in class
{
mFirst = null;
}

public void Clear() // shown in class
{
mFirst = null;
}

public bool IsEmpty() // shown in class
{
return mFirst == null;
}

public void AddFront(T value) // shown in class
{
mFirst = new OurListNode(value, mFirst);
}

public void RemoveFirst() // shown in class
{
if (mFirst != null)
mFirst = mFirst.Next;
}

public int Count
{
get
{
int count = 0;
OurListNode pTmp = mFirst;
while (pTmp != null)
{
count++;
pTmp = pTmp.Next;
}
return count;
}
}

public void AddLast(T value) // shown in class
{
if (mFirst == null)
AddFront(value);
else
{
OurListNode mTmp = mFirst;
while (mTmp.Next != null)
mTmp = mTmp.Next;

mTmp.Next = new OurListNode(value, null);
}
}

public void RemoveLast() // shown in class
{
if (mFirst == null)
return;
else if (mFirst.Next == null) // can't stop at previous node
RemoveFirst();
else
{
OurListNode pTmp = mFirst;
while (pTmp.Next.Next != null) // stop at 2nd to last node
pTmp = pTmp.Next;

pTmp.Next = null;
}
}

// If index equals the number of items in the IList, then value is appended to the end.
public void InsertAt(int index, T value)
{
if (index >= 0 && index <= Count)
{
if (index == 0)
AddFront(value);
else if (index == Count)
AddLast(value);
else
{
OurListNode pTmp = mFirst;
for (int i = 0; i < index - 1; i++)
pTmp = pTmp.Next;

OurListNode newNode = new OurListNode(value, pTmp.Next);
pTmp.Next = new OurListNode(value, pTmp.Next);

}
}
else
throw new ArgumentOutOfRangeException();
}

public void RemoveAt(int index)
{
if (index >= 0 && index < Count)
{
if (index == 0)
RemoveFirst();
else if (index == (Count - 1))
RemoveLast();
else
{
OurListNode pTmp = mFirst;
for (int i = 0; i < index - 1; i++)
pTmp = pTmp.Next;
pTmp.Next = pTmp.Next.Next;
}
}
else
throw new ArgumentOutOfRangeException();
}

public void Add(T value)
{
AddLast(value);
}

public bool Contains(T value)
{
OurListNode pTmp = mFirst;
while (pTmp != null)
{
if (pTmp.Data.Equals(value))
return true;
pTmp = pTmp.Next;
}
return false;
}
  
public bool IsReadOnly
{
get { return false; }
}

public bool Remove(T value)
{
if (IsEmpty() == true)
return false;

if (mFirst.Data.Equals(value))
{
RemoveFirst();
return true;
}
else
{
OurListNode pTmp = mFirst;

while (pTmp.Next != null)
{
if (pTmp.Next.Data.Equals(value))
{
pTmp.Next = pTmp.Next.Next;
return true;
}
pTmp = pTmp.Next;
}
}
return false;
}

public void CopyTo(T[] array, int arrayIndex)
{
throw new NotImplementedException();
}

public IEnumerator GetEnumerator()
{
OurListNode pTmp = mFirst;
while (pTmp != null)
{
yield return pTmp.Data;
pTmp = pTmp.Next;
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}


public override string ToString()
{
if (IsEmpty() == true)
return string.Empty;
StringBuilder returnString = new StringBuilder();
foreach (T item in this)
{
if (returnString.Length > 0)
returnString.Append(":");
returnString.Append(item);
}
return returnString.ToString();
}
}
}

Explanation / Answer

public OurList every10thNode() { OurList newList = new OurList(); OurListNode mTmp = mFirst; int count = 0; while (mTmp.Next != null) { if(count % 10 == 0) { newList.AddLast(mTmp.Data); } mTmp = mTmp.Next; count++; } return newList; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote