Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
/pythonPublic archive

Commitd681e09

Browse files
code for circulardoublylinkedlist
1 parent337e300 commitd681e09

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
classNode:
2+
def__init__(self,data):
3+
self.data=data
4+
self.next=None
5+
self.prev=None
6+
7+
8+
classCircularDoublyLinkedList:
9+
def__init__(self):
10+
self.first=None
11+
12+
defget_node(self,index):
13+
current=self.first
14+
foriinrange(index):
15+
current=current.next
16+
ifcurrent==self.first:
17+
returnNone
18+
returncurrent
19+
20+
definsert_after(self,ref_node,new_node):
21+
new_node.prev=ref_node
22+
new_node.next=ref_node.next
23+
new_node.next.prev=new_node
24+
ref_node.next=new_node
25+
26+
definsert_before(self,ref_node,new_node):
27+
self.insert_after(ref_node.prev,new_node)
28+
29+
definsert_at_end(self,new_node):
30+
ifself.firstisNone:
31+
self.first=new_node
32+
new_node.next=new_node
33+
new_node.prev=new_node
34+
else:
35+
self.insert_after(self.first.prev,new_node)
36+
37+
definsert_at_beg(self,new_node):
38+
self.insert_at_end(new_node)
39+
self.first=new_node
40+
41+
defremove(self,node):
42+
ifself.first.next==self.first:
43+
self.first=None
44+
else:
45+
node.prev.next=node.next
46+
node.next.prev=node.prev
47+
ifself.first==node:
48+
self.first=node.next
49+
50+
defdisplay(self):
51+
ifself.firstisNone:
52+
return
53+
current=self.first
54+
whileTrue:
55+
print(current.data,end=' ')
56+
current=current.next
57+
ifcurrent==self.first:
58+
break
59+
60+
61+
a_cdllist=CircularDoublyLinkedList()
62+
63+
print('Menu')
64+
print('insert <data> after <index>')
65+
print('insert <data> before <index>')
66+
print('insert <data> at beg')
67+
print('insert <data> at end')
68+
print('remove <index>')
69+
print('quit')
70+
71+
whileTrue:
72+
print('The list: ',end='')
73+
a_cdllist.display()
74+
print()
75+
do=input('What would you like to do? ').split()
76+
77+
operation=do[0].strip().lower()
78+
79+
ifoperation=='insert':
80+
data=int(do[1])
81+
position=do[3].strip().lower()
82+
new_node=Node(data)
83+
suboperation=do[2].strip().lower()
84+
ifsuboperation=='at':
85+
ifposition=='beg':
86+
a_cdllist.insert_at_beg(new_node)
87+
elifposition=='end':
88+
a_cdllist.insert_at_end(new_node)
89+
else:
90+
index=int(position)
91+
ref_node=a_cdllist.get_node(index)
92+
ifref_nodeisNone:
93+
print('No such index.')
94+
continue
95+
ifsuboperation=='after':
96+
a_cdllist.insert_after(ref_node,new_node)
97+
elifsuboperation=='before':
98+
a_cdllist.insert_before(ref_node,new_node)
99+
100+
elifoperation=='remove':
101+
index=int(do[1])
102+
node=a_cdllist.get_node(index)
103+
ifnodeisNone:
104+
print('No such index.')
105+
continue
106+
a_cdllist.remove(node)
107+
108+
elifoperation=='quit':
109+
break

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp