Skip to content

Commit 2ec20e6

Browse files
authored
Merge pull request DeepNinja07x#7 from subhayu99/Subhayu
Added 2 Data Structures and fixed a typo
2 parents be89be2 + a4de483 commit 2ec20e6

File tree

4 files changed

+404
-0
lines changed

4 files changed

+404
-0
lines changed

Data Structures/Queue.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from os import sys
2+
class Queue(object):
3+
def __init__(self):
4+
self.array=[]
5+
self.top=0
6+
self.rear=0
7+
8+
def isEmpty(self):
9+
return self.array==[]
10+
11+
def push(self,item):
12+
self.array.insert(0,item)
13+
self.rear+=1
14+
15+
def pop(self):
16+
self.array.pop()
17+
self.rear-=1
18+
19+
def menu(self):
20+
char=0
21+
while char<6:
22+
print("Press 1 -> To add a element to the Queue")
23+
print("Press 2 -> To remove a element from the Queue")
24+
print("Press 3 -> To view the top and rear element of the Queue")
25+
print("Press 4 -> To view all the elements of the Queue")
26+
print("Press 5 -> To Exit")
27+
char=int(input("Enter your choice: "))
28+
print('\n')
29+
if char==1:
30+
val=int(input("Enter the element: "))
31+
self.push(val)
32+
print("Your element has been added.")
33+
print('\n')
34+
elif char==2:
35+
if self.isEmpty():
36+
print("Queue is underflowed. Please add elements to it.")
37+
break
38+
else:
39+
self.pop()
40+
print("Your element has been removed")
41+
print('\n')
42+
elif char==3:
43+
print("Top element -> {}".format(self.array[self.top]))
44+
print("Rear element -> {}".format(self.array[self.rear-1]))
45+
print('\n')
46+
elif char==4:
47+
for i in range(0,len(self.array)):
48+
if i==len(self.array) - 1:
49+
print("{} <- Rear Element".format(self.array[i]))
50+
elif i==0:
51+
print("{} <- Top Element".format(self.array[0]))
52+
else:
53+
print(self.array[i])
54+
print('\n')
55+
else:
56+
sys.exit()
57+
58+
Object1=Queue()
59+
Object1.menu()
60+
61+
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# creating the Node class to frame a single node
2+
class Node:
3+
def __init__(self,data):
4+
self.data = data
5+
self.next = None
6+
7+
# creating a Linked List class to get all the helper functionalities inside
8+
class LinkedList:
9+
def __init__(self):
10+
self.head = None
11+
self.tail = None
12+
13+
def append(self,data):
14+
if self.tail is None:
15+
self.head = Node(data)
16+
self.tail = self.head
17+
18+
else:
19+
self.tail.next = Node(data)
20+
self.tail = self.tail.next
21+
22+
def printLL(self):
23+
temp = self.head
24+
while(temp):
25+
print(temp.data, end =" ")
26+
temp = temp.next
27+
28+
# creating a solution class to implement more methods on the Linked List
29+
class solution:
30+
31+
def length(self,head):
32+
temp = head
33+
count = 0
34+
while(temp):
35+
temp = temp.next
36+
count += 1
37+
return count
38+
39+
# the recursive approach to reverse the Linked List
40+
def reverse_linked_recursive(self,head):
41+
42+
# if head or it's next pointer are null
43+
if(head == None or head.next == None):
44+
return head
45+
46+
# getting small output using recursion
47+
small_head = self.reverse_linked_recursive(head.next)
48+
head.next = None
49+
50+
# Traversing to the end node
51+
temp = small_head
52+
while(temp.next != None):
53+
temp = temp.next
54+
55+
# Putting the head pointer at the next of end node
56+
temp.next = head
57+
head = small_head
58+
return head
59+
60+
# the iterative approach to reverse the linked list
61+
def reverse_linked_iterative(self,head):
62+
63+
# if head or it's next pointer are null
64+
if(head == None or head.next == None):
65+
return head
66+
67+
68+
# getting three pointers,
69+
70+
# prev = to store the previous pointer
71+
# temp = auxiliary storage (Node pointer)
72+
# curr = current pointer
73+
prev = None
74+
curr = head
75+
76+
while(curr):
77+
temp = curr.next
78+
curr.next = prev
79+
prev = curr
80+
curr = temp
81+
return prev
82+
83+
if __name__ == '__main__':
84+
llist = LinkedList()
85+
llist.head = Node(1)
86+
87+
# takig array input using map and list
88+
arr = list(map(int,input().split()))
89+
90+
# forging the linked list
91+
for i in arr:
92+
llist.append(i)
93+
llist.printLL()
94+
sol = solution()
95+
96+
# recursive approach test
97+
llist2 = LinkedList()
98+
llist2.head = sol.reverse_linked_recurive(llist.head)
99+
llist2.printLL()
100+
101+
# iterative approach test
102+
llist3 = LinkedList()
103+
llist3.head = sol.reverse_linked_iterative(llist.head)
104+
llist3.printLL()
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# Python program for all basic functionalities of Singly Linked List
2+
3+
class Node:
4+
# Constructor to initialize the node object
5+
def __init__(self, data):
6+
self.data = data
7+
self.next = None
8+
9+
class LinkedList:
10+
# Constructor to initialize head
11+
def __init__(self):
12+
self.head = None
13+
14+
def insertNodeAtFront(self, ndata):
15+
nnode = Node(ndata)
16+
nnode.next = self.head
17+
self.head = nnode
18+
19+
def insertNodeAtEnd(self, ndata):
20+
nnode = Node(ndata)
21+
if self.head == None:
22+
self.head = nnode
23+
else:
24+
last = self.head
25+
while last.next != None:
26+
last = last.next
27+
last.next = nnode
28+
29+
def insertNodeAtPosition(self, index, ndata):
30+
if index == 1:
31+
insertNodeAtFront(self, ndata)
32+
else:
33+
cur = 1
34+
pos = self.head
35+
while cur < index-1 and pos != None:
36+
pos = pos.next
37+
cur += 1
38+
if pos == None:
39+
insertNodeAtEnd(self, ndata)
40+
else:
41+
nnode = Node(ndata)
42+
nnode.next = pos.next
43+
pos.next = nnode
44+
45+
def deleteNodeAtFront(self):
46+
if self.head == None:
47+
print("Empty list, nothing to delete!")
48+
else:
49+
self.head = self.head.next
50+
51+
def deleteNodeAtEnd(self):
52+
if self.head == None:
53+
print("Empty list, nothing to delete!")
54+
elif self.head.next == None:
55+
self.head=None
56+
else:
57+
pos = self.head
58+
while pos.next.next != None:
59+
pos = pos.next
60+
pos.next = None
61+
62+
def deleteNodeAtPosition(self, index):
63+
if self.head == None:
64+
print("Empty list, nothing to delete!")
65+
return
66+
pos = self.head
67+
if index == 1:
68+
self.head = pos.next
69+
temp = None
70+
return
71+
for ind in range(1,index-1):
72+
pos = pos.next
73+
if pos == None:
74+
break
75+
if pos == None or pos.next == None:
76+
print("Index more than number of nodes!")
77+
else:
78+
next = pos.next.next
79+
pos.next = None
80+
pos.next = next
81+
82+
def traverseList(self):
83+
if self.head == None:
84+
print("Empty list, nothing to print!")
85+
else:
86+
n = self.head
87+
while n != None:
88+
print(n.data)
89+
n = n.next
90+
91+
def countNodes(self):
92+
cnt = 0
93+
ind = self.head
94+
while ind != None:
95+
cnt += 1
96+
ind = ind.next
97+
print(cnt)
98+
99+
if __name__=='__main__':
100+
newLinkedList = LinkedList()
101+
print("SINGLY LINKED LIST")
102+
print("1. Insert Node at Front")
103+
print("2. Insert Node at End")
104+
print("3. Insert Node at Position")
105+
print("4. Delete Node at Front")
106+
print("5. Delete Node at End")
107+
print("6. Delete Node at Position")
108+
print("7. Print the List")
109+
print("8. Count nodes in the List")
110+
print("9. Exit")
111+
print("--Use 1-based indexing--")
112+
while(True):
113+
ch = int(input("Enter your choice: "))
114+
if ch == 1:
115+
ndata = int(input("Enter the element to insert: "))
116+
newLinkedList.insertNodeAtFront(ndata)
117+
elif ch == 2:
118+
ndata = int(input("Enter the element to insert: "))
119+
newLinkedList.insertNodeAtEnd(ndata)
120+
elif ch == 3:
121+
ndata = int(input("Enter the element to insert: "))
122+
ind = int(input("Enter index to insert this element: "))
123+
newLinkedList.insertNodeAtPosition(ind,ndata)
124+
elif ch == 4:
125+
newLinkedList.deleteNodeAtFront()
126+
elif ch == 5:
127+
newLinkedList.deleteNodeAtEnd()
128+
elif ch == 6:
129+
ind = int(input("Enter index to delete element: "))
130+
newLinkedList.deleteNodeAtPosition(ind)
131+
elif ch == 7:
132+
newLinkedList.traverseList()
133+
elif ch == 8:
134+
newLinkedList.countNodes()
135+
elif ch == 9:
136+
break
137+
else:
138+
print("Enter a valid choice (1-9)")
139+
140+
141+
# --- EXAMPLE ---
142+
143+
# SINGLY LINKED LIST
144+
# 1. Insert Node at Front
145+
# 2. Insert Node at End
146+
# 3. Insert Node at Position
147+
# 4. Delete Node at Front
148+
# 5. Delete Node at End
149+
# 6. Delete Node at Position
150+
# 7. Print the List
151+
# 8. Count nodes in the List
152+
# 9. Exit
153+
# --Use 1-based indexing--
154+
# Enter your choice: 1
155+
# Enter the element to insert: 10
156+
# Enter your choice: 2
157+
# Enter the element to insert: 5
158+
# Enter your choice: 3
159+
# Enter the element to insert: 3
160+
# Enter index to insert this element: 2
161+
# Enter your choice: 7
162+
# 10
163+
# 3
164+
# 5
165+
# Enter your choice: 6
166+
# Enter index to delete element: 2
167+
# Enter your choice: 7
168+
# 10
169+
# 5
170+
# Enter your choice: 8
171+
# 2
172+
# Enter your choice: 4
173+
# Enter your choice: 7
174+
# 5
175+
# Enter your choice: 5
176+
# Enter your choice: 7
177+
# Empty list, nothing to print!
178+
# Enter your choice: 10
179+
# Enter a valid choice (1-9)
180+
# Enter your choice: 9

0 commit comments

Comments
 (0)