forked from surajkarna/INFYTQ-Data-Structures-using-Python-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment on Linked List of Objects - Level 2 .py
166 lines (137 loc) · 4.74 KB
/
Assignment on Linked List of Objects - Level 2 .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
#lex_auth_0127438667235000321614
class Node:
def __init__(self,data):
self.__data=data
self.__next=None
def get_data(self):
return self.__data
def set_data(self,data):
self.__data=data
def get_next(self):
return self.__next
def set_next(self,next_node):
self.__next=next_node
class LinkedList:
def __init__(self):
self.__head=None
self.__tail=None
def get_head(self):
return self.__head
def get_tail(self):
return self.__tail
def add(self,data):
new_node=Node(data)
if(self.__head is None):
self.__head=self.__tail=new_node
else:
self.__tail.set_next(new_node)
self.__tail=new_node
def insert(self,data,data_before):
new_node=Node(data)
if(data_before==None):
new_node.set_next(self.__head)
self.__head=new_node
if(new_node.get_next()==None):
self.__tail=new_node
else:
node_before=self.find_node(data_before)
if(node_before is not None):
new_node.set_next(node_before.get_next())
node_before.set_next(new_node)
if(new_node.get_next() is None):
self.__tail=new_node
else:
print(data_before,"is not present in the Linked list")
def display(self):
temp=self.__head
while(temp is not None):
print(temp.get_data())
temp=temp.get_next()
def find_node(self,data):
temp=self.__head
while(temp is not None):
if(temp.get_data()==data):
return temp
temp=temp.get_next()
return None
def delete(self,data):
node=self.find_node(data)
if(node is not None):
if(node==self.__head):
if(self.__head==self.__tail):
self.__tail=None
self.__head=node.get_next()
else:
temp=self.__head
while(temp is not None):
if(temp.get_next()==node):
temp.set_next(node.get_next())
if(node==self.__tail):
self.__tail=temp
node.set_next(None)
break
temp=temp.get_next()
else:
print(data,"is not present in Linked list")
#You can use the below __str__() to print the elements of the DS object while debugging
def __str__(self):
temp=self.__head
msg=[]
while(temp is not None):
msg.append(str(temp.get_data()))
temp=temp.get_next()
msg=" ".join(msg)
msg="Linkedlist data(Head to Tail): "+ msg
return msg
class Child:
def __init__(self,name,item_to_perform):
self.__name=name
self.__item_to_perform=item_to_perform
def __str__(self):
return(self.__name+" "+self.__item_to_perform)
def get_name(self):
return self.__name
def get_item_to_perform(self):
return self.__item_to_perform
#Implement Performance class here
class Performance:
def __init__(self,children_list):
self.__children_list=children_list
def get_children_list(self):
return self.__children_list
def change_position(self,child):
#function copied from https://github.com/omkar98/InfyTQ-Answers/blob/master/DSA%20in%20Python/Day-2/Assignment%206.txt
temp=self.__children_list.get_head()
temp1=self.__children_list.get_head()
while(temp.get_next()):
temp1=temp1.get_next()
if temp.get_next().get_next():
temp=temp.get_next().get_next()
self.__children_list.delete(child)
self.__children_list.insert(child,temp1.get_data())
def add_new_child(self,child):
self.__children_list.add(child)
child1=Child("Rahul","solo song")
child2=Child("Sheema","Dance")
child3=Child("Gitu","Plays Flute")
child4=Child("Tarun","Gymnastics")
child5=Child("Tom","MIME")
#Add different values to the list and test the program
children_list=LinkedList()
children_list.add(child1)
children_list.add(child2)
children_list.add(child3)
children_list.add(child4)
children_list.add(child5)
performance=Performance(children_list)
print("The order in which the children would perform:")
performance.get_children_list().display()
print()
print("After Rahul's performance, the schedule would change to:")
performance.change_position(child1)
performance.get_children_list().display()
print()
child6=Child("Swetha","Vote of Thanks")
print("After Swetha has joined, the schedule is:")
performance.add_new_child(child6)
performance.get_children_list().display()