Skip to content
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

Update CircularLinkedList.py #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Diff view
205 changes: 104 additions & 101 deletions LinkedLists/CircularLinkedList.py
Original file line number Diff line number Diff line change
@@ -1,107 +1,110 @@
class Node(object):

def __init__ (self, d, n = None):
self.data = d
self.next_node = n

def get_next (self):
return self.next_node

def set_next (self, n):
self.next_node = n

def get_data (self):
return self.data

def set_data (self, d):
self.data = d

def __str__(self):
return "Node value: " + str(self.data)

class CircularLinkedList (object):

def __init__ (self, r = None):
self.root = r
self.size = 0

def get_size (self):
return self.size

def add (self, d):
if self.get_size() == 0:
self.root = Node(d)
self.root.set_next(self.root)
else:
new_node = Node (d, self.root.get_next())
self.root.set_next(new_node)
self.size += 1

def remove (self, d):
this_node = self.root
prev_node = None

while True:
if this_node.get_data() == d: # found
if prev_node is not None:
prev_node.set_next(this_node.get_next())
else:
while this_node.get_next() != self.root:
this_node = this_node.get_next()
this_node.set_next(self.root.get_next())
self.root = self.root.get_next()
self.size -= 1
return True # data removed
elif this_node.get_next() == self.root:
return False # data not found
prev_node = this_node
this_node = this_node.get_next()

def find (self, d):
this_node = self.root
while True:
if this_node.get_data() == d:
return d
elif this_node.get_next() == self.root:
return False
this_node = this_node.get_next()

def print_list (self):
print ("Print List..........")
if self.root is None:
return
this_node = self.root
print (this_node)
while this_node.get_next() != self.root:
this_node = this_node.get_next()
print (this_node)
def __init__(self, d, n=None):
self.data = d
self.next_node = n

def get_next(self):
return self.next_node

def set_next(self, n):
self.next_node = n

def get_data(self):
return self.data

def set_data(self, d):
self.data = d

def __str__(self):
return "Node value: " + str(self.data)


class CircularLinkedList(object):
def __init__(self, r=None):
self.root = r
self.size = 0

def get_size(self):
return self.size

def add(self, d):
if self.get_size() == 0:
# if the list is empty, create a new node and set it as the root
self.root = Node(d)
self.root.set_next(self.root) # make the root node circular
else:
# create a new node and add it after the root node
new_node = Node(d, self.root.get_next())
self.root.set_next(new_node)
self.size += 1

def remove(self, d):
this_node = self.root
prev_node = None

while True:
if this_node.get_data() == d: # found
if prev_node is not None:
prev_node.set_next(this_node.get_next())
else:
# if the root node is being removed, make the next node as the new root
while this_node.get_next() != self.root:
this_node = this_node.get_next()
this_node.set_next(self.root.get_next())
self.root = self.root.get_next()
self.size -= 1
return True # data removed
elif this_node.get_next() == self.root:
return False # data not found
prev_node = this_node
this_node = this_node.get_next()

def find(self, d):
this_node = self.root
while True:
if this_node.get_data() == d:
return d
elif this_node.get_next() == self.root:
return False
this_node = this_node.get_next()

def print_list(self):
print("Print List..........")
if self.root is None:
return
this_node = self.root
print(this_node)
while this_node.get_next() != self.root:
this_node = this_node.get_next()
print(this_node)

def main():
myList = CircularLinkedList()
myList.add(5)
myList.add(7)
myList.add(3)
myList.add(8)
myList.add(9)
print("Find 8", myList.find(8))
print("Find 12", myList.find(12))

cur = myList.root
print (cur)
for i in range(8):
cur = cur.get_next();
print (cur)

print("size="+str(myList.get_size()))
myList.print_list()
myList.remove(8)
print("size="+str(myList.get_size()))
print("Remove 15", myList.remove(15))
print("size="+str(myList.get_size()))
myList.remove(5) # delete root node
myList.print_list()

main()
myList = CircularLinkedList()
myList.add(5)
myList.add(7)
myList.add(3)
myList.add(8)
myList.add(9)
print("Find 8", myList.find(8))
print("Find 12", myList.find(12))

cur = myList.root
print(cur)
for i in range(8):
cur = cur.get_next()
print(cur)

print("size=" + str(myList.get_size()))
myList.print_list()
myList.remove(8)
print("size=" + str(myList.get_size()))
print("Remove 15", myList.remove(15))
print("size=" + str(myList.get_size()))
myList.remove(5) # delete root node
myList.print_list()

if __name__ == '__main__':
main()



Expand Down