diff --git a/Class Examples/mutex.cs b/Class Examples/mutex.cs new file mode 100644 index 0000000..ff93d3d --- /dev/null +++ b/Class Examples/mutex.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace mutexexample2 +{ + class Program + { + public static Mutex m = new Mutex(); + public static readonly int LOOP = 50000; + public static long gvar = 0; + + static public void produce() + { + m.WaitOne(); + for (int i = 0; i < LOOP; i++) + gvar++; + m.ReleaseMutex(); + } + + static public void consume() + { + m.WaitOne(); + for (int i = 0; i < LOOP; i++) + gvar--; + m.ReleaseMutex(); + } + + static void Main(string[] args) + { + Thread t1 = new Thread(new ThreadStart(produce)); + t1.Start(); + + Thread t2 = new Thread(new ThreadStart(consume)); + t2.Start(); + + t1.Join(); + t2.Join(); + + Console.WriteLine(gvar); + } + } +} diff --git a/Class Examples/node-generic.cs b/Class Examples/node-generic.cs new file mode 100644 index 0000000..46ec617 --- /dev/null +++ b/Class Examples/node-generic.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace generics +{ + class Program + { + class Node where T : System.IComparable + { + public T element; + public Node left = null; + public Node right = null; + Node parent = null; + + public Node createNode(T element) + { + Node n = new Node() { parent = this, element = element }; + return n; + } + + public Node addLeft(T element) + { + this.left = createNode(element); + return this.left; + } + public Node addRight(T element) + { + this.right = createNode(element); + return this.right; + } + public override string ToString() + { + return "Node element => " + element.ToString(); + } + + } + static Node dfs(T val, Node parent) where T : System.IComparable + { + if (parent == null) + return null; + + if (parent.element.CompareTo(val) == 0) + return parent; + + Node ret = null; + if (parent.left != null) + ret = dfs(val, parent.left); + if (ret != null) + return ret; + if (parent.right != null) + ret = dfs(val, parent.right); + if (ret != null) + return ret; + return null; + } + static void Main(string[] args) + { + Node n = new Node(); + n.addLeft(4).addLeft(10).addLeft(7); + n.addRight(5).addRight(2); + Console.WriteLine(dfs(2, n)); + + } + } +} diff --git a/Class Examples/node.cs b/Class Examples/node.cs new file mode 100644 index 0000000..72e78cd --- /dev/null +++ b/Class Examples/node.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace generics +{ + class Program + { + class Node + { + public int element; + public Node left = null; + public Node right = null; + Node parent = null; + + public Node createNode(int element) + { + Node n = new Node() { parent = this, element = element }; + return n; + } + + public Node addLeft(int element) + { + this.left = createNode(element); + return this.left; + } + public Node addRight(int element) + { + this.right = createNode(element); + return this.right; + } + public override string ToString() + { + return "Node element => " + element.ToString(); + } + } + static Node dfs(int val, Node parent) + { + if (parent == null) + return null; + + if (parent.element == val) + return parent; + + Node ret = null; + if (parent.left != null) + ret = dfs(val, parent.left); + if (ret != null) + return ret; + if (parent.right != null) + ret = dfs(val, parent.right); + if (ret != null) + return ret; + return null; + } + static void Main(string[] args) + { + Node n = new Node(); + n.addLeft(4).addLeft(10).addLeft(7); + n.addRight(5).addRight(2); + Console.WriteLine(dfs(22, n)); + + } + } +} diff --git a/Class Examples/produce-consume.cs b/Class Examples/produce-consume.cs new file mode 100644 index 0000000..9a09a4a --- /dev/null +++ b/Class Examples/produce-consume.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace mutexexample2 +{ + class Program + { + public static readonly int LOOP = 50000; + public static long gvar = 0; + + static public void produce() + { + for (int i = 0; i < LOOP; i++) + gvar++; + } + + static public void consume() + { + for (int i = 0; i < LOOP; i++) + gvar--; + } + + static void Main(string[] args) + { + Thread t1 = new Thread(new ThreadStart(produce)); + t1.Start(); + + Thread t2 = new Thread(new ThreadStart(consume)); + t2.Start(); + + t1.Join(); + t2.Join(); + + Console.WriteLine(gvar); + } + } +} diff --git a/Class Examples/swap.cs b/Class Examples/swap.cs new file mode 100644 index 0000000..055823e --- /dev/null +++ b/Class Examples/swap.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace generics +{ + class Program + { + static void swap(ref T lhs,ref T rhs) + { + T temp; + temp = lhs; + lhs = rhs; + rhs = temp; + } + static void Main(string[] args) + { + int a, b; + a = 10; + b = 20; + Console.WriteLine(a.ToString() + " " + b.ToString()); + swap(ref a, ref b); + Console.WriteLine(a.ToString() + " " + b.ToString()); + + } + } +}