From b8a5b2dc65e263a67cbf247445114fb0c3ae6436 Mon Sep 17 00:00:00 2001 From: sriharikarthik08 <73272009+sriharikarthik08@users.noreply.github.com> Date: Thu, 22 Oct 2020 15:31:27 +0530 Subject: [PATCH] c# linked list plz review the code --- C#/LinkedList.cs | 417 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 385 insertions(+), 32 deletions(-) diff --git a/C#/LinkedList.cs b/C#/LinkedList.cs index 9e98d20..57d372c 100644 --- a/C#/LinkedList.cs +++ b/C#/LinkedList.cs @@ -1,33 +1,386 @@ -class LinkedList +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FirstApp { - private T value; - private LinkedList next; - - public LinkedList(T value, LinkedList next) - { - SetValue(value); - SetNext(next); - } - - public LinkedList(T value) : this(value, null) {} - - public T GetValue() - { - return value; - } - - public LinkedList GetNext() - { - return next; - } - - public void SetValue(T value) - { - this.value = value; - } - - public void SetNext(LinkedList next) - { - this.next = next; - } -} \ No newline at end of file + class LinkedList: ICollection where T:IComparable + { + internal class LinkedListNode + { + public T info; + public LinkedListNode next; + public LinkedListNode(T data, LinkedListNodenext= null) + { + info = data; + this.next = next; + } + } + + public LinkedListNode _first; + public LinkedListNode _last; + + private class Enumerator:IEnumerator + { + private LinkedListNode _first; + private LinkedListNode _current; + private bool atBegin; + + public Enumerator(LinkedListNode first) + { + _first = first; + _current = null; + atBegin = true; + } + public void Dispose() { } + + public bool MoveNext() + { + if(atBegin) + { + atBegin = false; + _current = _first; + return true; + } + else + { + _current = _current.next; + return _current != null; + } + } + + public void Reset() + { + atBegin = true; + _current = null; + } + + public T Current { + get { return _current.info; } + } + + object IEnumerator.Current + { + get { return Current; } + } + } + + public IEnumerator GetEnumerator() + { + return new Enumerator(_first); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + void ICollection.Add(T item) + { + LinkedListNode tmp = new LinkedListNode(item, null); + if(Count==0) + _first = tmp; + else + _last.next = tmp; + _last = tmp; + ++Count; + } + + public void AddLast(T item) + { + ((ICollection) this).Add(item); + } + + public void Clear() + { + _first = null; + _last = null; + Count = 0; + } + + public bool Contains(T item) + { + foreach (var x in this) + if (x.Equals(item)) + return true; + return false; + } + + public void CopyTo(T[] array, int arrayIndex) + { + int i = arrayIndex; + foreach(var item in this) + { + array[i] = item; + ++i; + } + } + + public bool Remove(T item) + { + if(_first!=null&&_first.info.Equals(item)) + { + _first = _first.next; + --Count; + return true; + } + else + { + LinkedListNode before = FindPrev(item); + if (before == null) + return false; + before.next = before.next?.next; + --Count; + return true; + } + } + + private LinkedListNode FindPrev(T item) + { + var prev = _first; + var cur = _first?.next; + while (cur != null) + { + if (cur.info.Equals(item)) + return prev; + prev = cur; + cur = cur.next; + } + return null; + } + + public int Count { get; private set; } + + public bool IsReadOnly => false; + + public void InsertionSort() + { + LinkedListNode old = _first; + _first = null; + while(old!= null) + { + LinkedListNode tmp = old; + old = old.next; + tmp.next = null; + Insert(tmp); + } + } + + private void Insert(LinkedListNode tmp) + { + if (_first == null) + { + _first = tmp; + _last = tmp; + } + else if(_first.info.CompareTo(tmp.info)>=0) + { + tmp.next = _first; + _first = tmp; + } + else if(_last.info.CompareTo(tmp.info)<=0) + { + _last.next = tmp; + _last = tmp; + } + else + { + LinkedListNode prev = _first; + LinkedListNode cur = _first?.next; + + while(cur!=null&&cur.info.CompareTo(tmp.info)<0) + { + prev = cur; + cur = cur.next; + } + tmp.next = cur; + prev.next = tmp; + } + } + + private void Insert(ref LinkedListNodefirst ,LinkedListNode tmp) + { + if (first == null) + { + first = tmp; + } + else if (first.info.CompareTo(tmp.info) >= 0) + { + tmp.next = first; + first = tmp; + } + else + { + LinkedListNode prev = first; + LinkedListNode cur = first?.next; + + while (cur != null && cur.info.CompareTo(tmp.info) < 0) + { + prev = cur; + cur = cur.next; + } + tmp.next = cur; + prev.next = tmp; + } + } + + public void MergeSort() + { + MergeSort(ref _first, Count); + } + + private LinkedListNode GetMid(ref LinkedListNode first, int count) + { + int mid = count / 2; + LinkedListNode t = first; + for (int i = 0; i < mid-1; ++i) + t = t.next; + return t; + } + + private void MergeSort(ref LinkedListNode first, int count) + { + if (count < 2) return; + + var t = GetMid(ref first, count); + + LinkedListNode right = t.next; + t.next = null; + + MergeSort(ref first, count/2); + MergeSort(ref right, count - count/2); + + first = Merge(t, right); + } + + private LinkedListNode Merge(LinkedListNode left, LinkedListNode right) + { + LinkedListNode rbegin,rend;//=left, rend=left; + if(left.info.CompareTo(right.info)<0) + { + rbegin = left; + rend = left; + left = left.next; + } + else + { + rbegin = right; + rend = right; + right = right.next; + } + + while(left!=null&&right!=null) + { + while(left != null && left.info.CompareTo(right.info)<=0) + { + rend.next = left; + rend = rend.next; + left = left.next; + } + if (left == null) + break; + while (right.info.CompareTo(left.info) <= 0) + { + rend.next = right; + rend = rend.next; + right = right.next; + if (right == null) + break; + } + } + while(left!= null) + { + rend.next = left; + rend = rend.next; + left = left.next; + } + while (right != null) + { + rend.next = right; + rend = rend.next; + right = right.next; + } + rend.next = null; + return rbegin; + + //LinkedListNode result = null,tmp; + + //while(left!= null) + //{ + // tmp = left; + // left = left.next; + // tmp.next = null; + // Insert(ref result, tmp); + //} + //while (right != null) + //{ + // tmp = right; + // right = right.next; + // tmp.next = null; + // Insert(ref result, tmp); + //} + //return result; + } + + //private LinkedListNode PopFirst() + //{ + // var tmp = _first; + // _first = _first?.next; + // return tmp; + //} + } + + class Program + { + static void Main(string[] args) + { + LinkedList x = new LinkedList(); + x.AddLast(3); + x.AddLast(2); + x.AddLast(5); + x.AddLast(1); + x.AddLast(4); + foreach (var item in x) + Console.Write(item + " "); + Console.WriteLine(); + + x.MergeSort(); + foreach (var item in x) + Console.Write(item + " "); + Console.WriteLine(); + + //for (var i = x._first; i != null; i = i.next) + // Console.WriteLine(i.info); + + //System.Collections.Generic.LinkedList l = new System.Collections.Generic.LinkedList(); + + //var sw = new System.Diagnostics.Stopwatch(); + //sw.Start(); + //for (int i=0; i<10000000; i++) + //{ + // l.AddLast(i); + + // l.AddFirst(i); + //} + //sw.Stop(); + //Console.WriteLine(sw.ElapsedMilliseconds); + //Console.ReadKey(); + + //List ll = new List(); + //sw.Restart(); + //for (int i = 0; i < 10000000; i++) + //{ + // ll.Add(i); + // ll.Insert(0, i); + //} + //sw.Stop(); + //Console.WriteLine(sw.ElapsedMilliseconds); + + + + } + } +}