From 1edc751786c117ac05ce136997380172d32172a3 Mon Sep 17 00:00:00 2001 From: TheIncredibleHarsh Date: Tue, 4 Jun 2019 10:03:44 -0700 Subject: [PATCH 1/3] circular_linked_list Circular linked list is a linked list where last node is connected to the first node in the list. This can be used in traversals where we have to access a node before the current by access in a circular way. --- .../linked_list/circular_linked_list | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 data_structures/linked_list/circular_linked_list diff --git a/data_structures/linked_list/circular_linked_list b/data_structures/linked_list/circular_linked_list new file mode 100644 index 000000000000..6957002a74de --- /dev/null +++ b/data_structures/linked_list/circular_linked_list @@ -0,0 +1,78 @@ +from __future__ import print_function + +class Node: #create a node + def __init__(self,data): + self.data = data; #giving data + self.next = None; #giving next to None + +class Circular_Linked_List: + def __init__(self): + self.last = None #Initializing last to None + + def insert_beginning(self,data): + newNode = Node(data) #create node + if self.last == None: #If last is None, Make newNode last + self.last = newNode + self.last.next = self.last + else: + newNode.next = self.last.next #set newnode's next as head + self.last.next = newNode + + + def insert_end(self,data): + newNode = Node(data) + if self.last == None: + self.last = newNode + self.last.next = self.last + else: + newNode.next = self.last.next #set newnode's next as head + self.last.next = newNode + self.last = newNode #set newnode as last node + + def delete_beginning(self): + node = self.last.next + self.last.next = node.next #set last's next to first node's next + return node.data + + def delete_end(self): + temp = self.last.next + while temp.next is not self.last: #Traversing the list + temp=temp.next + node = self.last + temp.next = self.last.next; #set second last node's next to last's next + self.last = temp + return node.data + + def print_list(self): + print("The list is:") + temp = self.last.next #Creating iterator + while temp is not self.last: #Traversing the list + print(temp.data) + temp = temp.next + print(temp.data) #print last node + +def main(): + LL = Circular_Linked_List() + print("Inserting at beginning: ") + a1 = input() + LL.insert_beginning(a1) + print("Inserting at beginning: ") + a2 = input() + LL.insert_beginning(a2) + LL.print_list() + print("Inserting at the end: ") + a1 = input() + LL.insert_end(a1) + print("Inserting at the end: ") + a2 = input() + LL.insert_end(a2) + LL.print_list() + print("Deleting from beginning") + print(LL.delete_beginning()," deleted") + LL.print_list() + print("Deleting from end") + print(LL.delete_end()," deleted") + LL.print_list() + +if __name__ == '__main__': + main() From ed78357961e917dfd759c7064bb3f7b509621d9d Mon Sep 17 00:00:00 2001 From: TheIncredibleHarsh Date: Tue, 4 Jun 2019 10:20:55 -0700 Subject: [PATCH 2/3] Renamed file and removed semicolons --- .../{circular_linked_list => circular_linked_list.py} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename data_structures/linked_list/{circular_linked_list => circular_linked_list.py} (92%) diff --git a/data_structures/linked_list/circular_linked_list b/data_structures/linked_list/circular_linked_list.py similarity index 92% rename from data_structures/linked_list/circular_linked_list rename to data_structures/linked_list/circular_linked_list.py index 6957002a74de..1470c5f4deb4 100644 --- a/data_structures/linked_list/circular_linked_list +++ b/data_structures/linked_list/circular_linked_list.py @@ -2,8 +2,8 @@ class Node: #create a node def __init__(self,data): - self.data = data; #giving data - self.next = None; #giving next to None + self.data = data #giving data + self.next = None #giving next to None class Circular_Linked_List: def __init__(self): @@ -39,7 +39,7 @@ def delete_end(self): while temp.next is not self.last: #Traversing the list temp=temp.next node = self.last - temp.next = self.last.next; #set second last node's next to last's next + temp.next = self.last.next #set second last node's next to last's next self.last = temp return node.data From f425a8682780bece836e617bf36995958e9fa30a Mon Sep 17 00:00:00 2001 From: TheIncredibleHarsh Date: Wed, 5 Jun 2019 01:06:40 -0700 Subject: [PATCH 3/3] Indented with 4 spaces --- .../linked_list/circular_linked_list.py | 129 +++++++++--------- 1 file changed, 65 insertions(+), 64 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 1470c5f4deb4..d482431091e5 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -1,78 +1,79 @@ from __future__ import print_function class Node: #create a node - def __init__(self,data): - self.data = data #giving data - self.next = None #giving next to None + def __init__(self,data): + self.data = data #giving data + self.next = None #giving next to None class Circular_Linked_List: - def __init__(self): - self.last = None #Initializing last to None + def __init__(self): + self.last = None #Initializing last to None - def insert_beginning(self,data): - newNode = Node(data) #create node - if self.last == None: #If last is None, Make newNode last - self.last = newNode - self.last.next = self.last - else: - newNode.next = self.last.next #set newnode's next as head - self.last.next = newNode + def insert_beginning(self,data): + newNode = Node(data) #create node + if self.last == None: #If last is None, Make newNode last + self.last = newNode + self.last.next = self.last + else: + newNode.next = self.last.next #set newnode's next as head + self.last.next = newNode + def insert_end(self,data): + newNode = Node(data) + if self.last == None: + self.last = newNode + self.last.next = self.last + else: + newNode.next = self.last.next #set newnode's next as head + self.last.next = newNode + self.last = newNode #set newnode as last node - def insert_end(self,data): - newNode = Node(data) - if self.last == None: - self.last = newNode - self.last.next = self.last - else: - newNode.next = self.last.next #set newnode's next as head - self.last.next = newNode - self.last = newNode #set newnode as last node - - def delete_beginning(self): - node = self.last.next - self.last.next = node.next #set last's next to first node's next - return node.data + def delete_beginning(self): + node = self.last.next + self.last.next = node.next #set last's next to first node's next + return node.data - def delete_end(self): - temp = self.last.next - while temp.next is not self.last: #Traversing the list - temp=temp.next - node = self.last - temp.next = self.last.next #set second last node's next to last's next - self.last = temp - return node.data + def delete_end(self): + temp = self.last.next + while temp.next is not self.last: #Traversing the list + temp=temp.next + node = self.last + temp.next = self.last.next #set second last node's next to last's next + self.last = temp + return node.data - def print_list(self): - print("The list is:") - temp = self.last.next #Creating iterator - while temp is not self.last: #Traversing the list - print(temp.data) - temp = temp.next - print(temp.data) #print last node + def print_list(self): + print("The list is:") + temp = self.last.next #Creating iterator + while temp is not self.last: #Traversing the list + print(temp.data) + temp = temp.next + print(temp.data) #print last node + + #End Class def main(): - LL = Circular_Linked_List() - print("Inserting at beginning: ") - a1 = input() - LL.insert_beginning(a1) - print("Inserting at beginning: ") - a2 = input() - LL.insert_beginning(a2) - LL.print_list() - print("Inserting at the end: ") - a1 = input() - LL.insert_end(a1) - print("Inserting at the end: ") - a2 = input() - LL.insert_end(a2) - LL.print_list() - print("Deleting from beginning") - print(LL.delete_beginning()," deleted") - LL.print_list() - print("Deleting from end") - print(LL.delete_end()," deleted") - LL.print_list() + LL = Circular_Linked_List() + print("Inserting at beginning: ") + a1 = input() + LL.insert_beginning(a1) + print("Inserting at beginning: ") + a2 = input() + LL.insert_beginning(a2) + LL.print_list() + print("Inserting at the end: ") + a1 = input() + LL.insert_end(a1) + print("Inserting at the end: ") + a2 = input() + LL.insert_end(a2) + LL.print_list() + print("Deleting from beginning") + print(LL.delete_beginning()," deleted") + LL.print_list() + print("Deleting from end") + print(LL.delete_end()," deleted") + LL.print_list() if __name__ == '__main__': - main() + main()