-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.py
executable file
·186 lines (173 loc) · 5.43 KB
/
hello.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/python
"idk lol"
class Node:
'Node linked lists!'
def __init__(self, value):
self.value = value
self.next = None
def displayValue(self):
print self.value
class linkedListForward:
'linked List!'
first = None
last = None
current = None
nodeCount = 0
def __init__(self):
'thanks'
def printAll(self):
"Print all the Nodes"
if self.first is None:
print "There are no Nodes in the list\n"
else:
print "[" + str(self.first.value) + "]",
current = self.first
index = 1
while current.next is not None:
print "->",
index += 1
print "[" + str(current.next.value) + "]",
current = current.next
print ''
def printFL(self):
"Print the first and last"
if self.first != None:
print "first:"
print self.first.value
else:
print "There are no Nodes in the list\n"
return 1
if self.last != None:
print "last:"
print self.last.value
else:
print "There are no Nodes in the list\n"
return 1
def addNode(self, node):
"Add that node!"
if self.first is None:
self.first = node
self.nodeCount = 1
self.last = self.first
else:
self.last.next = node
self.last = node
self.nodeCount += 1
def printNodeCount(self):
"Print the node amount!"
print "Node Count:"
print self.nodeCount
def deleteNode(self, index):
"Delete that node!"
i = 1
current = self.first
previous = None
print index
if self.nodeCount == 1 and index == 1:
self.first = None
print "Successfully Deleted"
self.nodeCount -= 1
self.printAll()
return
elif self.nodeCount < index:
print "Invalid Index nodeCount < index"
return
elif index <= 0:
print "Invalid Index"
return
while current.next is not None and i < index:
temp = current.next
previous = current
current = temp
i += 1
if previous != None and current.next != None:
previous.next = current.next
print "Successfully Deleted"
self.nodeCount -= 1
self.printAll()
elif previous is None and current.next != None:
self.first = current.next
print "Successfully Deleted"
self.nodeCount -= 1
current = None
self.printAll()
elif previous != None and current.next is None:
previous.next = None
print "Successfully Deleted"
self.nodeCount -= 1
self.printAll()
def swap(self, index1, index2):
"Swappin"
if index1 < 0 or index1 > self.nodeCount:
print 'Invalid index 1'
return
if index2 < 0 or index2 > self.nodeCount:
print 'Invalid index 2'
return
current1 = self.first
current2 = self.first
for x in range(1, index1):
temp1 = current1.next
previous1 = current1
current1 = temp1
for y in range(1, index2):
temp2 = current2.next
previous2 = current2
current2 = temp2
previous1.next = current2
current2.next = current1.next
previous2.next = current1
current1.next = current2.next
def main():
"run it breh lol"
command = ''
print "--------------------------------------\n"
print "Welcome to your own linked list!"
print 'Commands:\n -quit\n -print all\n -print last\n -print fl\n -test\n -delete\n -add\n'
print "--------------------------------------"
ll = linkedListForward()
while True:
command = raw_input("Please enter an Input: ")
if command == "quit" or command == 'q' or command == "Quit":
break
elif command == "add":
print "\nWhat value do you want to add?\n"
value = raw_input("Value:")
node = Node(value)
ll.addNode(node)
print "Successfully Added!\n"
elif command == "print all":
print ''
ll.printAll()
print ''
elif command == "node count":
print ''
ll.printNodeCount()
print ''
elif command == "print fl":
print ''
ll.printFL()
print ''
elif command == 'test':
value = int(raw_input("Amount of nodes?:"))
for x in range(1, value+1):
node = Node(x)
ll.addNode(node)
print '\nAdded test nodes\n'
ll.printAll()
print ''
elif command == 'commands':
print '\nCommands:\n -quit\n -print all\n -print last\n -print fl\n -test\n -delete\n -add\n'
elif command == "delete":
print ''
index = int(raw_input("index:"))
ll.deleteNode(index)
print ''
elif command == 'swap':
index1 = int(raw_input("First Index: "))
index2 = int(raw_input("second Index: "))
ll.swap(index1, index2)
else:
print "Invalid Command"
print "left"
main()