107 320
Assignments Done
98.9%
Successfully Done
In March 2023

# Answer to Question #189810 in C# for Ayesha

Question #189810

Implement your own version of the linked list using C# with mentioned functionalities: insert, insert at position, delete, delete at position, center, reverse, size, iterator, traverse/print.

Use of similar data structures already present in the language/framework is not allowed.

1
2021-05-06T04:24:05-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace C_SHARP
{
class Program
{
class Node<T>
{
public T item;
public Node<T> next;
public Node()
{
this.next = null;
}
}
{
public Node<T> head { get; set; }
{
}
/// <summary>
/// inserts item
/// </summary>
/// <param name="item"></param>
public void insert(T item)
{
Node<T> newNode = new Node<T>();
newNode.item = item;
{
}
else
{
while (tempNode.next != null)
{
tempNode = tempNode.next;
}
tempNode.next = newNode;
}
}
/// <summary>
/// Returns node by Position
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
private Node<T> getNodeByPosition(int position)
{
if (position < 0)
{
throw new IndexOutOfRangeException("Wrong index. Index: " + position);
}
int i = 0;
try
{
for (; i < position; ++i)
{
currentNode = currentNode.next;
}
}
catch (NullReferenceException e)
{
throw new IndexOutOfRangeException("Wrong index. Index: " + position + ", Size: " + i);
}
return currentNode;
}
/// <summary>
/// insert at position
/// </summary>
/// <param name="position"></param>
/// <param name="item"></param>
public void insertAt(int position, T item)
{
Node<T> newNode = new Node<T>();
newNode.item = item;
newNode.next = null;
if (position == 0)
{
}
else if (position == size())
{
insert(item);
}
else
{
Node<T> prevNode = getNodeByPosition(position - 1);
Node<T> nextNode = getNodeByPosition(position);
prevNode.next = newNode;
newNode.next = nextNode;
}
}
/// <summary>
/// Checks if LinkList is empty
/// </summary>
/// <returns></returns>
public bool isEmpty()
{
}
/// <summary>
/// Display the middle element  of the link list
/// </summary>
/// <returns></returns>
public T center()
{

{
while (node2 != null && node2.next != null)
{
node2 = node2.next.next;
node1 = node1.next;
}
}
return node1.item;
}
/// <summary>
/// Delete item
/// </summary>
/// <param name="item"></param>
public void delete(T item)
{
{
}
else
{
bool isFound = false;
while (!(isFound = tempNode.item.Equals(item)) && tempNode.next != null)
{
tempPrevious = tempNode;
tempNode = tempNode.next;
}
if (isFound)
{
tempPrevious.next = tempNode.next;
}
else
{
}
}
}
/// <summary>
/// Delete item at position
/// </summary>
/// <param name="position"></param>
public void deleteAt(int position)
{
return;

if (position == 0)
{
return;
}
// Find previous node
for (int i = 0; tempNode != null && i < position - 1; i++)
{
tempNode = tempNode.next;
}
if (tempNode == null || tempNode.next == null)
{
return;
}
Node<T> next = tempNode.next.next;
tempNode.next = next;
}

/// <summary>
/// </summary>
/// <returns></returns>
public int size()
{
int numberNodes = 0;
while (tempNode != null)
{
numberNodes++;
tempNode = tempNode.next;
}
return numberNodes;
}
/// <summary>
/// reverse
/// </summary>
public void reverse()
{
Node<T> previousNode = null;
Node<T> tempNode = null;
while (currentNode != null)
{
tempNode = currentNode.next;
currentNode.next = previousNode;
previousNode = currentNode;
currentNode = tempNode;
}
}
/// <summary>
/// Display items of link list
/// </summary>
public void print()
{
while (tempNode != null)
{
Console.WriteLine(tempNode.item);
tempNode = tempNode.next;
}
}
}

{
private Node<T> currentNode;

{
}
// returns false if next node does not exist
public bool hasNext()
{
return this.currentNode != null;
}

// return current item
public T next()
{
T data = this.currentNode.item;
this.currentNode = this.currentNode.next;
return data;
}
}
static void Main(string[] args)
{

try
{
Console.WriteLine("Intger items");
Console.WriteLine("The middle element is: " + linkList.center() + "\n");
Console.WriteLine("Reverse: ");

{
}

Console.WriteLine("Delete 2 from the link list");
Console.WriteLine("DeleteAt position 1 from the link list");

Console.WriteLine("\n\nString items");
Console.WriteLine("The middle element is: " + linkListNames.center() + "\n");
Console.WriteLine("Reverse: ");

{
}

Console.WriteLine("Delete 'Mary' from the link list");
Console.WriteLine("DeleteAt position 1 from the link list");
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
}
}

Output

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!