<repo_name>timkellogg/data_structures<file_sep>/spec/data_structures/stack_spec.rb
RSpec.describe DataStructures::Stack do
  subject { described_class.new }

  describe "#pop" do
    it "removes the first element from the stack" do
      subject.push 1
      subject.pop
      
      expect(subject.size).to eq 0
    end
  end

  describe "#push" do
    it "adds a new element to the top of the stack" do
      subject.push 1

      expect(subject.size).to eq 1

      subject.push 2

      expect(subject.elements).to eq [2, 1]
    end
  end

  describe "#reverse" do
    it "returns a copy of the array that's in the opposite order" do
      expect(subject.reverse).to eq subject.elements

      subject.push 1
      subject.push 2

      expect(subject.reverse).to eq [1, 2]
    end
  end

  describe "#size" do
    it "returns the number of elements in the stack" do
      expect(subject.size).to eq 0

      subject.push 1
      subject.push 2

      expect(subject.size).to eq 2
    end
  end

  describe "#empty?" do
    it "returns a boolean for whether there are any elements in the stack" do
      expect(subject.empty?).to eq true

      subject.push 1

      expect(subject.empty?).to eq false
    end
  end
end
<file_sep>/lib/data_structures/node.rb
module DataStructures
  class Node
    attr_accessor :value, :child

    def initialize(value, child: nil)
      @value = value
      @child = child
    end
  end
end
<file_sep>/spec/data_structures/linked_list_spec.rb
RSpec.describe DataStructures::LinkedList do
  subject { described_class.new }

  describe "#initialize" do
    it "creates initial head" do
      expect(described_class.method_defined?(:head)).to eq true
    end
  end

  describe "#prepend" do
    it "sets the new head equal to the prepending node" do
      subject.prepend 1
      expect(subject.values).to eq [1]

      subject.prepend 2
      subject.prepend 3

      expect(subject.values).to eq [3, 2, 1]
    end
  end

  describe "#find" do
    it "returns the node by value" do
      expect(subject.find(1)).to eq nil

      subject.prepend 2

      expect(subject.find(5)).to eq nil

      subject.prepend 3

      expect(subject.find(3).class).to eq DataStructures::Node
      expect(subject.find(3).value).to eq 3
    end
  end

  describe "#append" do
    it "adds a node on to the last of the linked list" do
      subject.prepend 1
      subject.prepend 2
      subject.append 10
      subject.append 19

      expect(subject.values).to eq [2, 1, 10, 19]
    end
  end

  describe "#delete_head" do
    it "removes the current head and sets the new head as the child if it exists" do
      subject.prepend 1
      subject.prepend 2
      subject.prepend 3
      subject.delete_head

      expect(subject.head.value).to eq 2
    end
  end

  describe "#delete_tail" do
    it "removes the last element" do
      subject.prepend 1
      subject.delete_tail

      expect(subject.values).to eq []

      subject.prepend 1
      subject.prepend 2
      subject.prepend 3
      subject.delete_tail

      expect(subject.values).to eq [3, 2]
    end
  end

  describe "#append_after" do
    it "adds the element as a child of the element by the same value" do
      subject.prepend 1
      subject.prepend 2
      subject.prepend 4
      subject.append_after 4, 3

      expect(subject.values).to eq [4, 3, 2, 1]
    end
  end

  describe "#append_before" do
    it "adds the element as a parent of the element by the same value" do
      subject.prepend 1
      subject.append_before 1, 0

      expect(subject.values).to eq [0, 1]

      subject.prepend 2
      subject.prepend 4
      subject.append_before 2, 3

      expect(subject.values).to eq [4, 3, 2, 0, 1]
    end
  end
end
<file_sep>/lib/data_structures/linked_list.rb
module DataStructures
  class LinkedList
    attr_reader :head

    def initialize
      @head = nil
    end

    def prepend(value)
      unless head
        @head = Node.new value
        return
      end

      current_head = head
      @head = Node.new value, child: current_head
    end

    def append(value)
      return unless head

      node = head
      while true
        unless node.child
          node.child = Node.new value
          return
        end

        node = node.child
      end
    end

    def append_after(current_node_value, value)
      return unless head

      node = find current_node_value
      current_child = node.child
      node.child = Node.new value, child: current_child
    end

    def append_before(current_node_value, value)
      return unless head

      node = head
      unless head.child
        @head = Node.new value, child: node
        return
      end

      while node.child
        if node.child.value = current_node_value
          current_child = node.child
          node.child = Node.new value, child: current_child
          return
        end

        node = node.child
      end
    end

    def delete_head
      return unless head

      @head = head.child
    end

    def delete_tail
      return unless head

      unless head.child
        @head = head.child = nil
        return
      end

      node = head
      while node.child.child
        node = node.child
      end

      node.child = nil
    end

    def find(value)
      return unless head

      node = head
      while node.child
        return node if node.value == value
        node = node.child
      end
    end

    def values
      return [] unless head

      values = [head.value]

      node = head
      while node.child
        values << node.child.value
        node = node.child
      end

      values
    end
  end
end
<file_sep>/spec/data_structures_spec.rb
RSpec.describe DataStructures do
  it "has a version number" do
    expect(DataStructures::VERSION).not_to be nil
  end
end
<file_sep>/lib/data_structures/queue.rb
module DataStructures
  class Queue < List
    def enqueue(element)
      elements << element
    end

    def dequeue
      elements.shift
    end
  end
end
<file_sep>/lib/data_structures/version.rb
module DataStructures
  VERSION = "0.1.0"
end
<file_sep>/lib/data_structures/list.rb
module DataStructures
  class List
    attr_reader :elements

    def initialize
      @elements = []
    end

    def pop
      elements.shift
    end

    def reverse
      elements.reverse
    end

    def size
      elements.count
    end

    def empty?
      !elements.any?
    end
  end
end
<file_sep>/lib/data_structures/stack.rb
module DataStructures
  class Stack < List
    def push(element)
      elements.unshift element
    end
  end
end
<file_sep>/lib/data_structures.rb
require "data_structures/version"
require "data_structures/list"
require "data_structures/queue"
require "data_structures/stack"
require "data_structures/node"
require "data_structures/linked_list"

module DataStructures
  
end
