-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.py
152 lines (128 loc) · 3.82 KB
/
linked_list.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
class Node:
def __init__(self,item=None,next=None):
self.item=item
self.next=next
class Sll:
def __init__(self,start=None):
self.start=start
def is_empty(self):
return self.start is None
def insert_at_start(self,data):
n=Node(data,self.start)
self.start=n
def search(self,data):
temp=self.start
while temp is not None:
if temp.item==data:
return temp
else:
temp=temp.next
return None
def insert_at_last(self,data):
temp=self.start
n=Node(data)
while temp.next is not None:
temp=temp.next
temp.next=n
def insert_after(self,target_node,data):
if target_node is not None:
n=Node(data,target_node.next)
target_node.next=n
def delete_first(self):
if self.start is None: #checking if linked list is empty
pass
elif self.start.next is None: #checking if linked list has onely one node
self.start=None
else: #else case where linked list has more than one node
self.start=self.start.next
def delete_last(self):
if self.start is None:
pass
elif self.start.next is None:
self.start=None
else:
temp=self.start
while temp.next.next is not None:
temp=temp.next
temp.next=None
def delete_specific_item(self,data):
if self.start is None:
pass
elif self.start.next is None:
if self.start.item==data:
self.start=None
else:
temp=self.start
if temp.item==data:
self.start=temp.next
else:
while temp.next is not None: #checking current note next if it is None or not
if temp.next.item==data:
temp.next=temp.next.next
break
temp=temp.next
def print_list(self):
temp=self.start
#print(temp.item,temp.next)
while temp is not None:
print(temp.item,end=' ')
temp=temp.next
#print(temp.item,end=' ')
def __iter__(self):
return SLLIterator(self.start)
class SLLIterator:
def __init__(self,start):
self.current=start
def __iter__(self):
return self
def __next__(self):
if not self.current:
raise StopIteration
data=self.current.item
self.current=self.current.next
return data
mylist=Sll()
# mylist.insert_at_start(10)
# mylist.insert_at_start(60)
# mylist.insert_at_start(30)
# mylist.insert_at_start(40)
# # mylist.print_list()
# for i in mylist:
# print(i,end=' ')
# mylist.insert_at_last(70)
# mylist.print_list()
# print("------",end='\n')
# print(mylist.is_empty())
# print(f"item is at node {mylist.search(80)} and item is not present")
# print(f"item is at node {mylist.search(70)} and item is present")
# mylist.insert_after(mylist.search(70),90)
# mylist.print_list()
# print()
# print("delete first",end='\n')
# print("--------",end='\n')
# mylist.delete_first()
# mylist.print_list()
# print()
# print("delete first",end='\n')
# print("--------",end='\n')
# mylist.delete_first()
# mylist.print_list()
# print()
# print("delete first",end='\n')
# print("--------",end='\n')
# mylist.delete_first()
# mylist.print_list()
# print()
# print("delete last",end='\n')
# print("--------",end='\n')
# mylist.delete_last()
# mylist.print_list()
# print()
# print("delete last",end='\n')
# print("--------",end='\n')
# mylist.delete_last()
# mylist.print_list()
# print("delete specific item",end='\n')
# print("--------",end='\n')
# mylist.delete_specific_item(10)
# mylist.print_list()