From a4de4839a9ec22a1264deeb0640ff9d838609237 Mon Sep 17 00:00:00 2001 From: Subhayu Kumar Bala Date: Thu, 1 Oct 2020 00:54:11 +0530 Subject: [PATCH] PR-1 --- Data Structures/Queue.py | 61 +++++++++ Data Structures/ReverseLinkedList.py | 104 ++++++++++++++++ Data Structures/SinglyLinkedList.py | 180 +++++++++++++++++++++++++++ Data Structures/stack.py | 59 +++++++++ 4 files changed, 404 insertions(+) create mode 100644 Data Structures/Queue.py create mode 100644 Data Structures/ReverseLinkedList.py create mode 100644 Data Structures/SinglyLinkedList.py create mode 100644 Data Structures/stack.py diff --git a/Data Structures/Queue.py b/Data Structures/Queue.py new file mode 100644 index 0000000..1a22e7f --- /dev/null +++ b/Data Structures/Queue.py @@ -0,0 +1,61 @@ +from os import sys +class Queue(object): + def __init__(self): + self.array=[] + self.top=0 + self.rear=0 + + def isEmpty(self): + return self.array==[] + + def push(self,item): + self.array.insert(0,item) + self.rear+=1 + + def pop(self): + self.array.pop() + self.rear-=1 + + def menu(self): + char=0 + while char<6: + print("Press 1 -> To add a element to the Queue") + print("Press 2 -> To remove a element from the Queue") + print("Press 3 -> To view the top and rear element of the Queue") + print("Press 4 -> To view all the elements of the Queue") + print("Press 5 -> To Exit") + char=int(input("Enter your choice: ")) + print('\n') + if char==1: + val=int(input("Enter the element: ")) + self.push(val) + print("Your element has been added.") + print('\n') + elif char==2: + if self.isEmpty(): + print("Queue is underflowed. Please add elements to it.") + break + else: + self.pop() + print("Your element has been removed") + print('\n') + elif char==3: + print("Top element -> {}".format(self.array[self.top])) + print("Rear element -> {}".format(self.array[self.rear-1])) + print('\n') + elif char==4: + for i in range(0,len(self.array)): + if i==len(self.array) - 1: + print("{} <- Rear Element".format(self.array[i])) + elif i==0: + print("{} <- Top Element".format(self.array[0])) + else: + print(self.array[i]) + print('\n') + else: + sys.exit() + +Object1=Queue() +Object1.menu() + + diff --git a/Data Structures/ReverseLinkedList.py b/Data Structures/ReverseLinkedList.py new file mode 100644 index 0000000..cdae84b --- /dev/null +++ b/Data Structures/ReverseLinkedList.py @@ -0,0 +1,104 @@ +# creating the Node class to frame a single node +class Node: + def __init__(self,data): + self.data = data + self.next = None + +# creating a Linked List class to get all the helper functionalities inside +class LinkedList: + def __init__(self): + self.head = None + self.tail = None + + def append(self,data): + if self.tail is None: + self.head = Node(data) + self.tail = self.head + + else: + self.tail.next = Node(data) + self.tail = self.tail.next + + def printLL(self): + temp = self.head + while(temp): + print(temp.data, end =" ") + temp = temp.next + +# creating a solution class to implement more methods on the Linked List +class solution: + + def length(self,head): + temp = head + count = 0 + while(temp): + temp = temp.next + count += 1 + return count + + # the recursive approach to reverse the Linked List + def reverse_linked_recursive(self,head): + + # if head or it's next pointer are null + if(head == None or head.next == None): + return head + + # getting small output using recursion + small_head = self.reverse_linked_recursive(head.next) + head.next = None + + # Traversing to the end node + temp = small_head + while(temp.next != None): + temp = temp.next + + # Putting the head pointer at the next of end node + temp.next = head + head = small_head + return head + + # the iterative approach to reverse the linked list + def reverse_linked_iterative(self,head): + + # if head or it's next pointer are null + if(head == None or head.next == None): + return head + + + # getting three pointers, + + # prev = to store the previous pointer + # temp = auxiliary storage (Node pointer) + # curr = current pointer + prev = None + curr = head + + while(curr): + temp = curr.next + curr.next = prev + prev = curr + curr = temp + return prev + +if __name__ == '__main__': + llist = LinkedList() + llist.head = Node(1) + + # takig array input using map and list + arr = list(map(int,input().split())) + + # forging the linked list + for i in arr: + llist.append(i) + llist.printLL() + sol = solution() + + # recursive approach test + llist2 = LinkedList() + llist2.head = sol.reverse_linked_recurive(llist.head) + llist2.printLL() + + # iterative approach test + llist3 = LinkedList() + llist3.head = sol.reverse_linked_iterative(llist.head) + llist3.printLL() diff --git a/Data Structures/SinglyLinkedList.py b/Data Structures/SinglyLinkedList.py new file mode 100644 index 0000000..310e3b8 --- /dev/null +++ b/Data Structures/SinglyLinkedList.py @@ -0,0 +1,180 @@ +# Python program for all basic functionalities of Singly Linked List + +class Node: + # Constructor to initialize the node object + def __init__(self, data): + self.data = data + self.next = None + +class LinkedList: + # Constructor to initialize head + def __init__(self): + self.head = None + + def insertNodeAtFront(self, ndata): + nnode = Node(ndata) + nnode.next = self.head + self.head = nnode + + def insertNodeAtEnd(self, ndata): + nnode = Node(ndata) + if self.head == None: + self.head = nnode + else: + last = self.head + while last.next != None: + last = last.next + last.next = nnode + + def insertNodeAtPosition(self, index, ndata): + if index == 1: + insertNodeAtFront(self, ndata) + else: + cur = 1 + pos = self.head + while cur < index-1 and pos != None: + pos = pos.next + cur += 1 + if pos == None: + insertNodeAtEnd(self, ndata) + else: + nnode = Node(ndata) + nnode.next = pos.next + pos.next = nnode + + def deleteNodeAtFront(self): + if self.head == None: + print("Empty list, nothing to delete!") + else: + self.head = self.head.next + + def deleteNodeAtEnd(self): + if self.head == None: + print("Empty list, nothing to delete!") + elif self.head.next == None: + self.head=None + else: + pos = self.head + while pos.next.next != None: + pos = pos.next + pos.next = None + + def deleteNodeAtPosition(self, index): + if self.head == None: + print("Empty list, nothing to delete!") + return + pos = self.head + if index == 1: + self.head = pos.next + temp = None + return + for ind in range(1,index-1): + pos = pos.next + if pos == None: + break + if pos == None or pos.next == None: + print("Index more than number of nodes!") + else: + next = pos.next.next + pos.next = None + pos.next = next + + def traverseList(self): + if self.head == None: + print("Empty list, nothing to print!") + else: + n = self.head + while n != None: + print(n.data) + n = n.next + + def countNodes(self): + cnt = 0 + ind = self.head + while ind != None: + cnt += 1 + ind = ind.next + print(cnt) + +if __name__=='__main__': + newLinkedList = LinkedList() + print("SINGLY LINKED LIST") + print("1. Insert Node at Front") + print("2. Insert Node at End") + print("3. Insert Node at Position") + print("4. Delete Node at Front") + print("5. Delete Node at End") + print("6. Delete Node at Position") + print("7. Print the List") + print("8. Count nodes in the List") + print("9. Exit") + print("--Use 1-based indexing--") + while(True): + ch = int(input("Enter your choice: ")) + if ch == 1: + ndata = int(input("Enter the element to insert: ")) + newLinkedList.insertNodeAtFront(ndata) + elif ch == 2: + ndata = int(input("Enter the element to insert: ")) + newLinkedList.insertNodeAtEnd(ndata) + elif ch == 3: + ndata = int(input("Enter the element to insert: ")) + ind = int(input("Enter index to insert this element: ")) + newLinkedList.insertNodeAtPosition(ind,ndata) + elif ch == 4: + newLinkedList.deleteNodeAtFront() + elif ch == 5: + newLinkedList.deleteNodeAtEnd() + elif ch == 6: + ind = int(input("Enter index to delete element: ")) + newLinkedList.deleteNodeAtPosition(ind) + elif ch == 7: + newLinkedList.traverseList() + elif ch == 8: + newLinkedList.countNodes() + elif ch == 9: + break + else: + print("Enter a valid choice (1-9)") + + +# --- EXAMPLE --- + +# SINGLY LINKED LIST +# 1. Insert Node at Front +# 2. Insert Node at End +# 3. Insert Node at Position +# 4. Delete Node at Front +# 5. Delete Node at End +# 6. Delete Node at Position +# 7. Print the List +# 8. Count nodes in the List +# 9. Exit +# --Use 1-based indexing-- +# Enter your choice: 1 +# Enter the element to insert: 10 +# Enter your choice: 2 +# Enter the element to insert: 5 +# Enter your choice: 3 +# Enter the element to insert: 3 +# Enter index to insert this element: 2 +# Enter your choice: 7 +# 10 +# 3 +# 5 +# Enter your choice: 6 +# Enter index to delete element: 2 +# Enter your choice: 7 +# 10 +# 5 +# Enter your choice: 8 +# 2 +# Enter your choice: 4 +# Enter your choice: 7 +# 5 +# Enter your choice: 5 +# Enter your choice: 7 +# Empty list, nothing to print! +# Enter your choice: 10 +# Enter a valid choice (1-9) +# Enter your choice: 9 diff --git a/Data Structures/stack.py b/Data Structures/stack.py new file mode 100644 index 0000000..0a89652 --- /dev/null +++ b/Data Structures/stack.py @@ -0,0 +1,59 @@ +from os import sys +class Stack(object): + def __init__(self): + self.array=[] + self.reverse=[] + self.top=0 + + def push(self,item): + self.array.append(item) + self.top+=1 + + + def pop(self): + self.top-=1 + self.array.pop() + + + def isEmpty(self): + return self.array==[] + + def menu(self): + char=0 + while char<6: + print("Press 1 -> To add a element in the Stack") + print("Press 2 -> To remove a element from the Stack") + print("Press 3 -> To veiw the top element of the Stack") + print("Press 4 -> To display all the elements of the Stack") + print("Press 5 -> To Exit") + print('\n') + char=int(input("Enter your choice: ")) + print('\n') + if char==1: + val=int(input("Enter the value you want to add: ")) + self.push(val) + print("Your item {} has been added.".format(val)) + print('\n') + elif char==2: + if self.isEmpty(): + print("Stack underflowed! Please add items into the stack") + print('\n') + break + else: + self.pop() + elif char==3: + print("The top item is -> {}".format(self.array[self.top-1])) + print('\n') + elif char==4: + print("The Stack: ") + self.reverse=self.array[::-1] + for i in range (0,len(self.reverse)): + if i == 0: + print("{} <- Top Item".format(self.reverse[i])) + else: + print(self.reverse[i]) + else: + sys.exit() + +Object1=Stack() +Object1.menu()