-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.py
More file actions
73 lines (61 loc) · 1.51 KB
/
Copy pathlinked_list.py
File metadata and controls
73 lines (61 loc) · 1.51 KB
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
class Node:
def __init__(self,v=None):
self.value=v
self.next=None
def isempty(self):
if self.value==None:
return(True)
else:
return (False)
def append(self,v):
if self.isempty():
self.value=v
return()
temp=self
while temp.next!=None:
temp=temp.next
newnode=Node(v)
temp.next=newnode
return()
def insert(self,v):
if self.isempty():
self.value=v
return
newnode=Node(v)
(self.value,newnode.value)=(newnode.value,self.value)
(self.next,newnode.next)=(newnode,self.next)
def delete(self,x):
if self.isempty():
return()
if self.value==x:
if self.next==None:
self.value=None
else:
self.value=self.next.value
self.next=self.next.next
temp=self
while temp.next!=None:
if temp.next.value==x:
temp.next=temp.next.next
return()
else:
temp=temp.next
return()
def __str__(self):
selflist=[]
if self.value==None:
return(str(selflist))
temp=self
selflist.append(temp.value)
while temp.next!=None:
temp=temp.next
selflist.append(temp.value)
return(str(selflist))
l=Node(1)
print(l)
l.append(2)
print(l)
l.delete(2)
print(l)
l.insert(24)
print(l)