-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.rb
147 lines (126 loc) · 3.04 KB
/
linkedlist.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require 'pry'
class LinkedListNode
attr_accessor :value, :next_node
def initialize(value, next_node=nil)
@value = value
@next_node = next_node # LIFO
end
#
# Reverse the list with using stack
#
def self.reverse_list1(list)
stack = Stack.new
new_list = nil
while !list.nil?
value = stack.push(list.value)
list = list.next_node
end
# This linkedlist is FILO, with regard to the "head" node (list)
# The stack is LIFO, stored with the same linkedList design FILO.
# So the internal linkedlist of the stack is already in reserve order.
new_list = stack.get_linkedlist
return new_list
end
#
# Reverse the list by switching the direction of next_node
#
def self.reverse_list2(list, previous=nil)
return nil if list.nil?
if list.next_node.nil?
list.next_node = previous
head = list
return head
else
head = reverse_list2(list.next_node, list)
if previous.nil?
list.next_node = nil
else
list.next_node = previous
end
end
return head
end
# work in progress
def self.append_node(list, stack)
if !stack.nil?
value = stack.pop
LinkedListNode.new(value, list)
append_node(list, stack)
end
end
def self.print_node(note, node)
if node.nil?
text = note + ", node=nil"
puts text
return
end
text = note + ", value=" + node.value.to_s
if node.next_node.nil?
text = text + ", next=nil"
else
text = text + ", next=" + node.next_node.value.to_s
end
puts text
end
def self.print_values(list)
if !list.nil?
print "#{list.value} --> "
print_values(list.next_node)
else
print "nil\n"
return
end
end
end
class Stack
attr_reader :data
def initialize
@data = nil
end
# Push a value onto the stack
def push(value)
if @data.nil?
@data = LinkedListNode.new(value)
else
@data = LinkedListNode.new(value, @data)
end
end
def pop
if !@data.nil?
value = @data.value
@data = @data.next_node
return value
end
end
def get_linkedlist
return @data
end
def print_values
puts "print stack as linked list"
LinkedListNode.print_values(@data)
end
end
puts "\n===== LinkedListNode new"
node1 = LinkedListNode.new(37)
node2 = LinkedListNode.new(99, node1)
node3 = LinkedListNode.new(12, node2)
LinkedListNode.print_values(node1)
LinkedListNode.print_values(node2)
LinkedListNode.print_values(node3)
puts "\n===== reverse_list1"
LinkedListNode.print_values(node3)
revlist = LinkedListNode.reverse_list1(node3)
LinkedListNode.print_values(revlist)
revlist = LinkedListNode.reverse_list1(revlist)
LinkedListNode.print_values(revlist)
puts "\n===== reverse_list2"
LinkedListNode.print_values(node3)
revlist = LinkedListNode.reverse_list2(node3)
LinkedListNode.print_values(revlist)
revlist = LinkedListNode.reverse_list2(revlist)
LinkedListNode.print_values(revlist)
puts "\n===== Stack push twice: 3, 5"
stack1 = Stack.new
stack1.push(3)
stack1.push(5)
puts "pop twice: #{stack1.pop}, #{stack1.pop}"