Skip to content

Commit

Permalink
new ds
Browse files Browse the repository at this point in the history
  • Loading branch information
BALAJI24092001 committed Jan 10, 2025
1 parent 7fd4376 commit c48f277
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 32 deletions.
67 changes: 67 additions & 0 deletions DSA/Data Structures/Doubly Linked List.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class Node:
def __init__(self, val):
self.val = val
self.next = None
self.prev = None


class DoublyLinkedList:
def __init__(self):
self.main = None

def append_(self, val):
if self.main is None:
self.main = Node(val)
return
temp = self.main
while temp is not None:
if temp.next is None:
temp.next = Node(val)
temp.next.prev = temp
return
else:
temp = temp.next

def print_(self):
temp = None
if self.main is None:
print("Doubly Linked List is Empty")
return
else:
temp = self.main
while temp is not None:
print("_" * 12)
print("|" + f"{temp.val}".center(10) + "|")
print("־" * 12)
print(" ʌ".center(12))
print("| |".center(12))
print("V ".center(12))
temp = temp.next

def delete_(self, value):
if self.main.val == value:
self.main = self.main.next
temp = self.main
if temp is not None:
while temp is not None:
if temp.val == value:
temp.prev.next = temp.next
return
else:
temp = temp.next

def prepend_(self, val):
self.main.prev = Node(val)
self.main.prev.next = self.main
self.main = self.main.prev


if __name__ == "__main__":
dll = DoublyLinkedList()
dll.append_(1)
dll.append_(2)
dll.append_(2)
dll.append_(3)
dll.prepend_(0)
dll.delete_(2)
dll.print_()
67 changes: 67 additions & 0 deletions DSA/Data Structures/Singly Linked List.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class Node:
def __init__(self, val):
self.val = val
self.next = None


class SinglyLinkedList:
def __init__(self):
self.main = None

def append_(self, val):
temp = None
if self.main is None:
self.main = Node(val)
return
else:
temp = self.main
while temp.next is not None:
temp = temp.next
temp.next = Node(val)

def print_(self):
temp = None
if self.main is None:
print("Singly Linked List is Empty")
return
else:
temp = self.main
while temp is not None:
print("_" * 12)
print("|" + f"{temp.val}".center(10) + "|")
print("־" * 12)
print("|".center(12))
print("V".center(12))
temp = temp.next

def prepend_(self, val):
temp = self.main
self.main = Node(val)
self.main.next = temp

def delete_(self, value):
"""
Give the value in the list to be deleted from the list.
"""
temp = self.main
if temp is None:
print("No values in the List!")
return
while temp is not None:
if temp.next is not None:
if temp.next.val == value:
temp.next = temp.next.next
return
else:
temp = temp.next


if __name__ == "__main__":
ll = SinglyLinkedList()
ll.append_(2)
ll.append_(2)
ll.prepend_(1)
ll.delete_(2)
ll.append_(3)
ll.append_(4)
ll.print_()
25 changes: 12 additions & 13 deletions DSA/Data Structures/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,37 @@ class queue:
def __init__(self):
self.lst = []

def push(self, val):
def push_(self, val):
self.lst.append(val)

def pop(self, val):
if not self.isEmpty():
def pop_(self, val):
if not self.IsEmpty_():
self.lst = self.lst[1:]
else:
print("Queue is Empty!")

def size(self):
def size_(self):
return len(self.lst)

def print(self):
if not self.isEmpty():
def print_(self):
if not self.IsEmpty_():
print("\n" + "Queue".center(12))
for i in range(len(self.lst) - 1, -1, -1):
string = "\n " + "_" * 10 + "\n|" + \
str(self.lst[i]).center(10) + "|"
string = "\n " + "_" * 10 + "\n|" + str(self.lst[i]).center(10) + "|"
print(string, end="")
else:
print("Stack is Empty!")

print("------->")

def isEmpty(self):
def IsEmpty_(self):
return True if len(self.lst) == 0 else False


if __name__ == "__main__":
a = queue()
a.push(5)
a.push(3)
a.push(2)
a.push(1)
a.push_(5)
a.push_(3)
a.push_(2)
a.push_(1)
a.print()
38 changes: 19 additions & 19 deletions DSA/Data Structures/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class stack:
Returns number of values available in the stack.
peek()
Returns the top most number in the stack.
is_empty()
IsEmpty_()
Return a boolean value based on the size of the stack.
print()
Prints the value in the stack in the stack format.
Expand All @@ -22,29 +22,29 @@ class stack:
def __init__(self):
self.lst = []

def push(self, a):
def push_(self, a):
self.lst.append(a)

def pop(self):
if not self.is_empty():
def pop_(self):
if not self.IsEmpty_():
return self.lst.pop()
else:
return "Stack is Empty!"

def size(self):
def size_(self):
return len(self.lst)

def peek(self):
if not self.is_empty():
def peek_(self):
if not self.IsEmpty_():
return self.lst[-1]
else:
print("Stack is Empty!")

def is_empty(self):
def Isempty_(self):
return True if len(self.lst) == 0 else False

def print(self):
if not self.is_empty():
def print_(self):
if not self.IsEmpty_():
print("Stack".center(12))
for i in range(len(self.lst) - 1, -1, -1):
if i == len(self.lst) - 1:
Expand Down Expand Up @@ -79,12 +79,12 @@ def print(self):
print(a)

a = stack()
a.push(1)
a.push(2)
a.push(3)
a.push(5)
print(f"Popped an item off the stack: {a.pop()}")
a.push(4)
a.push(5)
print(f"Size of the stack is : {a.size()}")
a.print()
a.push_(1)
a.push_(2)
a.push_(3)
a.push_(5)
print(f"Popped an item off the stack: {a.pop_()}")
a.push_(4)
a.push_(5)
print(f"Size of the stack is : {a.size_()}")
a.print_()

0 comments on commit c48f277

Please sign in to comment.