From 0b23b06fb47082545c4cd0e3574f539c393b7381 Mon Sep 17 00:00:00 2001 From: hwat212 Date: Sun, 30 Jan 2022 22:59:19 -0500 Subject: [PATCH] Implemented stack and queue in C# --- .../stacks_and_queues/code/csharp/Queue.cs | 80 +++++++++++++++++++ .../stacks_and_queues/code/csharp/Stack.cs | 80 +++++++++++++++++++ .../stacks_and_queues/stacks_and_queues.md | 4 + 3 files changed, 164 insertions(+) create mode 100644 contents/stacks_and_queues/code/csharp/Queue.cs create mode 100644 contents/stacks_and_queues/code/csharp/Stack.cs diff --git a/contents/stacks_and_queues/code/csharp/Queue.cs b/contents/stacks_and_queues/code/csharp/Queue.cs new file mode 100644 index 000000000..eec7f1372 --- /dev/null +++ b/contents/stacks_and_queues/code/csharp/Queue.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; + +namespace my { + /** + * implementation using linked list + * [value][next] -> [value][next] -> ... -> [value][next] + * (top Node) (intermediat Nodes) + * left most Node represents top element of queue + */ + public class Node { + public Node next; + public object data; + } + public class Queue { + private Node head; + private Node tail; + public int size; + + public Queue() { + head = new Node(); + tail = head; + size = 0; + } + public void Enqueue(object value) { + Node newNode = new Node(); + newNode.data = value; + tail.next = newNode; + tail = newNode; + size++; + } + + public void Dequeue() { + if (size != 0) { + head.next = head.next.next; + size--; + } + else { + Console.WriteLine("No element to remove."); + } + } + + public object Front() { + return head.next.data; + } + + public int Size() { + return size; + } + + public bool Empty() { + return size == 0; + } + + } + + class Test { + static void Main(string[] args) { + Console.WriteLine("Queue:"); + Queue intQueue = new Queue(); + + intQueue.Enqueue(4); + intQueue.Enqueue(5); + intQueue.Enqueue(9); + + Console.Write("Size: "); + Console.WriteLine(intQueue.Size()); + Console.Write("Front: "); + Console.WriteLine(intQueue.Front()); + + intQueue.Dequeue(); + + Console.Write("Size: "); + Console.WriteLine(intQueue.Size()); + Console.Write("Front: "); + Console.WriteLine(intQueue.Front()); + + } + } +} diff --git a/contents/stacks_and_queues/code/csharp/Stack.cs b/contents/stacks_and_queues/code/csharp/Stack.cs new file mode 100644 index 000000000..c442ba431 --- /dev/null +++ b/contents/stacks_and_queues/code/csharp/Stack.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; + +namespace my +{ + /** + * implementation using linked list + * [value][next] -> [value][next] -> ... -> [value][next] + * (top Node) (intermediat Nodes) + * left most Node represents top element of stack + */ + public class Node { + public Node next; + public object data; + } + + public class Stack { + private Node head; + public int size; + + public Stack() { + head = new Node(); + size = 0; + } + public void Push(object value) { + Node newNode = new Node(); + newNode.data = value; + newNode.next = head.next; + head.next = newNode; + size++; + } + + public void Pop() { + if (size != 0) { + head.next = head.next.next; + size--; + } + else { + Console.WriteLine("No element to remove."); + } + } + + public object Top() { + return head.next.data; + } + + public int Size() { + return size; + } + + public bool Empty() { + return size == 0; + } + + } + + class Test { + static void Main(string[] args) { + + Console.WriteLine("Stack:"); + Stack intStack = new Stack(); + + intStack.Push(4); + intStack.Push(5); + intStack.Push(9); + + Console.Write("Size: "); + Console.WriteLine(intStack.Size()); + Console.Write("Top: "); + Console.WriteLine(intStack.Top()); + + intStack.Pop(); + + Console.Write("Size: "); + Console.WriteLine(intStack.Size()); + Console.Write("Top: "); + Console.WriteLine(intStack.Top()); + } + } +} diff --git a/contents/stacks_and_queues/stacks_and_queues.md b/contents/stacks_and_queues/stacks_and_queues.md index b695f53bd..16ea40539 100644 --- a/contents/stacks_and_queues/stacks_and_queues.md +++ b/contents/stacks_and_queues/stacks_and_queues.md @@ -24,6 +24,8 @@ Here is a simple implementation of a stack: [import, lang:"cpp"](code/cpp/stack.cpp) {% sample lang="rust" %} [import, lang:"rust"](code/rust/Stack.rs) +{% sample lang="csharp" %} +[import, lang:"csharp"](code/csharp/Stack.cs) {% endmethod %} Here is a simple implementation of a queue: @@ -36,6 +38,8 @@ Here is a simple implementation of a queue: [import, lang:"cpp"](code/cpp/queue.cpp) {% sample lang="rust" %} [import, lang:"rust" ](code/rust/Queue.rs) +{% sample lang="csharp" %} +[import, lang:"csharp"](code/csharp/Queue.cs) {% endmethod %}