-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
146 lines (113 loc) · 3.62 KB
/
main.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
require_relative 'lib/linked_list_singly'
require_relative 'lib/linked_list_doubly'
puts "Create and mess around with Linked Lists!\n\n"
list_choice = 0
until list_choice.between?(1,2)
puts "1. Singly linked list"
puts "2. Doubly linked list"
print "Which linked list would you like to create: "
list_choice = gets.chomp.to_i
list = if list_choice == 1
LinkedList::Singly.new
elsif list_choice == 2
LinkedList::Doubly.new
else
puts "Wrong input #{list_choice}, please type 1 or 2...\n\n"
end
end
puts "\nCreated a #{list.class} Linked List!\n\n"
operation = 1
until operation.zero?
operations = [
"Quit/Exit program",
"Print list",
"Add to end of list",
"Add to start of list",
"Return item at certain index",
"Remove from end of list",
"Remove from start of list",
"Check if list contains a value",
"Find index of value",
"Insert item at specific index",
"Remove iitem at specific index",
"Reverse the list",
"Examine node"
]
operations.each_with_index do |operation, index|
puts "#{index}. ".ljust(4) + operation
end
print "\n==> Select one of the above operations [type 0-11]: "
operation = gets.chomp.to_i
puts ""
begin
case operation
when 0
puts "Ending program..."
when 1
puts "Printing list..."
puts list
when 2
puts "Adding to the end of list"
print "Enter one item, or comma separted items: "
user_values = gets.chomp.split(",")
user_values.each { |item| list.append item }
when 3
puts "Adding to the start of list"
print "Enter one item, or comma separted items: "
user_values = gets.chomp.split(",")
user_values.each { |item| list.prepend item }
when 4
puts "Returning an item at certain index"
print "Enter index: "
user_index = gets.chomp.to_i
if list.instance_of? LinkedList::Singly
puts "Item at #{user_index}: #{list.at(user_index)[0].value}"
elsif list.instance_of? LinkedList::Doubly
puts "Item at #{user_index}: #{list.at(user_index).value}"
end
when 5
puts "Removing from the end of list"
puts "Item removed/popped from end of list: #{list.pop.value}"
when 6
puts "Removing from the start of list"
puts "Item removed/shifted from start of list: #{list.shift.value}"
when 7
puts "Checking if list contains a value"
print "Enter value to check: "
user_value = gets.chomp
puts "Checking presence of #{user_value}: #{list.contains(user_value)}"
when 8
puts "Finding an index for a value"
print "Enter value to check: "
user_value = gets.chomp
puts "Index for #{user_value}: #{list.find(user_value)}"
when 9
puts "Inserting an item at specific index"
print "Enter value: "
user_value = gets.chomp
print "Enter index: "
user_index = gets.chomp.to_i
list.insert_at(user_value, user_index)
puts "Inserted #{user_value} at #{user_index}."
when 10
puts "Removing an item at specific index"
print "Enter index: "
user_index = gets.chomp.to_i
puts "Removing item at #{user_index}: #{list.remove_at(user_index).value}"
when 11
puts "Reversing the list"
list.reverse!
puts list
when 12
puts "Examining node"
print "Enter index: "
user_index = gets.chomp.to_i
puts list.examine_node(user_index)
else
puts "Incorrect input #{operation}, type 0-11."
end
rescue StandardError => e
puts e
end
puts "\n------------------------------------------\n\n"
end