-
-
Notifications
You must be signed in to change notification settings - Fork 128
Add Ruby Language #139
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
Merged
Merged
Add Ruby Language #139
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
85ff2d8
Create Ruby programming language
ACR1209 f1291ff
Added ruby array manipulation category and some snippets
ACR1209 92c408c
Add Ruby string manipulation snippets
ACR1209 993a50a
Add Ruby snippet for calculating compound interest
ACR1209 43f49a1
Add Ruby Sieve of Sundaram snippet
ACR1209 3807b5c
Add Ruby snippet for checking prime numbers
ACR1209 ec3ee2b
Add Ruby snippet for calculating factorial
ACR1209 cdbd348
Add Ruby snippet for defining and raising a custom error class
ACR1209 88fcb81
Add Ruby snippet for implementing a basic binary tree with in-order t...
ACR1209 d6364c6
Add Ruby snippet for implementing a singly linked list with node inse...
ACR1209 9f15bd9
Add Ruby snippet for implementing a doubly linked list with node inse...
ACR1209 108ca7b
Update consolidated snippets
actions-user 5caa33c
Fix to fit new guidelines for snippets
ACR1209 461ae2d
Update consolidated snippets
actions-user 28703fa
Apply feedback to align with contributing guidelines and add new snip...
ACR1209 0c2e2b3
Update consolidated snippets
actions-user e40c812
Rename function to transform from snake_case to PascalCase in Ruby sn...
ACR1209 bebe1c7
Update consolidated snippets
actions-user 0c87a21
Merge branch 'main' into ruby-language
ACR1209 0ac16f0
Update consolidated snippets
actions-user File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add Ruby snippet for implementing a doubly linked list with node inse...
...rtion and traversal
- Loading branch information
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
snippets/ruby/data-structures/doubly-linked-list.md
ACR1209 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ACR1209 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- | ||
title: Doubly Linked List | ||
description: Implements a doubly linked list with node insertion and traversal. | ||
author: ACR1209 | ||
tags: ruby,data structures,linked list,doubly linked list | ||
--- | ||
|
||
```rb | ||
class Node | ||
attr_accessor :data, :next, :prev | ||
|
||
def initialize(data) | ||
@data = data | ||
@next = nil | ||
@prev = nil | ||
end | ||
end | ||
|
||
class DoublyLinkedList | ||
def initialize | ||
@head = nil | ||
@tail = nil | ||
end | ||
|
||
def append(data) | ||
new_node = Node.new(data) | ||
if @head.nil? | ||
@head = new_node | ||
@tail = new_node | ||
else | ||
@tail.next = new_node | ||
new_node.prev = @tail | ||
@tail = new_node | ||
end | ||
end | ||
|
||
def prepend(data) | ||
new_node = Node.new(data) | ||
if @head.nil? | ||
@head = new_node | ||
@tail = new_node | ||
else | ||
new_node.next = @head | ||
@head.prev = new_node | ||
@head = new_node | ||
end | ||
end | ||
|
||
def display_forward | ||
current = @head | ||
while current | ||
print "#{current.data} <-> " | ||
current = current.next | ||
end | ||
puts "nil" | ||
end | ||
|
||
def display_backward | ||
current = @tail | ||
while current | ||
print "#{current.data} <-> " | ||
current = current.prev | ||
end | ||
puts "nil" | ||
end | ||
end | ||
|
||
# Usage: | ||
dll = DoublyLinkedList.new | ||
dll.append(1) | ||
dll.append(2) | ||
dll.append(3) | ||
dll.display_forward # Output: 1 <-> 2 <-> 3 <-> nil | ||
dll.display_backward # Output: 3 <-> 2 <-> 1 <-> nil | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.