static void Compute(CStack N, CStack O) { int operand1; int operand2; string oper; operand1 = Convert.ToInt32(N.Pop()); operand2 = Convert.ToInt32(N.Pop()); oper = Convert.ToString(O.Pop()); switch (oper) { case "+": N.Push(operand1 + operand2); break; case "-": N.Push(operand1 - operand2); break; case "*": N.Push(operand1 * operand2); break; case "/": N.Push(operand1 / operand2); break; } }
// Use this for initialization void Start() { CStack <int> tStack = new CStack <int>(); tStack.Push(5); tStack.Push(10); int ta = tStack.Pop(); int tb = tStack.Pop(); Debug.Log(ta); Debug.Log(tb); int tA_0 = 5; int tB_0 = 777; DoSwap(ref tA_0, ref tB_0); Debug.Log(tA_0); Debug.Log(tB_0); //런타임이 추론에 헷갈리지 않게 표기하여 지정해줄 수도 있다. DoSwap <int>(ref tA_0, ref tB_0); Debug.Log(tA_0); Debug.Log(tB_0); }
public void TestMethodStackPush() { CStack <string> s = new CStack <string>(); Assert.AreEqual(s.Size, 0); s.Push("a"); Assert.AreEqual(s.Size, 1); Assert.AreEqual(s.Top(), "a"); s.Push("b"); Assert.AreEqual(s.Size, 2); Assert.AreEqual(s.Top(), "b"); }
public GameEntryUI() { this.InitializeComponent(); CStack lifeStack = new CStack(); lifeStack.Push(3); lifeStack.Push(2); lifeStack.Push(1); int popped = lifeStack.Pop(); int check = lifeStack.Peek(); }
static void Calculate(CStack N, CStack O, string exp) { string ch; string token = string.Empty; for (int p = 0; p < exp.Length; p++) { ch = exp.Substring(p, 1); if (IsNumber(ch) == true) { token += ch; } if (ch == " " || p == (exp.Length - 1)) { if (IsNumber(token)) { N.Push(token); token = string.Empty; } } else if (ch == "+" || ch == "-" || ch == "*" || ch == "/") { O.Push(ch); } if (N.Count == 2) { Compute(N, O); } } }
public void TestMethodStackPop() { CStack <string> s = new CStack <string>(); Assert.AreEqual(s.Size, 0); s.Push("a"); s.Push("b"); s.Push("c"); Assert.AreEqual(s.Top(), "c"); s.Pop(); Assert.AreEqual(s.Top(), "b"); s.Pop(); Assert.AreEqual(s.Top(), "a"); s.Pop(); Assert.IsTrue(s.IsEmpty()); Assert.ThrowsException <Exception>(() => s.Top()); Assert.ThrowsException <Exception>(() => s.Pop()); }
// Use this for initialization void Start() { CStack <int> tStack = new CStack <int>(); tStack.Push(5); tStack.Push(10); int tA = tStack.Pop(); int tB = tStack.Pop(); Debug.Log(tA); Debug.Log(tB); int tA_0 = 5; int tB_0 = 777; DoSwap(ref tA_0, ref tB_0); Debug.Log(tA_0); Debug.Log(tB_0); DoSwap <int>(ref tA_0, ref tB_0); Debug.Log(tA_0); Debug.Log(tB_0); }
public static void RunStack() { CStack <int> s = new CStack <int>(); for (int i = 1; i <= 3; i++) { s.Push(i); } Console.WriteLine(s); s.Pop(); Console.WriteLine(s); s.Pop(); Console.WriteLine(s); s.Pop(); Console.WriteLine(s); Console.WriteLine(s.Size); Console.WriteLine(s.IsEmpty()); //Console.ReadLine(); }
//Klicken des Push Buttons protected void OnBtnPushClicked(object sender, EventArgs e) { //Auslesen des Strings in der Textbox und Speichern in einer int Variable "i_e" int.TryParse(txt_eingabe.Text, out int i_e); //Prüfen, ob man die Zahl noch auf den Stack legen kann if (S1.Push(i_e) != true) { //Falls nein, dann wird eine Fehlermeldung in einem Popup ausgeben MessageDialog mb = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Info, ButtonsType.Close, "Der Stack ist voll!"); mb.Run(); mb.Destroy(); } else { //Wenn die Zahl auf dem Stack abgelegt werden konnte, wird die Zahl aus dem Textfeld gelöscht txt_eingabe.DeleteText(0, -1); //Danach wird die Füllstandsanzeige geupdated lbl_fuellstand.Text = "Füllstand: " + S1.Get_fuellstand().ToString(); //Und die Anzeige des Stacks wird auch geupdated txt_view.Buffer.Text = S1.fill_listbox(); } }
// Start is called before the first frame update void Start() { // 제네틱 사용하지 않았을때 스택을 사용하는 코드 int size = 5; CStack <int> istack = new CStack <int>(size); for (int i = 0; i < 7; i++) { if (!istack.IsFull()) { istack.Push(i); } else { Debug.Log("스택이 꽉 찼습니다. [저장 실패 : " + i + "]"); } } while (!istack.IsEmpty()) { Debug.Log(istack.Pop()); } CStack <float> fstack = new CStack <float>(size); for (int i = 0; i < 7; i++) { if (!fstack.IsFull()) { fstack.Push(i * 1.2f); } else { Debug.Log("스택이 꽉 찼습니다. [저장 실패 : " + i + "]"); } } while (!fstack.IsEmpty()) { Debug.Log(fstack.Pop()); } CStack <string> sstack = new CStack <string>(size); for (int i = 0; i < 7; i++) { if (!sstack.IsFull()) { sstack.Push(i + "번"); } else { Debug.Log("스택이 꽉 찼습니다. [저장 실패 : " + i + "]"); } } while (!sstack.IsEmpty()) { Debug.Log(sstack.Pop()); } }
static void Main(string[] args) { LinkedList <int> ll = new LinkedList <int>(); TimerPerformance tp = new TimerPerformance(); tp.StartTiming(); ll.AddNode(1); Console.WriteLine($"Time for adding \"1\": {tp.StopTiming()}"); ll.AddNode(2); ll.AddNode(3); ll.AddNode(4); ll.AddNode(5); ll.AddNode(6); ll.AddNode(7); ll.AddNode(8); tp.StartTiming(); ll.AddNode(9); Console.WriteLine($"Time for adding \"9\": {tp.StopTiming()}"); ll.AddNode(10); ll.AddNode(11); ll.AddNode(12); ll.AddNode(13); ll.AddNode(14); ll.AddNode(15); ll.AddNode(16); ll.AddNode(17); tp.StartTiming(); ll.AddNode(18); Console.WriteLine($"Time for adding \"18\": {tp.StopTiming()}"); Console.WriteLine("-------------------------DOUBLY LINKED LISTS-------------"); var doubleLL = new DoublyLinkedList <int>(); doubleLL.AddNode(1); doubleLL.AddNode(2); doubleLL.AddNode(3); doubleLL.AddNode(4); doubleLL.AddNode(5); doubleLL.AddNode(6); doubleLL.AddNode(7); doubleLL.AddNode(8); doubleLL.AddNode(9); doubleLL.AddNode(10); doubleLL.AddNode(11); doubleLL.AddNode(12); doubleLL.AddNode(13); doubleLL.AddNode(14); doubleLL.AddNode(15); doubleLL.AddNode(16); doubleLL.AddNode(17); doubleLL.AddNode(18); foreach (var item in doubleLL) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine("-----------Previous value of node with value \"3\"------------"); Console.WriteLine(((DoubleNode <int>)doubleLL.HeadNode.NextNode.NextNode).PreviousNode.Value); Console.WriteLine($"\nDeleteAt(3)"); doubleLL.DeleteAt(3); //foreach(var item in doubleLL) // Console.WriteLine(item); foreach (var item in doubleLL) { Console.WriteLine(item); } Console.WriteLine("\nPrevious node of element at 3rd index: "); Console.Write((doubleLL.HeadNode.NextNode.NextNode.NextNode as DoubleNode <int>).PreviousNode.Value); Console.WriteLine(); Console.WriteLine("Merge nodes and multiply values for singly linked list -----------------"); Console.WriteLine(); MergeNodesAndMultValues(ll.HeadNode); foreach (var item in ll.ReadValues()) { Console.WriteLine(item); } //Console.WriteLine($"\nValue of the previous node in singly linked list: {ll.HeadNode.NextNode.NextNode.Value}"); Console.WriteLine("STACK DATA STRUCTURE ----------------------"); CStack <int> stack = new CStack <int>(); stack.Push(1); stack.Push(2); stack.Push(3); stack.Push(4); stack.Push(5); stack.Push(7); stack.Push(8); stack.Push(9); stack.Push(10); stack.Push(11); stack.Push(12); Console.WriteLine("\ncontinuous .Pop() calls"); Console.WriteLine(); foreach (var item in stack) { Console.WriteLine(item); } Console.WriteLine(); stack.Pop(); foreach (var item in stack) { Console.WriteLine(item); } Console.WriteLine(); stack.Pop(); foreach (var item in stack) { Console.WriteLine(item); } Console.WriteLine(); stack.Pop(); foreach (var item in stack) { Console.WriteLine(item); } Console.WriteLine("\nSTACK DATA STRUCTURE WITH LINKED LIST ----------------------"); LLStack <int> llStack = new LLStack <int>(); llStack.Push(1); llStack.Push(2); llStack.Push(3); llStack.Push(4); llStack.Push(5); llStack.Push(6); llStack.Push(7); llStack.Push(8); llStack.Push(9); llStack.Push(10); Console.WriteLine("Read llStack values----------"); Console.WriteLine(); foreach (var item in llStack) { Console.WriteLine(item); } Console.WriteLine("llStack.Pop()"); llStack.Pop(); foreach (var item in llStack) { Console.WriteLine(item); } Console.WriteLine("llStack.Pop()"); llStack.Pop(); foreach (var item in llStack) { Console.WriteLine(item); } Console.WriteLine("llStack.Push(11)"); llStack.Push(11); foreach (var item in llStack) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine("Intersecting node------"); LinkedList <int> l1 = new LinkedList <int>(), l2 = new LinkedList <int>(); l1.AddNode(1); l1.AddNode(2); l1.AddNode(3); l1.AddNode(4); l1.AddNode(7); l1.AddNode(8); l1.AddNode(9); l2.AddNode(2); l2.AddNode(4); Console.WriteLine(LinkedList <int> .FindIntesectingNode(l1, l2)); l1.GetNthReverseNode(1); foreach (var item in l1) { Console.WriteLine(item); } //QUEUE //Console.WriteLine("\nQUEUE DATA STRUCTURE USAGE\n"); //CQueue<int> queue = new CQueue<int>(); //queue.Enqueue(1); //queue.Enqueue(3); //queue.Enqueue(2); //queue.Enqueue(4); //queue.Enqueue(5); //queue.Enqueue(6); //queue.Enqueue(7); //queue.Enqueue(8); //queue.Enqueue(7); //queue.Dequeue(); //queue.Dequeue(); //queue.Enqueue(9); //queue.Enqueue(10); //queue.Enqueue(11); //foreach (var item in queue) // Console.WriteLine(item); //Console.WriteLine("queue.Dequeue() * 3"); //Console.WriteLine($"ITEM REMOVED: {queue.Dequeue()}"); //Console.WriteLine($"ITEM REMOVED: {queue.Dequeue()}"); //Console.WriteLine($"ITEM REMOVED: {queue.Dequeue()}"); //Console.WriteLine($"ITEM REMOVED: {queue.Dequeue()}"); //Console.WriteLine($"ITEM REMOVED: {queue.Dequeue()}"); //Console.WriteLine($"ITEM REMOVED: {queue.Dequeue()}"); //Console.WriteLine($"ITEM REMOVED: {queue.Dequeue()}"); //Console.WriteLine($"ITEM REMOVED: {queue.Dequeue()}"); //Console.WriteLine($"ITEM REMOVED: {queue.Dequeue()}"); //Console.WriteLine($"ITEM REMOVED: {queue.Dequeue()}"); //Console.WriteLine($"ITEM REMOVED: {queue.Dequeue()}"); //Console.WriteLine($"ITEM REMOVED: {queue.Dequeue()}"); //Console.WriteLine($"ITEM REMOVED: {queue.Dequeue()}"); //Console.WriteLine($"\nqueue.IsEmpty(): {queue.IsEmpty()}"); //foreach (var item in queue) // Console.WriteLine(item); //Console.WriteLine("LLQueue USAGE----------------\n"); //LLQueue<int> queue2 = new LLQueue<int>(); //queue2.Enqueue(1); //queue2.Enqueue(2); //queue2.Enqueue(3); //queue2.Enqueue(4); //queue2.Enqueue(5); //queue2.Enqueue(6); //queue2.Enqueue(7); //queue2.Enqueue(8); //queue2.Enqueue(9); //Console.WriteLine(); //queue2.PrintElems(); //Console.WriteLine(); //queue2.Dequeue(); //queue2.Dequeue(); //queue2.PrintElems(); //Console.WriteLine(); //queue2.Enqueue(10); //queue2.PrintElems(); //BST Console.WriteLine("Binary Search Tree in ACTION!----------"); BinarySearchTree bst = new BinarySearchTree(); //bst.AddNode(6); //bst.AddNode(3); //bst.AddNode(9); //bst.AddNode(1); //bst.AddNode(8); //bst.AddNode(4); //bst.AddNode(10); ITERATIVE APROACH, BETTER bst.AddNodeRecursive(6); bst.AddNodeRecursive(3); bst.AddNodeRecursive(9); bst.AddNodeRecursive(1); bst.AddNodeRecursive(8); bst.AddNodeRecursive(4); bst.AddNodeRecursive(10); bst.AddNodeRecursive(7); Console.Write("\nbst.ExistNode(4)? -> "); Console.WriteLine(bst.ExistNode(4)); tp.StartTiming(); Console.Write("\nbst.ExistNode(11)? -> "); Console.WriteLine(bst.ExistNode(11)); Console.WriteLine($"TIME TAKEN IN ExistNode() OPERATION: {tp.StopTiming()}"); Console.Write("\nbst.ExistNode(10)? -> "); Console.WriteLine(bst.ExistNode(10)); //USE OF BFS AND DFS ALGORITHMS TO TRAVERSE THE TREE Console.WriteLine("using BFS and DFS ---------------------------"); List <int> bfs = new List <int>(); bst.TraverseDFSPreorder(bfs, bst.Root); foreach (var item in bfs) { Console.WriteLine(item); } Console.WriteLine(); bfs.Clear(); bst.TraverseDFSPreorderIterative(bfs); foreach (var item in bfs) { Console.WriteLine(item); } Console.WriteLine("\nGiven a BT, return the sum of all the depths for all the nodes in it\nSOLUTION"); BinarySearchTree bst2 = new BinarySearchTree(); bst2.AddNode(4); bst2.AddNode(2); bst2.AddNode(6); bst2.AddNode(1); bst2.AddNode(3); bst2.AddNode(7); bst2.AddNode(5); Console.WriteLine(bst2.SumDepths()); BSTNode <int> rootNode = new BSTNode <int>(5); rootNode.LeftRoot = new BSTNode <int>(3); rootNode.RightRoot = new BSTNode <int>(8); rootNode.RightRoot.LeftRoot = new BSTNode <int>(7); rootNode.RightRoot.RightRoot = new BSTNode <int>(9); rootNode.LeftRoot.RightRoot = new BSTNode <int>(4); rootNode.LeftRoot.LeftRoot = new BSTNode <int>(2); Console.WriteLine($"\nIs Binary Search Tree the following tree?: {bst2.IsBST(rootNode)}"); Console.WriteLine($"\nBinarySearchTreee.GetPredecessor(4): {bst2.GetPredecessor(4).Value}"); Console.WriteLine($"\nBinarySearchTreee.GetSucessor(3): {bst2.GetSuccesor(3).Value}"); Console.WriteLine("\nBinarySearchTree.DeleteNode(6)"); bst2.DeleteNode(bst2.Root, 6); bst2.AddNode(6); Console.WriteLine("\nBinarySearchTree.DeleteNodeMCD(6)"); bst2.DeleteNodeMCD(bst2.Root, 6); bst2.AddNode(6); //Graphs Console.WriteLine("Graphs---------------------------"); EdgeListGraph <int> undirectedGraph = new EdgeListGraph <int>(); undirectedGraph.vertices = new int[] { 1, 2, 3, 4 }; undirectedGraph.edges = new Edge[] { new Edge(0, 1), new Edge(0, 3), new Edge(2, 1), new Edge(2, 3) }; Console.WriteLine("undirectedGraph.GetAdyacentNodes(4): "); undirectedGraph.GetAdyacentNodes(4); //Heaps Console.WriteLine("Min Heap ds-----------"); MinHeap minHeap = new MinHeap(10); minHeap.Insert(7); minHeap.Insert(3); minHeap.Insert(6); minHeap.Remove(); minHeap.Insert(1); minHeap.Insert(2); minHeap.Remove(); minHeap.Insert(5); minHeap.Insert(4); foreach (var item in minHeap) { Console.WriteLine(item); } Console.WriteLine("-Max Heap--------------------------------"); MaxHeap maxHeap = new MaxHeap(10); maxHeap.Insert(2); maxHeap.Insert(1); maxHeap.Insert(4); maxHeap.Remove(); maxHeap.Insert(2); maxHeap.Insert(1); maxHeap.Insert(9); maxHeap.Remove(); Console.WriteLine(maxHeap.Peek() == 2); foreach (var item in maxHeap) { Console.WriteLine(item); } }
private static void ConcurrentCollectionsExample() { CDictionary stock = new CDictionary(); int stockValue; //Try add var result = stock.TryAdd("Simpsons", 5); Console.WriteLine($"Tried to add 5 Simpsons tshirts with result:{result}"); Console.WriteLine($"Add the same tshirt again"); result = stock.TryAdd("Simpsons", 2); Console.WriteLine($"Tried to add 2 Simpsons tshirts with result:{result}"); //Try Update result = stock.TryUpdate("Simpsons", 5, 6); Console.WriteLine($"Tried to update Simpsons tshirts (5 of stock), with a new value of 6 with result {result}"); result = stock.TryUpdate("Simpsons", 7, 6); Console.WriteLine($"Tried to update Simpsons tshirts (5 of stock) sending 7 instead, with a new value of 6 with result {result}"); //TryGet result = stock.TryGet("Simpsons", out stockValue); Console.WriteLine($"Get value for simpsons occured with result {result} and the value is {stockValue}"); //Add or Update -> try to update the value, if key it's not present add it var updatedStock = stock.AddOrUpdate("Simpsons", 3); Console.WriteLine($"Tried to increment Simpsons tshirts (5 of stock) with a new value of {updatedStock}"); updatedStock = stock.AddOrUpdate("Family Guy", 10); Console.WriteLine($"Tried to increment Simpsons tshirts (5 of stock) with a new value of {updatedStock}"); //Get or Add -> try and get the value. if key not present add it stockValue = stock.GetOrAdd("Family Guy", 0); Console.WriteLine($"Get the value of {stockValue} for tshirt Family Guy"); //Try Remove result = stock.TryRemove("Simpsons", out stockValue); Console.WriteLine($"Tried to remove remove Simpsons tshirts wich had {stockValue} of stock with result {result}"); result = stock.TryRemove("Simpsons", out stockValue); Console.WriteLine($"Tried to remove remove Simpsons tshirts wich had {stockValue} of stock with result {result}"); //Concurrent Queue CQueue queue = new CQueue(); queue.Enqueue("Simpsons"); queue.Enqueue("FamilyGuy"); queue.Enqueue("StarWars"); string shirt; result = queue.TryPeek(out shirt); if (result) { Console.WriteLine($"The item at the front of the queue is {shirt}"); } else { Console.WriteLine($"Error peeking. Pervert?"); } result = queue.TryDequeue(out shirt); if (result) { Console.WriteLine($"The item dequeued is {shirt}"); } else { Console.WriteLine($"Error peeking. Pervert?"); } result = queue.TryPeek(out shirt); if (result) { Console.WriteLine($"The item at the front of the queue (after dequeue) is {shirt}"); } else { Console.WriteLine($"Error peeking. Pervert?"); } //Concurrent Stack CStack stack = new CStack(); stack.Push("Simpsons"); stack.Push("FamilyGuy"); stack.Push("StarWars"); result = stack.TryPeek(out shirt); if (result) { Console.WriteLine($"The item at the Top of the stack is {shirt}"); } else { Console.WriteLine($"Error peeking. Pervert?"); } result = stack.TryPop(out shirt); if (result) { Console.WriteLine($"The item poped is {shirt}"); } else { Console.WriteLine($"Error peeking. Pervert?"); } result = stack.TryPeek(out shirt); if (result) { Console.WriteLine($"The item at the Top of the stack (after Pop) is {shirt}"); } else { Console.WriteLine($"Error peeking. Pervert?"); } //ConcurrentBag No garanties for the order of put and take. In single thread it tends to have the same beahaviour of stack. It is //used if the same thread does multiple add and remove. in that case it is more efficient. CBag bag = new CBag(); bag.Push("Simpsons"); bag.Push("FamilyGuy"); bag.Push("StarWars"); result = bag.TryPeek(out shirt); if (result) { Console.WriteLine($"The item peeked is {shirt}"); } else { Console.WriteLine($"Error peeking. Pervert?"); } result = bag.TryPop(out shirt); if (result) { Console.WriteLine($"The item poped is {shirt}"); } else { Console.WriteLine($"Error peeking. Pervert?"); } result = bag.TryPeek(out shirt); if (result) { Console.WriteLine($"The item at the Top of the stack (after Pop) is {shirt}"); } else { Console.WriteLine($"Error peeking. Pervert?"); } }