Skip to content
Merged
Show file tree
Hide file tree
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
61 changes: 61 additions & 0 deletions Data Structures/Queue.py
Original file line number Diff line number Diff line change
@@ -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()


104 changes: 104 additions & 0 deletions Data Structures/ReverseLinkedList.py
Original file line number Diff line number Diff line change
@@ -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()
180 changes: 180 additions & 0 deletions Data Structures/SinglyLinkedList.py
Original file line number Diff line number Diff line change
@@ -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
Loading