8000 Hacktoberfest-Code for dequeue by yashaswiadyalu · Pull Request #119 · AllAlgorithms/python · GitHub
[go: up one dir, main page]

Skip to content

Hacktoberfest-Code for dequeue #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
10000
Diff view
116 changes: 116 additions & 0 deletions data-structures/linked-lists/circularsinglelinkedlist.py
< 8A5B /tr>
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
class Node:
def __init__(self, data):
self.data = data
self.next = None


class CircularLinkedList:
def __init__(self):
self.head = None

def get_node(self, index):
if self.head is None:
return None
current = self.head
for i in range(index):
current = current.next
if current == self.head:
return None
return current

def get_prev_node(self, ref_node):
if self.head is None:
return None
current = self.head
while current.next != ref_node:
current = current.next
return current

def insert_after(self, ref_node, new_node):
new_node.next = ref_node.next
ref_node.next = new_node

def insert_before(self, ref_node, new_node):
prev_node = self.get_prev_node(ref_node)
self.insert_after(prev_node, new_node)

def insert_at_end(self, new_node):
if self.head is None:
self.head = new_node
new_node.next = new_node
else:
self.insert_before(self.head, new_node)

def insert_at_beg(self, new_node):
self.insert_at_end(new_node)
self.head = new_node

def remove(self, node):
if self.head.next == self.head:
self.head = None
else:
prev_node = self.get_prev_node(node)
prev_node.next = node.next
if self.head == node:
self.head = node.next

def display(self):
if self.head is None:
return
current = self.head
while True:
print(current.data, end = ' ')
current = current.next
if current == self.head:
break


a_cllist = CircularLinkedList()

print('Menu')
print('insert <data> after <index>')
print('insert <data> before <index>')
print('insert <data> at beg')
print('insert <data> at end')
print('remove <index>')
print('quit')

while True:
print('The list: ', end = '')
a_cllist.display()
print()
do = input('What would you like to do? ').split()

operation = do[0].strip().lower()

if operation == 'insert':
data = int(do[1])
position = do[3].strip().lower()
new_node = Node(data)
suboperation = do[2].strip().lower()
if suboperation == 'at':
if position == 'beg':
a_cllist.insert_at_beg(new_node)
elif position == 'end':
a_cllist.insert_at_end(new_node)
else:
index = int(position)
ref_node = a_cllist.get_node(index)
if ref_node is None:
print('No such index.')
continue
if suboperation == 'after':
a_cllist.insert_after(ref_node, new_node)
elif suboperation == 'before':
a_cllist.insert_before(ref_node, new_node)

elif operation == 'remove':
index = int(do[1])
node = a_cllist.get_node(index)
if node is None:
print('No such index.')
continue
a_cllist.remove(node)

elif operation == 'quit':
break
0