From 081f1c966fa4283cfb09f7171c63e13cf97d643b Mon Sep 17 00:00:00 2001 From: Henrik Christensen Date: Tue, 19 Oct 2021 12:21:27 +0200 Subject: [PATCH 1/3] Stack and Queue implementation in Crystal --- .../stacks_and_queues/code/crystal/queue.cr | 53 +++++++++++++++++++ .../stacks_and_queues/code/crystal/stack.cr | 53 +++++++++++++++++++ .../stacks_and_queues/stacks_and_queues.md | 4 ++ 3 files changed, 110 insertions(+) create mode 100644 contents/stacks_and_queues/code/crystal/queue.cr create mode 100644 contents/stacks_and_queues/code/crystal/stack.cr diff --git a/contents/stacks_and_queues/code/crystal/queue.cr b/contents/stacks_and_queues/code/crystal/queue.cr new file mode 100644 index 000000000..c0a5241ad --- /dev/null +++ b/contents/stacks_and_queues/code/crystal/queue.cr @@ -0,0 +1,53 @@ +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. + def enqueue(item : T) + @queue << item + 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 + @queue.size + end + + # Returns `true` if the queue is empty, `false` otherwise. + def empty? + @queue.empty? + end +end + +def queue_example + queue = Queue(Int32).new + + queue.enqueue(1) + queue.enqueue(2) + queue.enqueue(3) + + puts "#{queue.size} items in the queue" + + puts "#{queue.dequeue} removed from the queue." + puts "Front item in the queue: #{queue.front}" + puts "#{queue.dequeue} removed from the queue." + puts "#{queue.dequeue} removed from the queue." + + puts "The queue is empty" if queue.empty? +end + +queue_example \ No newline at end of file diff --git a/contents/stacks_and_queues/code/crystal/stack.cr b/contents/stacks_and_queues/code/crystal/stack.cr new file mode 100644 index 000000000..5140b3b74 --- /dev/null +++ b/contents/stacks_and_queues/code/crystal/stack.cr @@ -0,0 +1,53 @@ +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. + def push(item : T) + @stack << item + end + + # Remove the last item from the stack. + def pop() : T + @stack.pop + end + + # Returns the number of items in the stack. + def size + @stack.size + end + + # Returns the last item push onto the stack. + def top : T + @stack[-1] + end + + # Returns `true` if the stack is empty, `false` otherwise. + def empty? : Bool + @stack.empty? + end +end + +def stack_example + stack = Stack(Int32).new + + stack.push(1) + stack.push(2) + stack.push(3) + + puts "#{stack.size} items in the stack" + + puts "#{stack.pop} popped from the stack." + puts "Top item in the stack: #{stack.top}" + puts "#{stack.pop} popped from the stack." + puts "#{stack.pop} popped from the stack." + + puts "The stack is empty" if stack.empty? +end + +stack_example \ No newline at end of file diff --git a/contents/stacks_and_queues/stacks_and_queues.md b/contents/stacks_and_queues/stacks_and_queues.md index 11c7088f7..691c64ce9 100644 --- a/contents/stacks_and_queues/stacks_and_queues.md +++ b/contents/stacks_and_queues/stacks_and_queues.md @@ -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 %} From 33dc41bffd5510db9f26ae2071e5d66088510a25 Mon Sep 17 00:00:00 2001 From: Henrik Christensen Date: Tue, 19 Oct 2021 12:43:15 +0200 Subject: [PATCH 2/3] added missing return types --- contents/stacks_and_queues/code/crystal/queue.cr | 4 ++-- contents/stacks_and_queues/code/crystal/stack.cr | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/contents/stacks_and_queues/code/crystal/queue.cr b/contents/stacks_and_queues/code/crystal/queue.cr index c0a5241ad..32961f5e5 100644 --- a/contents/stacks_and_queues/code/crystal/queue.cr +++ b/contents/stacks_and_queues/code/crystal/queue.cr @@ -23,12 +23,12 @@ class Queue(T) end # Returns the number of items in the queue. - def size + def size : Int32 @queue.size end # Returns `true` if the queue is empty, `false` otherwise. - def empty? + def empty? : Bool @queue.empty? end end diff --git a/contents/stacks_and_queues/code/crystal/stack.cr b/contents/stacks_and_queues/code/crystal/stack.cr index 5140b3b74..9112b0952 100644 --- a/contents/stacks_and_queues/code/crystal/stack.cr +++ b/contents/stacks_and_queues/code/crystal/stack.cr @@ -18,7 +18,7 @@ class Stack(T) end # Returns the number of items in the stack. - def size + def size : Int32 @stack.size end From e82807b605817609f82ac6f89b017ab467c69a3a Mon Sep 17 00:00:00 2001 From: Henrik Christensen Date: Sat, 23 Oct 2021 12:59:22 +0200 Subject: [PATCH 3/3] updated examples to match typescript examples --- .../stacks_and_queues/code/crystal/queue.cr | 27 +++++++------------ .../stacks_and_queues/code/crystal/stack.cr | 27 +++++++------------ 2 files changed, 18 insertions(+), 36 deletions(-) diff --git a/contents/stacks_and_queues/code/crystal/queue.cr b/contents/stacks_and_queues/code/crystal/queue.cr index 32961f5e5..9f0a0b55b 100644 --- a/contents/stacks_and_queues/code/crystal/queue.cr +++ b/contents/stacks_and_queues/code/crystal/queue.cr @@ -7,9 +7,10 @@ class Queue(T) @queue = Array(T).new end - # Pushes the given *item* onto the queue. + # 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). @@ -26,28 +27,18 @@ class Queue(T) def size : Int32 @queue.size end - - # Returns `true` if the queue is empty, `false` otherwise. - def empty? : Bool - @queue.empty? - end end def queue_example queue = Queue(Int32).new - queue.enqueue(1) - queue.enqueue(2) - queue.enqueue(3) - - puts "#{queue.size} items in the queue" - - puts "#{queue.dequeue} removed from the queue." - puts "Front item in the queue: #{queue.front}" - puts "#{queue.dequeue} removed from the queue." - puts "#{queue.dequeue} removed from the queue." + queue.enqueue(4) + queue.enqueue(5) + queue.enqueue(9) - puts "The queue is empty" if queue.empty? + puts queue.dequeue + puts queue.size + puts queue.front end -queue_example \ No newline at end of file +queue_example diff --git a/contents/stacks_and_queues/code/crystal/stack.cr b/contents/stacks_and_queues/code/crystal/stack.cr index 9112b0952..aaff55ea2 100644 --- a/contents/stacks_and_queues/code/crystal/stack.cr +++ b/contents/stacks_and_queues/code/crystal/stack.cr @@ -7,9 +7,10 @@ class Stack(T) @stack = Array(T).new end - # Pushes the given *item* onto the stack. + # 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. @@ -26,28 +27,18 @@ class Stack(T) def top : T @stack[-1] end - - # Returns `true` if the stack is empty, `false` otherwise. - def empty? : Bool - @stack.empty? - end end def stack_example stack = Stack(Int32).new - stack.push(1) - stack.push(2) - stack.push(3) - - puts "#{stack.size} items in the stack" - - puts "#{stack.pop} popped from the stack." - puts "Top item in the stack: #{stack.top}" - puts "#{stack.pop} popped from the stack." - puts "#{stack.pop} popped from the stack." + stack.push(4) + stack.push(5) + stack.push(9) - puts "The stack is empty" if stack.empty? + puts stack.pop + puts stack.size + puts stack.top end -stack_example \ No newline at end of file +stack_example