Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Mathys-Gasnier merged 20 commits into quicksnip-dev:main from ACR1209:ruby-language
Jan 4, 2025
Merged
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 Jan 2, 2025
f1291ff
Added ruby array manipulation category and some snippets
ACR1209 Jan 2, 2025
92c408c
Add Ruby string manipulation snippets
ACR1209 Jan 2, 2025
993a50a
Add Ruby snippet for calculating compound interest
ACR1209 Jan 2, 2025
43f49a1
Add Ruby Sieve of Sundaram snippet
ACR1209 Jan 2, 2025
3807b5c
Add Ruby snippet for checking prime numbers
ACR1209 Jan 2, 2025
ec3ee2b
Add Ruby snippet for calculating factorial
ACR1209 Jan 2, 2025
cdbd348
Add Ruby snippet for defining and raising a custom error class
ACR1209 Jan 2, 2025
88fcb81
Add Ruby snippet for implementing a basic binary tree with in-order t...
ACR1209 Jan 2, 2025
d6364c6
Add Ruby snippet for implementing a singly linked list with node inse...
ACR1209 Jan 2, 2025
9f15bd9
Add Ruby snippet for implementing a doubly linked list with node inse...
ACR1209 Jan 2, 2025
108ca7b
Update consolidated snippets
actions-user Jan 2, 2025
5caa33c
Fix to fit new guidelines for snippets
ACR1209 Jan 2, 2025
461ae2d
Update consolidated snippets
actions-user Jan 2, 2025
28703fa
Apply feedback to align with contributing guidelines and add new snip...
ACR1209 Jan 3, 2025
0c2e2b3
Update consolidated snippets
actions-user Jan 3, 2025
e40c812
Rename function to transform from snake_case to PascalCase in Ruby sn...
ACR1209 Jan 4, 2025
bebe1c7
Update consolidated snippets
actions-user Jan 4, 2025
0c87a21
Merge branch 'main' into ruby-language
ACR1209 Jan 4, 2025
0ac16f0
Update consolidated snippets
actions-user Jan 4, 2025
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
Prev Previous commit
Next Next commit
Add Ruby snippet for implementing a doubly linked list with node inse...
...rtion and traversal
  • Loading branch information
ACR1209 committed Jan 2, 2025
commit 9f15bd9bb01cb3cc4c60a3cb7eb5598c80cf20d5
75 changes: 75 additions & 0 deletions snippets/ruby/data-structures/doubly-linked-list.md
View file Open in desktop
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
```

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /