Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented stack and queue in C# #990

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 80 additions & 0 deletions contents/stacks_and_queues/code/csharp/Queue.cs
Original file line number Diff line number Diff line change
@@ -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());

}
}
}
80 changes: 80 additions & 0 deletions contents/stacks_and_queues/code/csharp/Stack.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
4 changes: 4 additions & 0 deletions contents/stacks_and_queues/stacks_and_queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 %}


Expand Down