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

Stack and Queue implementation in Crystal #886

Open
wants to merge 3 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions contents/stacks_and_queues/code/crystal/queue.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Queue(T)
# The items in the queue.
@queue : Array(T)

# Creates a new empty queue.
def initialize
@queue = Array(T).new
end

# Pushes the given *item* onto the queue and returns the size of the queue.
def enqueue(item : T)
@queue << item
self.size
end

# Removes the first item in the queue (at index 0).
def dequeue : T
@queue.shift
end

# Returns the first item in the queue (at index 0).
def front : T
@queue[0]
end

# Returns the number of items in the queue.
def size : Int32
@queue.size
end
end

def queue_example
queue = Queue(Int32).new

queue.enqueue(4)
queue.enqueue(5)
queue.enqueue(9)

puts queue.dequeue
puts queue.size
puts queue.front
end

queue_example
44 changes: 44 additions & 0 deletions contents/stacks_and_queues/code/crystal/stack.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Stack(T)
# The items in the stack.
@stack : Array(T)

# Creates a new empty stack.
def initialize
@stack = Array(T).new
end

# Pushes the given *item* onto the stack and returns the size of the stack.
def push(item : T)
@stack << item
self.size
end

# Remove the last item from the stack.
def pop() : T
@stack.pop
end

# Returns the number of items in the stack.
def size : Int32
@stack.size
end

# Returns the last item push onto the stack.
def top : T
@stack[-1]
end
end

def stack_example
stack = Stack(Int32).new

stack.push(4)
stack.push(5)
stack.push(9)

puts stack.pop
puts stack.size
puts stack.top
end

stack_example
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 @@ -16,12 +16,16 @@ The notation for this depends on the language you are using. Queues, for example
## Example Code
Here is a simple implementation of a stack:
{% method %}
{% sample lang="crystal" %}
[import, lang:"crystal"](code/crystal/stack.cr)
{% sample lang="ts" %}
[import, lang:"typescript"](code/typescript/stack.ts)
{% endmethod %}

Here is a simple implementation of a queue:
{% method %}
{% sample lang="crystal" %}
[import, lang:"crystal"](code/crystal/queue.cr)
{% sample lang="ts" %}
[import, lang:"typescript"](code/typescript/queue.ts)
{% endmethod %}
Expand Down