I need somone help me in this assignment please by C# 1. Create a Generic Linked
ID: 3883142 • Letter: I
Question
I need somone help me in this assignment please by C#
1. Create a Generic LinkedList and test your LinkedList by inserting and removing nodes.
Include at least the following methods:
bool IsEmpty() (determine if your list is empty or not)
void InsertHeadNode(T headvalue) (insert a node with value headvalue to the front)
void InsertTailNode(T tailvalue) (insert a node with value tailvalue to the end)
T RemoveHeadNode () (remove the first Node and return its value)
T RemoveTailNode () (remove the last Node and return its value)
void PrintList() (print the value of all nodes in the list)
void InsertAfter (Node p, T newvalue) (insert a new node with value newvalue after node p )
void RemoveAfter (Node p ) (remove the node after node p )
Node<T> Find(T target) ( find and return the first occurrence of a node with value target)
2. Complete the attached MyLinkedStack.cs.
Part1. Complete the MyListStack. There are four methods to be completed: Push(), Pop(), IsEmpty() and Size() .
Part2. Complete a method to evaluate postfix expressions using MyListStack. (Don't know what is postfix! Refer to my slides on Stacks in Course Documents.)
You are supposed to submit the two.cs files (for the two exercises above)
Since most of you are confused about removeatLast() method for the linked list, here is a code snippet to give you some hints. Instead of creating a doubly linked list and previous links to find the previous node, you may do the following instead to get to the second last node. This way you still create a singly linked list (less complicated)
tail = current; //create a tail node to point to the last node to be deleted
current = head; // point to the head node
//Locate the node before tail node
while (current.next != tail) // and until you come to the one before the tail node
{ current = current.next; // keep pointing to the next one in line }
tail = current; // when you've exited that loop, you've reached the one whose "next" is the tail
tail.next = null; // and that one is now the last one in the list, we've eliminated the old tail
count--;
-------------------------------------------------------------------------------------
Since most of you are confused about removeatLast() method for the linked list, here is a code snippet to give you some hints. Instead of creating a doubly linked list and previous links to find the previous node, you may do the following instead to get to the second last node. This way you still create a singly linked list (less complicated)
tail = current; //create a tail node to point to the last node to be deleted
current = head; // point to the head node
//Locate the node before tail node
while (current.next != tail) // and until you come to the one before the tail node
{ current = current.next; // keep pointing to the next one in line }
tail = current; // when you've exited that loop, you've reached the one whose "next" is the tail
tail.next = null; // and that one is now the last one in the list, we've eliminated the old tail
count--;
Explanation / Answer
(Rate if satisfied else comment for queries)
1.Generic LinkedList
Class ListNode<T>
public class ListNode<T>
{
private ListNode<T> next;
private T item;
public ListNode<T> Next
{
get { return next; }
set { next = value; }
}
public T Item
{
get { return item; }
set { item = value; }
}
public ListNode(T item)
: this(item,null)
{
}
public ListNode(T item, ListNode<T> next)
{
this.item = item;
this.next = next;
}
public override string ToString()
{
if (item == null)
return string.Empty;
return item.ToString();
}
}
SinglyLinkedList<T> class
public class SinglyLinkedList<T>:ICollection<T>
{
#region private variables
private string strListName;
private ListNode<T> firstNode;
private ListNode<T> lastNode;
private int count;
#endregion
public ListNode<T> FirstNode
{
get { return firstNode; }
}
public ListNode<T> LastNode
{
get { return lastNode; }
}
public T this[int index]
{
get
{
if (index < 0)
throw new ArgumentOutOfRangeException();
ListNode<T> currentNode = firstNode;
for (int i = 0; i < index; i++)
{
if (currentNode.Next == null)
throw new ArgumentOutOfRangeException();
currentNode = currentNode.Next;
}
return currentNode.Item;
}
}
public int Count
{
get { return count; }
}
public bool IsEmpty
{
get
{
lock (this)
{
return firstNode == null;
}
}
}
public SinglyLinkedList(string strListName)
{
this.strListName = strListName;
count = 0;
firstNode = lastNode = null;
}
public SinglyLinkedList() : this("MyList") { }
public override string ToString()
{
if (IsEmpty)
return string.Empty;
StringBuilder returnString = new StringBuilder();
foreach (T item in this)
{
if (returnString.Length > 0)
returnString.Append("->");
returnString.Append(item);
}
return returnString.ToString();
}
public void InsertHeadNode(T item)
{
lock (this)
{
if (IsEmpty)
firstNode = lastNode = new ListNode<T>(item);
else
firstNode = new ListNode<T>(item, firstNode);
count++;
}
}
public void InsertTailNode(T item)
{
lock (this)
{
if (IsEmpty)
firstNode = lastNode = new ListNode<T>(item);
else
lastNode = lastNode.Next = new ListNode<T>(item);
count++;
}
}
public object RemoveHeadNode()
{
lock (this)
{
if (IsEmpty)
throw new ApplicationException("list is empty");
object removedData = firstNode.Item;
if (firstNode == lastNode)
firstNode = lastNode = null;
else
firstNode = firstNode.Next;
count--;
return removedData;
}
}
public object RemoveTailNode ()
{
lock (this)
{
if (IsEmpty)
throw new ApplicationException("list is empty");
object removedData = lastNode.Item;
if (firstNode == lastNode)
firstNode = lastNode = null;
else
{
ListNode<T> currentNode = firstNode;
while (currentNode.Next != lastNode)
currentNode = currentNode.Next;
lastNode = currentNode;
currentNode.Next = null;
}
count--;
return removedData;
}
}
public void InsertAfter(int index, T item)
{
lock (this)
{
if (index > count || index < 0)
throw new ArgumentOutOfRangeException();
if (index == 0)
InsertAtFront(item);
else if (index == (count - 1))
InsertAtBack(item);
else
{
ListNode<T> currentNode = firstNode;
for (int i = 0; i < index - 1; i++)
{
currentNode = currentNode.Next;
}
ListNode<T> newNode = new ListNode<T>(item, currentNode.Next);
currentNode.Next = newNode;
count++;
}
}
}
public object RemoveAfter (int index)
{
lock (this)
{
if (index > count || index < 0)
throw new ArgumentOutOfRangeException();
object removedData;
if (index == 0)
removedData = RemoveFromFront();
else if (index == (count - 1))
removedData = RemoveFromBack();
else
{
ListNode<T> currentNode = firstNode;
for (int i = 0; i < index; i++)
{
currentNode = currentNode.Next;
}
removedData = currentNode.Item;
currentNode.Next = currentNode.Next.Next;
count--;
}
return removedData;
}
}
public bool Remove(T item)
{
if (firstNode.Item.ToString().Equals(item.ToString()))
{
RemoveFromFront();
return true;
}
else if (lastNode.Item.ToString().Equals(item.ToString()))
{
RemoveFromBack();
return true;
}
else
{
ListNode<T> currentNode = firstNode;
while (currentNode.Next != null)
{
if (currentNode.Next.Item.ToString().Equals(item.ToString()))
{
currentNode.Next = currentNode.Next.Next;
count--;
if (currentNode.Next == null)
lastNode = currentNode;
return true;
}
currentNode = currentNode.Next;
}
}
return false;
}
public bool Contains(T item)
{
lock (this)
{
ListNode<T> currentNode = firstNode;
while (currentNode != null)
{
if (currentNode.Item.ToString().Equals(item.ToString()))
{
return true;
}
currentNode = currentNode.Next;
}
return false;
}
}
}
2. MyLinkedStack.cs.
part1. MyListStack.cs
class MyListStack
{
private int[] arr;
private int topOfStack;
public MyListStack(int size)
{
arr = new int[size];
topOfStack = 0;
}
public int size(){
return arr.Length;
}
public void Push(int value)//inserts an object or value onto the stack
{
if (topOfStack > arr.Length - 1)
{
Array.Resize(ref arr, topOfStack + 1);
arr[topOfStack] = value;
}
else
{
arr[topOfStack] = value;
}
topOfStack++;
}
public int Pop()//removes the object or value at the top of the stack
{
int val;
if (isEmpty())
{
return 0;
}
else
{
topOfStack --;
val = arr[topOfStack];
Array.Resize(ref arr, topOfStack);//resize the array
return val;
}
}
public bool isEmpty()//to check if the stack is empty
{
if (arr.Length >= 1)
{
return false;
}
else
{
return true;
}
}
}
Part 2.Postfix Evaluation
class postfix
{
public string po;
public string answer;
MyListStack i = new MyListStack(100);
public void e()
{
int a, b, ans;
for (int j = 0; j < po.Length; j++)
{
String c = po.Substring(j, 1);
if (c.Equals ("*"))
{
String sa = (String)i.Pop();
String sb = (String)i.Pop();
a = Convert.ToInt32(sb);
b = Convert.ToInt32(sa);
ans = a * b;
i.Push(ans.ToString());
}
else if (c.Equals("/"))
{
String sa = (String)i.Pop();
String sb = (String)i.Pop();
a = Convert.ToInt32(sb);
b = Convert.ToInt32(sa);
ans = a / b;
i.Push(ans.ToString());
}
else if (c.Equals("+"))
{
String sa = (String)i.Pop();
String sb = (String)i.Pop();
a = Convert.ToInt32(sb);
b = Convert.ToInt32(sa);
ans = a + b;
i.Push(ans.ToString());
}
else if (c.Equals("-"))
{
String sa = (String)i.Pop();
String sb = (String)i.Pop();
a = Convert.ToInt32(sb);
b = Convert.ToInt32(sa);
ans = a - b;
i.Push(ans.ToString());
}
else
{
i.Push(po.Substring(j, 1));
}
}
answer=(String)i.Pop();
}
}