-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaseminsucu_csc219_hw5_linkedlists.py
218 lines (174 loc) · 6.45 KB
/
yaseminsucu_csc219_hw5_linkedlists.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# -*- coding: utf-8 -*-
"""YaseminSucu_CSC219_HW5_LinkedLists.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ZuZy-G0QEwLkXa2mpcLErODrDn1uU9WP
#**Homework on Linked Lists and OOP**
We have defined two classes, "Node" and "LinkedList". Class LinkedList will have a listprint method within it that will print the list of months.
"""
class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None
class LinkedList:
def __init__(self):
self.headval = None
def listprint(self):
temp = self.headval
while (temp):
print(temp.dataval,end=" ")
temp = temp.nextval
"""1. Create a list called 'months' by instantiating the LinkedList class."""
#enter your answer below
# Instantiate the LinkedList class to create the 'months' list
months = LinkedList()
# Add nodes to the 'months' list
months.headval = Node("January")
months.headval.nextval = Node("February")
months.headval.nextval.nextval = Node("March")
months.headval.nextval.nextval.nextval = Node("April")
months.headval.nextval.nextval.nextval.nextval = Node("May")
months.headval.nextval.nextval.nextval.nextval.nextval = Node("June")
"""2. Assign the headvalue to be the node "January" by instantiating the Node class"""
#enter your answer below
# Instantiate the Node class to create the head node with data value "January"
head_node = Node("January")
# Instantiate the LinkedList class and set the headval attribute to the head node
months = LinkedList()
months.headval = head_node
"""3. Make 11 more variables labeled 'e2' through 'e12' for the rest of the months, February-December. Do this by instantiating the Node class for each month (expressed as a string), and then assigning that to your variable."""
#enter your answer below
# Instantiate the Node class to create nodes for each month
e1 = Node("January")
e2 = Node("February")
e3 = Node("March")
e4 = Node("April")
e5 = Node("May")
e6 = Node("June")
e7 = Node("July")
e8 = Node("August")
e9 = Node("September")
e10 = Node("October")
e11 = Node("November")
e12 = Node("December")
# Instantiate the LinkedList class and link the nodes together
months = LinkedList()
months.headval = e1
e1.nextval = e2
e2.nextval = e3
e3.nextval = e4
e4.nextval = e5
e5.nextval = e6
e6.nextval = e7
e7.nextval = e8
e8.nextval = e9
e9.nextval = e10
e10.nextval = e11
e11.nextval = e12
# Print the values of the nodes in the linked list
months.listprint()
"""4. Link the first node to the second node using .headval and .nextval. Then link each node to the next node using .nextval and the variable name.
"""
#enter your answer below
# Instantiate the Node class to create nodes for each month
jan = Node("January")
feb = Node("February")
mar = Node("March")
apr = Node("April")
may = Node("May")
jun = Node("June")
jul = Node("July")
aug = Node("August")
sep = Node("September")
oct = Node("October")
nov = Node("November")
dec = Node("December")
# Instantiate the LinkedList class and link the nodes together
months = LinkedList()
months.headval = jan
jan.nextval = feb
feb.nextval = mar
mar.nextval = apr
apr.nextval = may
may.nextval = jun
jun.nextval = jul
jul.nextval = aug
aug.nextval = sep
sep.nextval = oct
oct.nextval = nov
nov.nextval = dec
# Print the values of the nodes in the linked list
months.listprint()
"""5. Use the .listprint() method to print the list of the months. It should print a sequence of months from January through December."""
#enter your answer below
# Instantiate the Node class to create nodes for each month
jan = Node("January")
feb = Node("February")
mar = Node("March")
apr = Node("April")
may = Node("May")
jun = Node("June")
jul = Node("July")
aug = Node("August")
sep = Node("September")
oct = Node("October")
nov = Node("November")
dec = Node("December")
# Instantiate the LinkedList class and link the nodes together
months = LinkedList()
months.headval = jan
jan.nextval = feb
feb.nextval = mar
mar.nextval = apr
apr.nextval = may
may.nextval = jun
jun.nextval = jul
jul.nextval = aug
aug.nextval = sep
sep.nextval = oct
oct.nextval = nov
nov.nextval = dec
# Print the values of the nodes in the linked list
months.listprint()
"""6. Here, we are going to add a method that will append an element to the end of the linked list created by LinkedList. We are going to recreated the list of months by using this method.
Define a method called append() that takes self and new_data as an argument. Add this method to the LinkedList class that creates a new node (new_node) and then instantiates the Node class to place new_data into the new node. Then this method will say "If the linked list is empty, make the new node the headval." If this is not the case (your linked list is not empty, in this example), then use a while loop in your method to traverse to the last node. After that, your method needs to change the next node of the last node to new_node. Refer to your class materials for how to do this.
Because we are redefining our classes, your linked list that you made earlier will be lost. Start it again by using the append() method that you just created, and rebuild your list of months, then print it. You have been provided the classes you need. Add your append() method to the Months class.
"""
#Here are the classes as they are already defined. Add your method to the LinkedList class
class LinkedList:
def __init__(self):
self.headval = None
def listprint(self):
temp = self.headval
while (temp):
print(temp.dataval,end=" ")
temp = temp.nextval
#enter your new method for question 6 here
def append(self, new_data):
new_node = Node(new_data)
if self.headval is None:
self.headval = new_node
return
last_node = self.headval
while last_node.nextval:
last_node = last_node.nextval
last_node.nextval = new_node
"""7. Use your new append() method to append January-December to the list. Refer to class materials. Start by creating a variable called months that instantiates LinkedList(). Finish by using .listprint() to display your full list of months."""
#enter answer below
# Instantiate the linked list
months = LinkedList()
# Append each month to the LinkedList
months.append("January")
months.append("February")
months.append("March")
months.append("April")
months.append("May")
months.append("June")
months.append("July")
months.append("August")
months.append("September")
months.append("October")
months.append("November")
months.append("December")
# Print the list of months
months.listprint()